Exemplo n.º 1
0
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int T = sc.nextInt();

    long d3 = 3, d5 = 5, d15 = 15;
    long n3 = 0, n5 = 0, n15 = 0;
    if ((1 <= T) && (T <= 100000)) {
      for (int j = 0; j < T; j++) {
        long sum3 = 0, sum5 = 0, sum15 = 0, sum = 0;
        long l3, l5, l15 = 0;

        int N = sc.nextInt();
        if ((1 <= N) && (N <= 1000000000)) {
          N = N - 1;
          l3 = N % 3;
          l3 = N - l3;
          n3 = (l3 - 3) / 3 + 1;
          sum3 = (3 * ((n3) + 1) * (n3)) / 2;

          l5 = N % 5;
          l5 = N - l5;
          n5 = (l5 - 5) / 5 + 1;
          sum5 = (5 * ((n5) + 1) * (n5)) / 2;

          l15 = N % 15;
          l15 = N - l15;
          n15 = (l15 - 15) / 15 + 1;
          sum15 = (15 * ((n15) + 1) * (n15)) / 2;

          sum = sum3 + sum5 - sum15;
          System.out.println(sum);
        }
      }
    }
  }
  public static void main(String[] args) {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */

    try {

      Scanner sc = new Scanner(new File("/home/santosh/Desktop/testData"));

      int testCases = sc.nextInt();
      int i = 0;
      ArrayList<ArrayList<Integer>> inputArraysList = new ArrayList<ArrayList<Integer>>();
      ArrayList<Integer> list;
      while (i++ < testCases) {

        int n = sc.nextInt();
        int k = 0;
        list = new ArrayList<Integer>();
        while (k < n) {
          list.add(sc.nextInt());
          k++;
        }
        inputArraysList.add(list);
      }

      // System.out.println(inputArraysList.size());
      for (ArrayList<Integer> arr : inputArraysList) {
        countPairs(arr.toArray(new Integer[arr.size()]));
      }

    } catch (Exception e) {

    }
  }
Exemplo n.º 3
0
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner in = new Scanner(System.in);
    int t = in.nextInt();
    TreeMap<Long, Long> tmap = new TreeMap<>();
    long res = 0;
    while (t-- > 0) {
      res = 0;
      int n = in.nextInt();
      long m = in.nextLong();
      tmap.clear();
      long[] arr = new long[n];
      res = in.nextLong();
      arr[0] = res % m;
      res = Long.MIN_VALUE;
      tmap.put(arr[0], arr[0]);
      for (int i = 1; i < arr.length; i++) {
        arr[i] = in.nextLong();
        arr[i] %= m;
        arr[i] += arr[i - 1];
        arr[i] %= m;

        if (tmap.higherEntry(arr[i]) == null) {
          res = Math.max(res, arr[i]);
          tmap.put(arr[i], arr[i]);
          continue;
        }
        long val = tmap.higherEntry(arr[i]).getValue();
        res = Math.max(res, (arr[i] - val + m) % m);
        tmap.put(arr[i], arr[i]);
      }

      System.out.println(res);
    }
  }
Exemplo n.º 4
0
  public static void main(String[] args) {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    Scanner in = new Scanner(System.in);

    int N = in.nextInt();

    Node[] nodes = new Node[N];

    for (int i = 0; i < N; i++) {
      nodes[i] = new Node(i + 1);
    }

    for (int i = 0; i < N; i++) {
      int a = in.nextInt();
      int b = in.nextInt();

      Node n = nodes[i];

      if (a != -1) n.left = nodes[a - 1];
      if (b != -1) n.right = nodes[b - 1];
    }

    int T = in.nextInt();

    for (int i = 0; i < T; i++) {
      int K = in.nextInt();

      swap(nodes[0], K, 1);

      Inorder(nodes[0]);
      System.out.println("");
    }
  }
Exemplo n.º 5
0
  public void solve() throws Exception {
    int n = in.nextInt();
    int[] a = new int[n];
    int sum = 0;

    for (int i = 0; i < n; i++) {
      a[i] = in.nextInt();
      sum += a[i];
    }

    sum /= 2;

    boolean[] dp = new boolean[sum + 1];
    dp[0] = true;

    for (int tmp : a) {
      for (int i = sum; i >= tmp; i--) {
        if (dp[i - tmp]) {
          dp[i] = true;
        }
      }
    }

    String ans = dp[sum] ? "YES" : "NO";
    out.println(ans);
  }
