Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class Test {
- public static void main(String[] args) {
- int[] a = {8, 3,-4, 7, 6, 4, 3, 2, 1, 0};
- int x = 9;
- Arrays.sort(a);
- int n = a.length;
- int bottom = 0;
- int top = a.length - 1;
- int steps=0;
- System.out.print("Sorted array: ");
- for (int v : a)
- System.out.print(v+" ");
- System.out.println();
- while (bottom < top) {
- steps++;
- System.out.println("Check "+a[bottom]+" and "+a[top]);
- int sum = a[bottom] + a[top];
- if (sum == x) {
- System.out.println("Found : "+a[bottom] + " " + a[top]);
- if (a[bottom]==a[bottom+1])
- bottom++; // repeated values
- else
- top--;
- continue;
- }
- if (sum>x)
- top--;
- else // sum too small
- bottom++;
- }
- System.out.println(steps+" after sort");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment