How to write an algorithm to check if the sum of any two numbers in an array/list matches a given number?

前端 未结 14 1954
萌比男神i
萌比男神i 2021-01-30 11:29

How can I write an algorithm to check if the sum of any two numbers in an array/list matches a given number with a complexity of nlogn?

14条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-30 12:01

    Step 1 : Sort the array in O(n logn)

    Step 2 : Find two indices

    0<=i

    int i=0,j=n;
    while(i k)
            j--;
    }
    

提交回复
热议问题