Exemplo n.º 6
0
 public static void main(String[] args) {
   /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
   Scanner in = new Scanner(System.in);
   int cases = in.nextInt();
   for (int i = 0; i < cases; i++) {
     System.out.println(digits(in.nextInt()));
   }
 }
 public static void main(String[] args) {
   /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
   Scanner in = new Scanner(System.in);
   int numberOfTestCases = in.nextInt();
   for (int i = 0; i < numberOfTestCases; i++) {
     int n = in.nextInt();
     System.out.println((n * (n - 1)) / 2);
   }
 }
 public static void main(String[] args) {
   Scanner in = new Scanner(System.in);
   int t = in.nextInt();
   for (int a0 = 0; a0 < t; a0++) {
     int n = in.nextInt();
     int treeHeight = getHeightOfTree(n);
     System.out.println(treeHeight);
   }
 }
Exemplo n.º 9
0
 public static void main(String[] args) {
   Scanner sc = new Scanner(System.in);
   int k = sc.nextInt();
   while (k > 0) {
     int a = sc.nextInt();
     int b = sc.nextInt();
     int n = sc.nextInt();
     printer(a, b, n);
     k--;
   }
 }
Exemplo n.º 10
0
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int N = in.nextInt();
    int arr[] = new int[N];
    for (int i = 0; i < N; i++) {
      arr[i] = in.nextInt();
    }

    for (int i = N - 1; i >= 0; i--) {
      System.out.print(arr[i] + " ");
    }
  }
Exemplo n.º 11
0
  public static void main(String[] args) {
    BigInteger sum = BigInteger.ZERO;
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    int arr[] = new int[n];
    for (int arr_i = 0; arr_i < n; arr_i++) {
      arr[arr_i] = in.nextInt();

      sum = sum.add(BigInteger.valueOf(arr[arr_i]));
    }
    System.out.println(sum);
  }
Exemplo n.º 12
0
  public static Matrix scanMatrix() { // 输入矩阵
    int i, j;
    Matrix matrix = new Matrix();
    Scanner scanner = new Scanner(System.in);

    matrix.setLine(scanner.nextInt());
    matrix.setRow(scanner.nextInt());
    matrix.setAccuracy(scanner.nextDouble());
    for (i = 0; i < matrix.getLine(); i++) {
      for (j = 0; j < matrix.getRow(); j++) matrix.setElement(i, j, scanner.nextDouble());
    }
    return matrix;
  }
 public static void main(String[] args) {
   Scanner in = new Scanner(System.in);
   int T;
   T = in.nextInt();
   while ((T--) > 0) {
     int n = in.nextInt(), k = in.nextInt();
     int value[] = new int[n];
     for (int i = 0; i < n; i++) {
       value[i] = in.nextInt();
     }
     System.out.println(UKS(value, n, k));
   }
   in.close();
 }
Exemplo n.º 14
0
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    int[] a = new int[N];
    for (int i = 0; i < N; i++) {
      a[i] = sc.nextInt();
    }

    Difference D = new Difference(a);

    D.computeDifference();

    System.out.print(D.maximumDifference);
  }
Exemplo n.º 15
0
  public static void main(String[] args) {
    long start = System.currentTimeMillis();
    Scanner input = new Scanner(System.in);
    int numberOfTestCases = input.nextInt();
    ArrayList<Integer> order = new ArrayList<Integer>(numberOfTestCases);
    int previousKey = -1;
    int previousValue = 0;
    int cycleNumber = 0;

    Map<Integer, Integer> testCases = new TreeMap<Integer, Integer>();

    for (int i = 0; i < numberOfTestCases; i++) {
      int numberOfCycles = input.nextInt();
      testCases.put(numberOfCycles, 1);
      order.add(numberOfCycles);
    }

    for (Map.Entry<Integer, Integer> entry : testCases.entrySet()) {
      int numberOfCycles;
      int initialHeight;

      if (previousKey == -1) {
        numberOfCycles = entry.getKey();
        initialHeight = entry.getValue();
      } else {
        numberOfCycles = entry.getKey() - previousKey;
        initialHeight = previousValue;
      }

      for (int i = 0; i < numberOfCycles; i++) {
        if (cycleNumber % 2 == 0) {
          initialHeight *= 2;
        } else {
          initialHeight += 1;
        }
        cycleNumber++;
      }

      entry.setValue(initialHeight);
      previousKey = entry.getKey();
      previousValue = initialHeight;
    }

    for (Integer element : order) {
      System.out.println(testCases.get(element));
    }

    long elapsed = System.currentTimeMillis() - start;
    System.out.println("time: " + elapsed);
  }
