waltermilner

Untitled

Apr 11th, 2016
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class Test {
  4.  
  5.   public static void main(String[] args) {
  6.     int[] a = {8, 3,-4, 7, 6, 4, 3, 2, 1, 0};
  7.     int x = 9;
  8.     Arrays.sort(a);
  9.     int n = a.length;
  10.     int bottom = 0;
  11.     int top = a.length - 1;
  12.     int steps=0;
  13.     System.out.print("Sorted array: ");
  14.     for (int v : a)
  15.       System.out.print(v+" ");
  16.     System.out.println();
  17.     while (bottom < top) {
  18.       steps++;
  19.       System.out.println("Check "+a[bottom]+" and "+a[top]);
  20.       int sum = a[bottom] + a[top];
  21.       if (sum == x) {
  22.         System.out.println("Found : "+a[bottom] + " " + a[top]);
  23.         if (a[bottom]==a[bottom+1])
  24.           bottom++; // repeated values
  25.         else
  26.          top--;
  27.         continue;
  28.       }
  29.       if (sum>x)
  30.         top--;
  31.       else // sum too small
  32.         bottom++;
  33.  
  34.     }
  35. System.out.println(steps+" after sort");
  36.   }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment