Exemplo n.º 1
0
  public static int findSameMin(int[] A, int[] B) {

    Arrays.sort(A);
    Arrays.sort(B);

    int indexA = 0;
    int indexB = 0;

    while (indexA < A.length && indexB < B.length) {
      int headOfA = A[indexA];
      int headOfB = B[indexB];

      if (headOfA == headOfB) {
        return headOfA;
      } else if (headOfA < headOfB) {
        indexA++;
      } else {
        indexB++;
      }
    }

    return -1;
  }
Exemplo n.º 2
0
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int times = in.nextInt();

    for (int i = 0; i < times; i++) {
      int size = in.nextInt();
      int[] nums = new int[size];
      for (int s = 0; s < size; s++) {
        nums[s] = in.nextInt();
      }
      Arrays.sort(nums);
      out.println("Case " + (i + 1) + ": " + nums[nums.length / 2]);
    }
  }
Exemplo n.º 3
0
  private static List<List<Integer>> threeSum(int[] num) {
    List resultList = new ArrayList<ArrayList>();
    Arrays.sort(num);
    int length = num.length;
    for (int i = 0; i < length - 2; ) {
      int j = i + 1, k = length - 1;
      while (j < k) {
        int sum = num[i] + num[j] + num[k];
        if (sum == 0) {
          List<Integer> list = new ArrayList<Integer>();
          list.add(num[i]);
          list.add(num[j]);
          list.add(num[k]);
          resultList.add(list);
          do {
            j++;
          } while (num[j] == num[j - 1]);
          do {
            k--;
          } while (num[k] == num[k + 1]);
        } else if (sum > 0) {
          do {
            k--;
          } while (num[k] == num[k + 1]);
        } else {
          do {
            j++;
          } while (num[j] == num[j - 1]);
        }
      }

      do {
        i++;
      } while (num[i] == num[i - 1]);
    }

    return resultList;
  }