Articles for category: Data Structures and Algorithms

n Queen Problem

What is N Queen Problem? The N-Queen is the problem of placing n queens on a chessboard of dimensions $n\times n$ such that no queen can attack another queen in a single move. Problem Statement We need to check if there exists such an arrangement of n queens and if it exists then print the ...

Largest Subarray with 0 Sum

Problem Statement Given the array $ar[]$ of size, $n$ has positive and negative integers. From the array $ar[]$, find the length of the largest subarray having a 0 sum. As we have to find the length of the largest subarray. So, first of all, it is required to understand what is a subarray Subarray :A ...

Merge K Sorted Arrays

Problem Statement k number of arrays are given, the task is to merge all the given integer arrays in such a manner that the resultant array is sorted in the runtime only. Note: arrays are of equal size. Example: Output: Explanation The array at the output is sorted by combining elements of all the three ...

Intersection of Two Arrays

Intersection Of Two Arrays Given two arrays Arr1[] and Arr2[] of length n and m, respectively. The task is to find the intersection of elements of Arr1 and Arr2. The intersection is the list of common and distinct elements between Arr1 and Arr2. The intersection list can be in any order. Example Input: Output: Explanation: Common elements between Arr1 and Arr2 is 1, 2, 3, 4, 5. So, [1, ...

Median of Array

Problem Statement An unsorted array of size n is given. write a program to find the median of an array. The median of array is the middle element of a sorted array in case of odd number of elements in an array and average of middle two elements when the number of elements in an array is even. Example Input-1 Output-1 ...

In Simple Chaining, What Data Structure is Appropriate?

Simple chaining (sometimes referred to as Separate chaining), is a collision resolution strategy followed in the concept of Hashing. As per the simple chaining strategy whenever there is a collision in HashTable, instead of looking for any other slot we prefer putting the data in the initial bucket i.e.i.e. generated by the hash function. So we would require some sort ...

Implementation of Queue Using Array

Problem Statement Write a program that implements the operations of queue using an array Implementation of Queue using Array Operations The queue is the linear data structure that works on the principle of FIFO( First In First Out). In queue insertion and deletion of elements takes place at two different ends. The addition of an element ...

Kosaraju Algorithm

Problem Statement Find all strongly connected components in O(V+E) time using Kosaraju’s algorithm. Strongly connected Component(SCC) – In a directed graph a strongly connected component is a component of the graph in which there is a path between every pair of vertices. Example Example of strongly connected component in a graph : Example Explanation In the above Example diagram ...

Line Sweep Algorithm

The line sweep algorithm is based on the idea of using a vertical imaginary line that moves in the rightward direction upon the occurrences of certain events defined by the current problem at hand. The line sweep algorithm is used to solve problems such as closest pair, line segment intersections, the union of rectangles, convex hull, Manhattan ...