Articles for category: Data Structures and Algorithms

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 ...

Majority Element in an Array

Problem Statement You are given an array arr[] containing n integers including duplicates. Write a program to find the majority element in an array if it is present. Else print no Majority Element Found. An element is called a majority element when it appears more than n/2 times in an array of length n. Example Input-1 Output-1 Explanation 9 appears 5 times which is ...

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, ...

Types of Sorting in Data Structures

Sorting is the act of arranging data in ascending or descending order based on linear relationships among data items. It is possible to sort by name, number, and record. Sorting reduces the difficulty of finding information: for example, looking up a friend’s phone number from a telephone dictionary is relatively straightforward because the names in ...

Sorting Techniques in Data Structures

Data structures are formats for organizing, processing, storing, and retrieving data. They can range from simple integer or character data types to more complex structures such as trees and maps. When we run a computer program, we might need to sort the data in a certain order. This is where sorting algorithms come into use. ...

Remove Duplicates from String

Problem Statement Given a string, we have to remove duplicate characters from the string such that each character appears only once (all the characters in the string should become unique). We can print the resultant string in any order. Example Input : “aaabbccd”Output : “abcd” Explanation As we can see the frequency of all the ...

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 ...