Suppose you are given an array of unsorted integers as
A = {3,4,5,1,4,2}
Input : 6
Output : {5,1}, {4,2}
How can I do this in O(n) or O(log n). Any suggestions will be appreciated.
Update:
Can we write something more efficient than this?
for(int i=0;i<array.length-1;i++)
{
if(array[i]+array[i+1]==6)
System.out.println("{"+array[i]+","+array[i+1]+"}");
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…