Fhernd

App.java

Feb 4th, 2026
2,850
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.03 KB | Source Code | 1 0
  1. import java.util.function.Predicate;
  2.  
  3. public class Calculations {
  4.  
  5.     public static void main(String[] args) {
  6.         char[] chars = { 'a', 'b', 'c', 'd' };
  7.         // looking for bba
  8.         calculate(chars, 3, i -> i[0] == 1 && i[1] == 1 && i[2] == 0);
  9.     }
  10.  
  11.     static void calculate(char[] a, int k, Predicate<int[]> decider) {
  12.         int n = a.length;
  13.  
  14.         if (k < 1 || k > n)
  15.             throw new IllegalArgumentException("Forbidden");
  16.  
  17.         int[] indexes = new int[n];
  18.         int total = (int) Math.pow(n, k);
  19.  
  20.         while (total-- > 0) {
  21.  
  22.             for (int i = 0; i < n - (n - k); i++)
  23.                 System.out.print(a[indexes[i]]);
  24.             System.out.println();
  25.  
  26.             if (decider.test(indexes))
  27.                 break;
  28.  
  29.             for (int i = 0; i < n; i++) {
  30.                 if (indexes[i] >= n - 1) {
  31.                     indexes[i] = 0;
  32.                 } else {
  33.                     indexes[i]++;
  34.                     break;
  35.                 }
  36.             }
  37.         }
  38.     }
  39. }
  40.  
Advertisement