Exemplo n.º 16
0
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    int k = in.nextInt();
    int a[] = new int[n];
    for (int a_i = 0; a_i < n; a_i++) {
      a[a_i] = in.nextInt();
    }

    int[] output = new int[n];
    output = arrayLeftRotation(a, n, k);
    for (int i = 0; i < n; i++) System.out.print(output[i] + " ");

    System.out.println();
  }
 public Solution() {
   Scanner scan = new Scanner(System.in);
   n = scan.nextInt();
   m = scan.nextInt();
   roads = new ArrayList<Road>[n];
   for (int i = 0; i < n; i++) {
     roads[i] = new ArrayList<Road>();
   }
   for (int i = 0; i < m; i++) {
     int start = scan.nextInt() - 1;
     int end = scan.nextInt() - 1;
     int condition = scan.nextInt();
     roads[start].add(new Road(end, condition == 1));
   }
 }
 public static void main(String[] args) {
   Scanner inp = new Scanner(System.in);
   int n = inp.nextInt();
   String[] store = new String[n];
   for (int i = 0; i < n; i++) store[i] = inp.next();
   int[] cnt = new int[n];
   Arrays.fill(cnt, 0);
   String str = inp.next();
   for (int j = 0; j < n; j++) {
     int l1 = store[j].length();
     for (int k = 0; k <= (str.length() - l1); k++) {
       if (str.substring(k, k + l1).equals(store[j])) {
         cnt[j] = cnt[j] + 1;
       }
     }
   }
   int y = 0;
   for (int m = 0; m < n; m++) {
     y = Math.max(y, cnt[m]);
   }
   System.out.println(y);
   for (int h = 0; h < n; h++) {
     if (cnt[h] == y) System.out.println(store[h]);
   }
 }
Exemplo n.º 19
0
 private static void displayPhoneBooks(ArrayList<String> phoneBooks)
     throws IOException, ParseException {
   if (phoneBooks.size() == 0) {
     System.out.println("\nNO PHONEBOOK AVAILABLE.CREATE A NEW PHONEBOOK.\n");
     return;
   } else {
     System.out.println("FOLLOWING ARE THE AVAILABLE PHONEBOOKS.");
     System.out.println("----------------------------------------\n");
     Scanner sc = new Scanner(System.in);
     for (int i = 0; i < phoneBooks.size(); i++) {
       String s = phoneBooks.get(i);
       System.out.println(s + "\t->PRESS " + (i + 1) + " LOAD THIS.");
     }
     System.out.println("TO GO BACK TO THE PREVIOUS MENU->PRESS " + (phoneBooks.size() + 1));
     int ch = 0;
     while (ch < 1 || ch > (phoneBooks.size() + 1)) {
       while (!sc.hasNextInt()) {
         System.out.println("\nONLY NUMBERS LISTED ABOVE SHOULD BE ENTERED..RETRY..");
         sc.nextLine();
       }
       ch = sc.nextInt();
       if (ch < 1 || ch > phoneBooks.size())
         System.out.println("\nONLY NUMBERS LISTED ABOVE SHOULD BE ENTERED..RETRY..");
     }
     if (ch == (phoneBooks.size() + 1)) return;
     contactBookOperations(phoneBooks.get(ch - 1) + ".txt");
   }
 }
Exemplo n.º 20
0
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int arr[][] = new int[6][6];
    int max = -10000; // lowest possible value is -9*6=-54
    int sum;
    for (int arr_i = 0; arr_i < 6; arr_i++) {
      for (int arr_j = 0; arr_j < 6; arr_j++) {
        arr[arr_i][arr_j] = in.nextInt();
      }
    }

    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 4; j++) {
        sum =
            arr[i][j]
                + arr[i][j + 1]
                + arr[i][j + 2]
                + arr[i + 1][j + 1]
                + arr[i + 2][j]
                + arr[i + 2][j + 1]
                + arr[i + 2][j + 2];
        max = Math.max(sum, max);
      }
    }
    System.out.println(max);
  }
Exemplo n.º 21
0
  static void solve() {
    for (int i = 0; i < MAX; i++) choose[i][0] = choose[i][i] = 1;
    for (int i = 1; i < MAX; i++)
      for (int j = 1; j < i; j++) choose[i][j] = (choose[i - 1][j - 1] + choose[i - 1][j]) % MOD;
    for (int i = 0; i < MAX; i++) Arrays.fill(memo[i], -1);

    Scanner scan = new Scanner(in);
    int T = scan.nextInt();
    for (int t = 1; t <= T; t++) {
      out.print("Case #" + t + ": ");
      int n = scan.nextInt();
      int ways = 0;
      for (int i = 1; i < n; i++) ways = (ways + go(i, n)) % MOD;
      out.println(ways);
    }
  }
 public void init() {
   Scanner scan = new Scanner(System.in);
   count = scan.nextInt();
   x0 = scan.nextLong();
   y0 = scan.nextLong();
   int result = 0;
   boolean special = false;
   for (int i = 0; i < count; i++) {
     long tempx = scan.nextLong();
     long tempy = scan.nextLong();
     if (tempx == x0 && tempy == y0) {
       special = true;
       continue;
     }
     boolean isDuplicate = false;
     for (int j = 0; j < result; j++) {
       long x1 = xList.get(j);
       long y1 = yList.get(j);
       if ((x1 - x0) * (tempy - y0) == (y1 - y0) * (tempx - x0)) {
         isDuplicate = true;
         break;
       }
     }
     if (!isDuplicate) {
       xList.add(tempx);
       yList.add(tempy);
       result++;
     }
   }
   if (special && result == 0) result = 1;
   System.out.println(result);
   scan.close();
 }
  public static void main(String[] args) {
    int i = 4;
    double d = 4.0;
    String s = "HackerRank ";

    Scanner scan = new Scanner(System.in);

    /* Declare second integer, double, and String variables. */
    int j;
    double e;
    String t;
    /* Read and save an integer, double, and String to your variables.*/
    j = scan.nextInt();
    e = scan.nextDouble();
    scan.nextLine();
    t = scan.nextLine();
    /* Print the sum of both integer variables on a new line. */
    System.out.println(i + j);
    /* Print the sum of the double variables on a new line. */
    System.out.println(d + e);
    /* Concatenate and print the String variables on a new line;
    the 's' variable above should be printed first. */
    System.out.println(s + t);

    scan.close();
  }
  public void init() {
    Scanner scan = new Scanner(System.in);
    n = scan.nextInt();
    m = scan.nextInt();
    for (int i = 0; i < n; i++) {
      String input = scan.next();
      record.add(input);
    }

    for (int i = 0; i < m; i++) {
      String input = scan.next();
      char[] charArray = input.toCharArray();
      boolean isValid = false;
      for (int j = 0; j < charArray.length; j++) {
        char c = charArray[j];
        for (char d = 'a'; d <= 'c'; d = (char) (d + 1)) {
          if (d == c) continue;
          charArray[j] = d;
          if (record.contains(new String(charArray))) {
            isValid = true;
            break;
          }
        }
        charArray[j] = c;
        if (isValid) break;
      }
      if (isValid) System.out.println("YES");
      else System.out.println("NO");
    }
    scan.close();
  }
Exemplo n.º 25
0
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int arr[][] = new int[6][6];
    for (int i = 0; i < 6; i++) {
      for (int j = 0; j < 6; j++) {
        arr[i][j] = in.nextInt();
      }
    }

    List sum = new ArrayList();
    for (int i = 1; i < 5; i++) {
      for (int j = 1; j < 5; j++) {
        int temp =
            arr[i][j]
                + arr[i - 1][j - 1]
                + arr[i - 1][j]
                + arr[i - 1][j + 1]
                + arr[i + 1][j - 1]
                + arr[i + 1][j]
                + arr[i + 1][j + 1];
        sum.add(temp);
      }
    }

    System.out.println(Collections.max(sum));
  }
Exemplo n.º 26
0
 public static void main(String[] args) {
   Scanner in = new Scanner(System.in);
   int t = in.nextInt();
   for (int a0 = 0; a0 < t; a0++) {
     int n = in.nextInt();
     int k = in.nextInt();
     int c = 0;
     int a[] = new int[n];
     for (int a_i = 0; a_i < n; a_i++) {
       a[a_i] = in.nextInt();
       if (a[a_i] <= 0) c++;
     }
     if (c < k) System.out.println("YES");
     else System.out.println("NO");
   }
 }
Exemplo n.º 27
0
  public static void main(String[] args) throws IOException {
    Scanner scanner = new Scanner(new BufferedInputStream(System.in));

    int num[][] = {
      {0, 2, 3, 5, 6, 7, 8, 9},
      {2, 3, 4, 5, 6, 8, 9},
      {0, 2, 3, 5, 6, 8, 9},
      {0, 4, 5, 6, 8, 9},
      {0, 1, 2, 3, 4, 7, 8, 9},
      {0, 2, 6, 8},
      {0, 1, 3, 4, 5, 6, 7, 8, 9}
    };

    while (scanner.hasNextInt()) {
      int a = scanner.nextInt();
      String b = scanner.next();
      if (a == 0) break;
      String data[][] = new String[5 + (a - 1) * 2][(3 + (a - 1)) * b.length()];

      for (int i = 0; i < 5 + (a - 1) * 2; i++)
        for (int j = 0; j < (3 + (a - 1)) * b.length(); j++) data[i][j] = " ";

      for (int i = 0; i < b.length(); i++) {
        for (int j = 0; j < 3; j++) {
          for (int k = 0; k < num[j].length; k++) {
            if ((int) b.charAt(i) - 48 == num[j][k]) {
              for (int h = 0; h < a; h++) data[(a + 1) * j][1 + i * (a + 2) + h] = "-";
            }
          }
        }
        for (int j = 3; j < 5; j++) {
          for (int k = 0; k < num[j].length; k++) {
            if ((int) b.charAt(i) - 48 == num[j][k]) {
              for (int h = 0; h < a; h++) data[1 + h][(j - 3) * (a + 1) + (i * (a + 2))] = "|";
            }
          }
        }
        for (int j = 5; j < 7; j++) {
          for (int k = 0; k < num[j].length; k++) {
            if ((int) b.charAt(i) - 48 == num[j][k]) {
              for (int h = 0; h < a; h++) data[a + 2 + h][(j - 5) * (a + 1) + (i * (a + 2))] = "|";
            }
          }
        }
      }
      StringBuffer sb = new StringBuffer("");
      for (int i = 0; i < 5 + (a - 1) * 2; i++) {
        for (int j = 0; j < (3 + (a - 1)) * b.length(); j++) {
          if ((j - (a + 1)) % (a + 2) == 0 && (j + 1) != (3 + (a - 1)) * b.length())
            sb.append(data[i][j] + " ");
          else sb.append(data[i][j]);
        }
        sb.append("\n");
      }
      System.out.println(sb);
    }
  }
Exemplo n.º 28
0
 public static void main(String[] args) {
   Scanner in = new Scanner(System.in);
   int m;
   m = in.nextInt();
   String grid[] = new String[m];
   for (int i = 0; i < m; i++) {
     grid[i] = in.next();
   }
   displayPathtoPrincess(m, grid);
 }
 public static void main(String[] args) {
   Scanner in = new Scanner(System.in);
   int t = in.nextInt();
   for (int a0 = 0; a0 < t; a0++) {
     int R = in.nextInt();
     int C = in.nextInt();
     String G[] = new String[R];
     for (int G_i = 0; G_i < R; G_i++) {
       G[G_i] = in.next();
     }
     int r = in.nextInt();
     int c = in.nextInt();
     String P[] = new String[r];
     for (int P_i = 0; P_i < r; P_i++) {
       P[P_i] = in.next();
     }
     // Check Grid
     checkGrid(G, P);
   }
 }
 public static void main(String[] args) {
   Scanner sc = new Scanner(System.in);
   int a = sc.nextInt();
   if (a == 2) {
     System.out.println("Bob wins, by winning 2 game(s) and tying 1 game(s)");
     System.out.println("Alice and Bob tie, each winning 0 game(s) and tying 1 game(s)");
   } else {
     System.out.println("Alice wins, by winning 2 game(s) and tying 1 game(s)");
     System.out.println("Alice and Bob tie, each winning 0 game(s) and tying 1 game(s)");
   }
 }