Find the numbers from array which are the sum of the other number in O(n) cost

public class NewClass {
    public static void main(String args[])
    {
    int a[]={1,7,8,13,15};
    int i=0;
    int l=0,r=4;
    int find=21;
    while(i<a.length)
    {
    if((a[l]+a[r])==find)
    {
    System.out.println(a[l]+" \t"+a[r]);
    }else if((a[l]+a[r])<find)
    {
    l++;
    }else if((a[l]+a[r])>find)
    {
    r--;
    }
    i++;
    }
           
     }
}

Comments

Popular Posts