Given two arrays a[] and b[], we need to build an array c[] such that every element c[i] of c[] contains a value from a[] which is greater than b[i] and is closest to b[i]. If a[] has no greater element than b[i], then value of c[i] is -1. All arrays are of same size.
Examples:
Input : a[] = [ 2, 6, 5, 7, 0]
b[] = [1, 3, 2, 5, 8]
Output : c[] = [2, 5, 5, 7, -1]
Input : a[] = [ 2, 6, 5, 7, 0]
b[] = [0, 2, 3, 5, 1]
Output : c[] = [2, 5, 5, 6, 2]
Naive Approach : For each element in b[], we traverse the whole of a[] and try to find the closest greater element, and save the result for each search. This will cost time complexity of O(n^2).
Efficient Approach : Sort the array a[], and for each b[i], apply binary search in sorted array a[]. For this method our time complexity will be O(nlogn).
Note: For closest greater element we can use upper_bound().
Below is the implementation.
C++
// CPP to find result from target array // for closest element #include <bits/stdc++.h> using namespace std; // Function for printing resultant array void closestResult(int a[], int b[], int n) { // change arr[] to vector vector<int> vect(a, a + n); // sort vector for ease sort(vect.begin(), vect.end()); // iterator for upper_bound vector<int>::iterator up; // vector for result vector<int> c; // calculate resultant array for (int i = 0; i < n; i++) { // check upper bound element up = upper_bound(vect.begin(), vect.end(), b[i]); // if no element found push -1 if (up == vect.end()) c.push_back(-1); // Else push the element else c.push_back(*up); // add to resultant } cout << "Result = "; for (auto it = c.begin(); it != c.end(); it++) cout << *it << " "; } // driver program int main() { int a[] = { 2, 5, 6, 1, 8, 9 }; int b[] = { 2, 1, 0, 5, 4, 9 }; int n = sizeof(a) / sizeof(a[0]); closestResult(a, b, n); return 0; } |
Java
// Java to find result from target array // for closest element import java.util.*; class GFG { // Function for printing resultant array static void closestResult(Integer[] a, int[] b, int n) { // change arr[] to Set TreeSet<Integer> vect = new TreeSet<>(Arrays.asList(a)); // vector for result Vector<Integer> c = new Vector<>(); // calculate resultant array for (int i = 0; i < n; i++) { // check upper bound element Integer up = vect.higher(b[i]); // if no element found push -1 if (up == null) c.add(-1); // Else push the element else c.add(up); // add to resultant } System.out.print("Result = "); for (int i : c) System.out.print(i + " "); } // Driver Code public static void main(String[] args) { Integer[] a = { 2, 5, 6, 1, 8, 9 }; int[] b = { 2, 1, 0, 5, 4, 9 }; int n = a.length; closestResult(a, b, n); } } // This code is contributed by // sanjeev2552 |
Python3
# Python implementation to find result # from target array for closest element import bisect # Function for printing resultant array def closestResult(a, b, n): # sort list for ease a.sort() # list for result c = [] # calculate resultant array for i in range(n): # check location of upper bound element up = bisect.bisect_right(a, b[i]) # if no element found push -1 if up == n: c.append(-1) # else puch the element else: c.append(a[up]) # add to resultant print("Result = ", end = "") for i in c: print(i, end = " ") # Driver code if __name__ == "__main__": a = [2,5,6,1,8,9] b = [2,1,0,5,4,9] n = len(a) closestResult(a, b, n) # This code is contributed by # sanjeev2552 |
Result = 5 2 1 6 5 -1
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:
- Find closest greater value for every element in array
- Closest greater or same value on left side for every element in array
- Find just strictly greater element from first array for each element in second array
- Find closest value for every element in array
- Find closest smaller value for every element in array
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Count of elements in first Array greater than second Array with each element considered only once
- Find Array formed by adding each element of given array with largest element in new array to its left
- Find the next greater element in a Circular Array
- Count of greater elements for each element in the Array
- Smallest element greater than X not present in the array
- Elements greater than the previous and next element in an Array
- Largest element smaller than current element on left for every element in Array
- Sum of elements in an array with frequencies greater than or equal to that element
- First strictly greater element in a sorted array in Java
- Rearrange an array such that every odd indexed element is greater than it previous
- Adding elements of an array until every element becomes greater than or equal to k
- Find element in a sorted array whose frequency is greater than or equal to n/2.
- Minimum element whose n-th power is greater than product of an array of size n
- Count of distinct index pair (i, j) such that element sum of First Array is greater
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.
Improved By : sanjeev2552

