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

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

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