3 Sum - Find All Triplets with Zero SumGiven an array arr[], the task is to find all possible indices {i, j, k} of triplet {arr[i], arr[j], arr[k]} such that their sum is equal to zero and all indices in a triplet should be distinct (i != j, j != k, k != i). We need to return indices of a triplet in sorted order, i.e., i < j < k.Ex
11 min read
Find the element before which all the elements are smaller than it, and after which all are greaterGiven an array, find an element before which all elements are equal or smaller than it, and after which all the elements are equal or greater.Note: Print -1, if no such element exists.Examples:Input: arr[] = [5, 1, 4, 3, 6, 8, 10, 7, 9]Output: 6 Explanation: 6 is present at index 4. All elements on
14 min read
Largest pair sum in an arrayGiven an unsorted of distinct integers, find the largest pair sum in it. For example, the largest pair sum is 74. If there are less than 2 elements, then we need to return -1.Input : arr[] = {12, 34, 10, 6, 40}, Output : 74Input : arr[] = {10, 10, 10}, Output : 20Input arr[] = {10}, Output : -1[Naiv
10 min read
Kâth Smallest Element in Unsorted ArrayGiven an array arr[] of N distinct elements and a number K, where K is smaller than the size of the array. Find the K'th smallest element in the given array. Examples:Input: arr[] = {7, 10, 4, 3, 20, 15}, K = 3 Output: 7Input: arr[] = {7, 10, 4, 3, 20, 15}, K = 4 Output: 10 Table of Content[Naive Ap
15 min read
Search in a Sorted and Rotated ArrayGiven a sorted and rotated array arr[] of n distinct elements, the task is to find the index of given key in the array. If the key is not present in the array, return -1. Examples: Input: arr[] = [5, 6, 7, 8, 9, 10, 1, 2, 3], key = 3Output: 8Explanation: 3 is present at index 8 in arr[].Input: arr[]
15+ min read
Minimum in a Sorted and Rotated ArrayGiven a sorted array of distinct elements arr[] of size n that is rotated at some unknown point, the task is to find the minimum element in it. Examples: Input: arr[] = [5, 6, 1, 2, 3, 4]Output: 1Explanation: 1 is the minimum element present in the array.Input: arr[] = [3, 1, 2]Output: 1Explanation:
9 min read
Find the closest pair from two sorted arraysGiven two arrays arr1[0...m-1] and arr2[0..n-1], and a number x, the task is to find the pair arr1[i] + arr2[j] such that absolute value of (arr1[i] + arr2[j] - x) is minimum. Example: Input: arr1[] = {1, 4, 5, 7}; arr2[] = {10, 20, 30, 40}; x = 32Output: 1 and 30Input: arr1[] = {1, 4, 5, 7}; arr2[]
15+ min read
Find three closest elements from given three sorted arraysGiven three sorted arrays A[], B[] and C[], find 3 elements i, j and k from A, B and C respectively such that max(abs(A[i] - B[j]), abs(B[j] - C[k]), abs(C[k] - A[i])) is minimized. Here abs() indicates absolute value. Example : Input : A[] = {1, 4, 10} B[] = {2, 15, 20} C[] = {10, 12} Output: 10 15
15+ min read