Closest pair sum in two sorted arrays

后端 未结 3 466
感动是毒
感动是毒 2021-01-03 04:40

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]         


        
3条回答
  •  借酒劲吻你
    2021-01-03 05:05

    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

提交回复
热议问题