Closest greater element for every array element from another array
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++
#include <bits/stdc++.h>
using namespace std;
void closestResult( int a[], int b[], int n)
{
vector< int > vect(a, a + n);
sort(vect.begin(), vect.end());
vector< int >::iterator up;
vector< int > c;
for ( int i = 0; i < n; i++) {
up = upper_bound(vect.begin(), vect.end(), b[i]);
if (up == vect.end())
c.push_back(-1);
else
c.push_back(*up);
}
cout << "Result = " ;
for ( auto it = c.begin(); it != c.end(); it++)
cout << *it << " " ;
}
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
import java.util.*;
class GFG
{
static void closestResult(Integer[] a,
int [] b, int n)
{
TreeSet<Integer> vect = new TreeSet<>(Arrays.asList(a));
Vector<Integer> c = new Vector<>();
for ( int i = 0 ; i < n; i++)
{
Integer up = vect.higher(b[i]);
if (up == null )
c.add(- 1 );
else
c.add(up);
}
System.out.print( "Result = " );
for ( int i : c)
System.out.print(i + " " );
}
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);
}
}
|
Python3
import bisect
def closestResult(a, b, n):
a.sort()
c = []
for i in range (n):
up = bisect.bisect_right(a, b[i])
if up = = n:
c.append( - 1 )
else :
c.append(a[up])
print ( "Result = " , end = "")
for i in c:
print (i, end = " " )
if __name__ = = "__main__" :
a = [ 2 , 5 , 6 , 1 , 8 , 9 ]
b = [ 2 , 1 , 0 , 5 , 4 , 9 ]
n = len (a)
closestResult(a, b, n)
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class HelloWorld {
public static void closestResult( int [] a, int [] b, int n)
{
HashSet< int > vect = new HashSet< int >();
for ( int i = 0; i < n; i++){
vect.Add(a[i]);
}
List< int > c = new List< int >();
for ( int i = 0; i < n; i++) {
int up = -1;
foreach ( int elem in vect){
if (elem > b[i] && (up == -1 || elem < up)){
up = elem;
}
}
if (up == -1) c.Add(-1);
else c.Add(up);
}
Console.Write( "Result = " );
foreach ( var i in c){
Console.Write(i + " " );
}
}
static void Main() {
int [] a = { 2, 5, 6, 1, 8, 9 };
int [] b = { 2, 1, 0, 5, 4, 9 };
int n = a.Length;
closestResult(a, b, n);
}
}
|
Javascript
function closestResult(a, b, n) {
let vect = new Set(a);
let c = [];
for (let i = 0; i < n; i++) {
let up = null ;
vect.forEach((elem) => {
if (elem > b[i] && (up == null || elem < up)) {
up = elem;
}
});
if (up == null ) c.push(-1);
else c.push(up);
}
console.log( "Result =" , c.join( " " ));
}
let a = [2, 5, 6, 1, 8, 9];
let b = [2, 1, 0, 5, 4, 9];
let n = a.length;
closestResult(a, b, n);
|
Output:
Result = 5 2 1 6 5 -1
Time Complexity: O(n log2(n))
Auxiliary Space: O(n)
Last Updated :
09 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...