Given two unsorted arrays arr1[] and arr2[], the task is to find the sum of elements of arr1[] such that number of elements less than or equal to them in arr2[] is maximum.
Examples:
Input: arr1[] = {1, 2, 3, 4, 7, 9}, arr2[] = {0, 1, 2, 1, 1, 4}
Output: 20
Below table shows the count of elements in arr2[] which are ≤ the elements of arr1[]
arr1[i] Count 1 4 2 5 3 5 4 6 7 6 9 6 Count for 4, 7 and 9 is maximum.
Hence, the resultant sum is 4 + 7 + 9 = 20.Input:arr1[] = {5, 10, 2, 6, 1, 8, 6, 12}, arr2[] = {6, 5, 11, 4, 2, 3, 7}
Output: 12
Approach: Idea behind efficient solution for the above problem is to use hashing of second array and then finding the cumulative sum of hashed array. After that the count of elements in second array less than or equal to elements of 1st array can easily be calculated. This will give a frequency array which represents the count of elements in second array less than or equal to elements of 1st array from where the sum of elements of first array can be calculated corresponding to maximum frequency in frequency array.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <iostream> using namespace std; #define MAX 100000 // Function to return the required sum int findSumofEle(int arr1[], int m, int arr2[], int n) { // Creating hash array initially // filled with zero int hash[MAX] = { 0 }; // Calculate the frequency // of elements of arr2[] for (int i = 0; i < n; i++) hash[arr2[i]]++; // Running sum of hash array // such that hash[i] will give count of // elements less than or equal to i in arr2[] for (int i = 1; i < MAX; i++) hash[i] = hash[i] + hash[i - 1]; // To store the maximum value of // the number of elements in arr2[] which are // smaller than or equal to some element of arr1[] int maximumFreq = 0; for (int i = 0; i < m; i++) maximumFreq = max(maximumFreq, hash[arr1[i]]); // Calculate the sum of elements from arr1[] // corresponding to maximum frequency int sumOfElements = 0; for (int i = 0; i < m; i++) sumOfElements += (maximumFreq == hash[arr1[i]]) ? arr1[i] : 0; // Return the required sum return sumOfElements; } // Driver code int main() { int arr1[] = { 2, 5, 6, 8 }; int arr2[] = { 4, 10 }; int m = sizeof(arr1) / sizeof(arr1[0]); int n = sizeof(arr2) / sizeof(arr2[0]); cout << findSumofEle(arr1, m, arr2, n); return 0; } |
Java
// Java implementation of the approach class GFG { static int MAX = 100000; // Function to return the required sum static int findSumofEle(int arr1[], int m, int arr2[], int n) { // Creating hash array initially // filled with zero int hash[] = new int[MAX]; // Calculate the frequency // of elements of arr2[] for (int i = 0; i < n; i++) { hash[arr2[i]]++; } // Running sum of hash array // such that hash[i] will give count of // elements less than or equal to i in arr2[] for (int i = 1; i < MAX; i++) { hash[i] = hash[i] + hash[i - 1]; } // To store the maximum value of // the number of elements in arr2[] which are // smaller than or equal to some element of arr1[] int maximumFreq = 0; for (int i = 0; i < m; i++) { maximumFreq = Math.max(maximumFreq, hash[arr1[i]]); } // Calculate the sum of elements from arr1[] // corresponding to maximum frequency int sumOfElements = 0; for (int i = 0; i < m; i++) { sumOfElements += (maximumFreq == hash[arr1[i]]) ? arr1[i] : 0; } // Return the required sum return sumOfElements; } // Driver code public static void main(String[] args) { int arr1[] = {2, 5, 6, 8}; int arr2[] = {4, 10}; int m = arr1.length; int n = arr2.length; System.out.println(findSumofEle(arr1, m, arr2, n)); } } // This code has been contributed by 29AjayKumar |
Python3
# Python 3 implementation of the approach MAX = 100000 # Function to return the required sum def findSumofEle(arr1, m, arr2, n): # Creating hash array initially # filled with zero hash = [0 for i in range(MAX)] # Calculate the frequency # of elements of arr2[] for i in range(n): hash[arr2[i]] += 1 # Running sum of hash array # such that hash[i] will give count of # elements less than or equal to i in arr2[] for i in range(1, MAX, 1): hash[i] = hash[i] + hash[i - 1] # To store the maximum value of # the number of elements in arr2[] # which are smaller than or equal # to some element of arr1[] maximumFreq = 0 for i in range(m): maximumFreq = max(maximumFreq, hash[arr1[i]]) # Calculate the sum of elements from arr1[] # corresponding to maximum frequency sumOfElements = 0 for i in range(m): if (maximumFreq == hash[arr1[i]]): sumOfElements += arr1[i] # Return the required sum return sumOfElements # Driver code if __name__ == '__main__': arr1 = [2, 5, 6, 8] arr2 = [4, 10] m = len(arr1) n = len(arr2) print(findSumofEle(arr1, m, arr2, n)) # This code is contributed by # Surendra_Gangwar |
C#
// C# implementation of the approach using System; class GFG { static int MAX = 100000; // Function to return the required sum static int findSumofEle(int[] arr1, int m, int[] arr2, int n) { // Creating hash array initially // filled with zero int[] hash = new int[MAX]; // Calculate the frequency // of elements of arr2[] for (int i = 0; i < n; i++) { hash[arr2[i]]++; } // Running sum of hash array // such that hash[i] will give count of // elements less than or equal to i in arr2[] for (int i = 1; i < MAX; i++) { hash[i] = hash[i] + hash[i - 1]; } // To store the maximum value of // the number of elements in arr2[] which are // smaller than or equal to some element of arr1[] int maximumFreq = 0; for (int i = 0; i < m; i++) { maximumFreq = Math.Max(maximumFreq, hash[arr1[i]]); } // Calculate the sum of elements from arr1[] // corresponding to maximum frequency int sumOfElements = 0; for (int i = 0; i < m; i++) { sumOfElements += (maximumFreq == hash[arr1[i]]) ? arr1[i] : 0; } // Return the required sum return sumOfElements; } // Driver code public static void Main() { int[] arr1 = {2, 5, 6, 8}; int[] arr2 = {4, 10}; int m = arr1.Length; int n = arr2.Length; Console.WriteLine(findSumofEle(arr1, m, arr2, n)); } } // This code has been contributed by Code_Mech. |
PHP
<?php // PHP implementation of the approach $MAX = 10000; // Function to return the required sum function findSumofEle($arr1, $m, $arr2, $n) { // Creating hash array initially // filled with zero $hash = array_fill(0, $GLOBALS['MAX'], 0); // Calculate the frequency // of elements of arr2[] for ($i = 0; $i < $n; $i++) $hash[$arr2[$i]]++; // Running sum of hash array // such that hash[i] will give count of // elements less than or equal to i in arr2[] for ($i = 1; $i < $GLOBALS['MAX']; $i++) $hash[$i] = $hash[$i] + $hash[$i - 1]; // To store the maximum value of // the number of elements in arr2[] which are // smaller than or equal to some element of arr1[] $maximumFreq = 0; for ($i = 0; $i < $m; $i++) $maximumFreq = max($maximumFreq, $hash[$arr1[$i]]); // Calculate the sum of elements from arr1[] // corresponding to maximum frequency $sumOfElements = 0; for ($i = 0; $i < $m; $i++) $sumOfElements += ($maximumFreq == $hash[$arr1[$i]]) ? $arr1[$i] : 0; // Return the required sum return $sumOfElements; } // Driver code $arr1 = array( 2, 5, 6, 8 ); $arr2 = array( 4, 10 ); $m = count($arr1); $n = count($arr2); echo findSumofEle($arr1, $m, $arr2, $n); // This code is contributed by Ryuga ?> |
19
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- For each element in 1st array count elements less than or equal to it in 2nd array
- For each element in 1st array count elements less than or equal to it in 2nd array | Set 2
- Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array
- Rearrange an array in order - smallest, largest, 2nd smallest, 2nd largest, ..
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Find maximum number of elements such that their absolute difference is less than or equal to 1
- Maximum sum subarray having sum less than or equal to given sum
- Maximum sum subarray having sum less than or equal to given sum using Set
- Find three integers less than or equal to N such that their LCM is maximum
- Largest number less than N with digit sum greater than the digit sum of N
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Find maximum sum array of length less than or equal to m
- Number of sub-sequence such that it has one consecutive element with difference less than or equal to 1
- Maximum subset sum such that no two elements in set have same digit in them
- Highest and Smallest power of K less than and greater than equal to N respectively
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Sum of all array elements less than X and greater than Y for Q queries
- Maximum length L such that the sum of all subarrays of length L is less than K
- Maximum subarray size, such that all subarrays of that size have sum less than k
- Number of elements less than or equal to a number in a subarray : MO's Algorithm
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

