Check if an array is subset of another arrayGiven two arrays a[] and b[] of size m and n respectively, the task is to determine whether b[] is a subset of a[]. Both arrays are not sorted, and elements are distinct.Examples: Input: a[] = [11, 1, 13, 21, 3, 7], b[] = [11, 3, 7, 1] Output: trueInput: a[]= [1, 2, 3, 4, 5, 6], b = [1, 2, 4] Output
13 min read
Two Sum - Pair with given SumGiven an array arr[] of n integers and a target value, the task is to find whether there is a pair of elements in the array whose sum is equal to target. This problem is a variation of 2Sum problem.Examples: Input: arr[] = [0, -1, 2, -3, 1], target = -2Output: trueExplanation: There is a pair (1, -3
15+ min read
Max Distance Between Two OccurrencesGiven an array arr[], the task is to find the maximum distance between two occurrences of any element. If no element occurs twice, return 0.Examples: Input: arr = [1, 1, 2, 2, 2, 1]Output: 5Explanation: distance for 1 is: 5-0 = 5, distance for 2 is: 4-2 = 2, So max distance is 5.Input : arr[] = [3,
8 min read
Most frequent element in an arrayGiven an array, the task is to find the most frequent element in it. If there are multiple elements that appear a maximum number of times, return the maximum element.Examples: Input : arr[] = [1, 3, 2, 1, 4, 1]Output : 1Explanation: 1 appears three times in array which is maximum frequency.Input : a
10 min read
Only Repeating From 1 To n-1Given an array arr[] of size n filled with numbers from 1 to n-1 in random order. The array has only one repetitive element. The task is to find the repetitive element.Examples:Input: arr[] = [1, 3, 2, 3, 4]Output: 3Explanation: The number 3 is the only repeating element.Input: arr[] = [1, 5, 1, 2,
15+ min read
Check for Disjoint Arrays or SetsGiven two arrays a and b, check if they are disjoint, i.e., there is no element common between both the arrays.Examples: Input: a[] = {12, 34, 11, 9, 3}, b[] = {2, 1, 3, 5} Output: FalseExplanation: 3 is common in both the arrays.Input: a[] = {12, 34, 11, 9, 3}, b[] = {7, 2, 1, 5} Output: True Expla
11 min read
Non-overlapping sum of two setsGiven two arrays A[] and B[] of size n. It is given that both array individually contains distinct elements. We need to find the sum of all elements that are not common.Examples: Input : A[] = {1, 5, 3, 8} B[] = {5, 4, 6, 7}Output : 291 + 3 + 4 + 6 + 7 + 8 = 29Input : A[] = {1, 5, 3, 8} B[] = {5, 1,
9 min read
Find missing elements of a rangeGiven an array, arr[0..n-1] of distinct elements and a range [low, high], find all numbers that are in a range, but not the array. The missing elements should be printed in sorted order.Examples: Input: arr[] = {10, 12, 11, 15}, low = 10, high = 15Output: 13, 14Input: arr[] = {1, 14, 11, 51, 15}, lo
15+ min read
2 Sum - Count pairs with given sumGiven an array arr[] of n integers and a target value, the task is to find the number of pairs of integers in the array whose sum is equal to target.Examples: Input: arr[] = {1, 5, 7, -1, 5}, target = 6Output: 3Explanation: Pairs with sum 6 are (1, 5), (7, -1) & (1, 5). Input: arr[] = {1, 1, 1,
9 min read
Sort elements by frequency | Set 4 (Efficient approach using hash)Print the elements of an array in the decreasing frequency if 2 numbers have the same frequency then print the one which came first. Examples: Input : arr[] = {2, 5, 2, 8, 5, 6, 8, 8} Output : arr[] = {8, 8, 8, 2, 2, 5, 5, 6} Input : arr[] = {2, 5, 2, 6, -1, 9999999, 5, 8, 8, 8} Output : arr[] = {8,
12 min read
Find all pairs (a, b) in an array such that a % b = kGiven an array with distinct elements, the task is to find the pairs in the array such that a % b = k, where k is a given integer. You may assume that a and b are in small range Examples : Input : arr[] = {2, 3, 5, 4, 7} k = 3Output : (7, 4), (3, 4), (3, 5), (3, 7)7 % 4 = 33 % 4 = 33 % 5 = 33 % 7 =
15 min read
Group words with same set of charactersGiven a list of words with lower cases. Implement a function to find all Words that have the same unique character set. Example: Input: words[] = { "may", "student", "students", "dog", "studentssess", "god", "cat", "act", "tab", "bat", "flow", "wolf", "lambs", "amy", "yam", "balms", "looped", "poodl
8 min read
k-th distinct (or non-repeating) element among unique elements in an array.Given an integer array arr[], print kth distinct element in this array. The given array may contain duplicates and the output should print the k-th element among all unique elements. If k is more than the number of distinct elements, print -1.Examples:Input: arr[] = {1, 2, 1, 3, 4, 2}, k = 2Output:
7 min read