find pair of numbers in array that add to given sum

前端 未结 19 2146
萌比男神i
萌比男神i 2020-11-30 20:17

Question: Given an unsorted array of positive integers, is it possible to find a pair of integers from that array that sum up to a given sum?

Constraints: This shou

相关标签:
19条回答
  • 2020-11-30 20:51

    In javascript : This code when n is greater then the time and number of iterations increase. Number of test done by the program will be equal to ((n*(n/2)+n/2) where n is the number of elements.The given sum number is discarded in if (arr[i] + arr[j] === 0) where 0 could be any number given.

    var arr = [-4, -3, 3, 4];          
                    var lengtharr = arr.length;        
                    var i = 0;                         
                    var j = 1;                         
                    var k = 1;                          
                    do {                                                    
                        do {
                            if (arr[i] + arr[j] === 0) { document.write(' Elements arr [' + i + '] [' + j + '] sum 0'); } else { document.write('____'); }
                         j++;
                        } while (j < lengtharr);        
                        k++;
                        j = k;
                        i++;
                    } while (i < (lengtharr-1));        
    
    0 讨论(0)
提交回复
热议问题