Articles for category: Data Structures and Algorithms

Remove Duplicates from an Unsorted Linked List

Problem Statement Write a program to delete duplicate nodes present in an unsorted linked list. Print the original linked list, and the linked list obtained after the deletions. Example Consider the following linked list : After you remove duplicates from the linked list : Example Explanation The given unsorted linked list is : There are multiple occurrences ...

Subarray with Given Sum

Problem Statement An unsorted array nums and an integer sum k is given. Array has non- negative integers in it. Find out the continuous subarray which elements sums up to the given sum. There can be multiple such subarray with given sum, print out the first such subarray with given sum. If the subarray with ...

Reverse a Linked List

Problem Statement The objective is to reverse a given linked list, which involves altering the pointers between nodes such that the last node becomes the new head node, and each subsequent node points to its predecessor. For Example: Input1 : Output1 : Input2 : Output2 : Input3 : Output3 : Approach 1 : Iterative Method ...

Reverse an Array

Problem Statement Given an array arr[] of length N, print the reverse of it. Example Consider the following example: After reversing this array,we have the new array as Example Explanation The initial array [1,2,3,4,5] when reversed gives [5,4,3,2,1]. Constraints $1 <= N <= 10^5$ There N is the length of the array. Approach 1- Recursive ...

Rearrange Array Alternately

Problem Statement We are provided with a sorted array of size n and we need to rearrange the array alternately. By rearrange array alternately, we should rearrange the array so that the first greatest element will come at the first position, and the first smallest element comes at the second position. Similarly, the second greatest element comes at ...

Serialize and Deserialize a Binary Tree

The need for data structures also demands the ability to convert them into a storable format and reconstruct back later. Serialization is the process of converting a data structure into a sequence of bits. And deserialization is the process of reconstructing the data structure from the serialized sequence. Introduction Data structures play a crucial role in designing efficient software, and ...

Rotate A Linked List

Problem Statement You are given the head of a singly linked list and an integer K, write a program to Rotate the Linked List in a clockwise direction by K positions from the last node. Example Input-1 Output-1 Explanation Here, we have rotated the linked list exactly 2 times. On the First rotation 5 becomes head and 4 becomes ...

Remove Duplicates from Array

Problem Statement Given an array of integers, we have to remove duplicates from array such that each element in the array appears only once (all the values in the array should become unique). Example Input : [1,2,2,3,4,4,4,5,5] Output : [1,2,3,4,5] Explanation As we can see the frequency of all the elements Element Frequency 1 1 2 2 3 1 ...

Sliding Window Algorithm

Sliding Window Algorithm is a technique for reducing the complexity of algorithms. It is used such that the need for reusing the loops gets reduced and hence the program gets optimized. In this technique, we reuse the result of the previous step to compute the result of the next step. What is Sliding Window Algorithm? It ...

kth Smallest Element

Problem Statement We are given an integer array nums and an integer k. We have to return the kth smallest element in the array where $k <=$ size of nums. Also, note that we have to return the $kth$ smallest element not the kth distinct element. Example Example Explanation If we sort the above array, ...