[Java] Slide window with serial array
Use slide window with at most k.\n\nSerial array to count distinct subarrays: [1, 2, 3] ==> "1 2 3 "\n\nTime: O(N^2)\nSpace: O(N^2)\n\n\nclass Solution {\n  pub
Use slide window with at most k.\n\nSerial array to count distinct subarrays: [1, 2, 3] ==> "1 2 3 "\n\nTime: O(N^2)\nSpace: O(N^2)\n\n```\nclass Solution {\n  public int countDistinct(int[] nums, int k, int p) {\n    final int n = nums.length;\n    Set<String> candidate = new HashSet<>();\n    int count = 0;\n    for (int i = 0, j = 0; i < n; i++) {\n      if (nums[i] % p == 0) {\n        count++;\n      }\n      if (count > k) {\n        // window is full, so move left side\n        while (nums[j] % p != 0) {\n          j++;\n        }\n\n        j++;\n        count--;\n      }\n\n      // all subarray among [i, j] is valid\n      for (int m = j; m <= i; m++) {\n        StringBuilder sb = new StringBuilder();\n        for (int l = m; l <= i; l++) {\n          sb.append(nums[l]).append(\' \');\n        }\n        // use set to distinct\n        candidate.add(sb.toString());\n      }\n    }\n\n    return candidate.size();\n  }\n}\n```\n
['Sliding Window', 'Java']