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

前端 未结 14 2070
萌比男神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:20

    This is in Java : This even removes the possible duplicates.. Runtime - O(n^2)

    private static int[] intArray = {15,5,10,20,25,30};
    private static int sum = 35;
    
    private static void algorithm()
    {
        Map intMap = new Hashtable();
        for (int i=0; i

提交回复
热议问题