Given two sorted arrays of integers, a
and b
, and an integer c
, I have to find i,j
such that:
a[i] + b[j]
The below solution is in linear Time Complexity O(n), Space Complexity O(1)
public class ClosestPair {
public static void main(String[] args)
{
int ar2[] = {4, 5, 7};
int ar1[] = {10, 20, 30, 40};
int x = 10 ;
closest(ar1,ar2,x);
}
public static void closest(int[] ar1, int[] ar2, int x)
{ int diff=Integer.MAX_VALUE;
int first_num=0;
int second_num=0;
int second_diff=Integer.MAX_VALUE;
for(int i=0; i