Arrays: Find the median of 2 sorted arrays of same size

Problem Statement: You are given 2 sorted arrays of same size. You need to obtain the median (middle) of the array. But first, what is a median? Median is an element separating higher half of the data from a lower half. It can be thought of as a middle value. Let n be the size […]

Arrays: Get minimum number of merge operations to make an array palindrome

Problem Statement: You are given an array, you need to find the minimum number of merge operations to be performed to make the array as a palindrome. You can only merge 2 adjacent elements. Example: arr[] = {1, 4, 5, 1} Output: 1 Here if you merge [4,5], the resultant array will be [1, 9, […]

Arrays: Three way partitioning of an array around a given range

Problem Statement: Given an array and 2 values [a, b], you need to create a partition such that: 1. Elements of the first partition will be lesser than “a” 2. Next partition such that elements lies within the given range will be in this partition 3. Numbers greater than the “b” will be the third […]

Arrays: Find a triplet that sum to a given value

Problem Statement: Given an unsorted array and a key, you need to find 3 elements in the array that is equal to the key. Example: arr = {2, 1, 10, 4, 5}, sum = 9; Output: 4, 5 Solution This problem can be solved with many different approach, in this article we shall learn about […]

Arrays: Find whether an array is subset of another array

Problem Statement: Given 2 arrays, check if arr2 is a subset of arr1. Consider all the elements are distinct and both the arrays are not sorted. We can solve this be number of different ways. Some of the ways that are discussed are: 1. Use 2 loops 2. Sorting and binary search method. 1. Using […]

Arrays: Rearrange the array with one positive and one negative number.

Problem Statement: Given an unsorted array with combination of positive and negative integers. you need to re arrange them in one positive and one negative format. Any extra +ve or -ve numbers should come at the end. Solution: This problem can be solved in number of different ways. In this article we shall discuss a […]

Arrays: Find common element in 3 sorted arrays.

Problem Statement: Given 3 sorted arrays, you need to find all the common elements in all the 3 elements. arr1 = 1 3 5 7 9 arr2 = 2 3 6 7 9 arr3 = 1 2 3 4 5 6 7 8 9 Common elements = 3 7 9 This problem can be solved […]

Arrays: Inversion Count in an Array

Problem Statement: You are given an unsorted array, you need to return the inversion count. Inversion count is the number of swaps required to make the array as sorted. If the input array is already sorted, then inversion count is 0. Example 1: arr[] = {3, 1, 2} Output: 2 Because, the above array can […]

Arrays: Merge Two Sorted arrays without using extra space

You are given 2 sorted arrays, merge and sort the two arrays in such a way that the initial numbers are in the first array other will be in the next array, Example: Input: arr1[] = {20}; arr2[] = {4, 5}; Now, the combined sorted array will be {4, 5, 20} As arr1 has only […]