PackageShuzu;/** Enter an array that has been sorted in ascending order and a number * Find two numbers in the array so that their sum is the number entered, requiring a time complexity of O (n) * If there are many pairs of numbers and equals the input number, the output can be any pair. */ Public classDemo1 {Private Static voidFindans (int[] Data,intsum) { intSize=data.length; intBegin =0; intEnd=size-1; while(Begin < Size && End >= 0 && begin <end) { intCU = Data[begin] +Data[end]; if(Cu >sum) {End--; }Else if(Cu <sum) {Begin++; }Else{System.out.println (Data[begin]+","+Data[end]); return; }} System.out.println ("Cannot match item"); } Public Static voidMain (string[] args) {inta[]= {1,3,5,8,12,15}; Findans (A,15); }}
Enter an array that has been sorted in ascending order and a number to find two numbers in the array so that their and is the number entered