diff --git a/.problemSiteData.json b/.problemSiteData.json index 129e689f9..883726dd2 100644 --- a/.problemSiteData.json +++ b/.problemSiteData.json @@ -1281,7 +1281,8 @@ "code":"1498-number-of-subsequences-that-satisfy-the-given-sum-condition", "cpp":true, "python":true, - "kotlin":true + "kotlin":true, + "java": true }, { "problem":"Rotate Array", @@ -1516,7 +1517,8 @@ "javascript":true, "typescript":true, "kotlin":true, - "python":true + "python":true, + "java": true }, { "problem":"Minimum Size Subarray Sum", diff --git a/java/1498-number-of-subsequences-that-satisfy-the-given-sum-condition.java b/java/1498-number-of-subsequences-that-satisfy-the-given-sum-condition.java new file mode 100644 index 000000000..c6f5348c1 --- /dev/null +++ b/java/1498-number-of-subsequences-that-satisfy-the-given-sum-condition.java @@ -0,0 +1,30 @@ +class Solution { + public int numSubseq(int[] nums, int target) { + Arrays.sort(nums); + int left = 0, right = nums.length-1; + long result = 0; + long mod = (int) 1e9 + 7; + while(left <= right) { + if(nums[left] + nums[right] > target) { + right--; + } else { + result = (result + fastPower(2, right - left, mod)) % mod; + left++; + } + } + return (int) (result % mod); + } + + long fastPower(int a, int b, long mod) { + long ans = 1; + long base = a; + while (b != 0) { + if (b % 2 == 1) { + ans = (ans * base) % mod; + } + base = (base * base) % mod; + b /= 2; + } + return ans; + } +} \ No newline at end of file diff --git a/java/1888-minimum-number-of-flips-to-make-the-binary-string-alternating.java b/java/1888-minimum-number-of-flips-to-make-the-binary-string-alternating.java new file mode 100644 index 000000000..62e8f7928 --- /dev/null +++ b/java/1888-minimum-number-of-flips-to-make-the-binary-string-alternating.java @@ -0,0 +1,39 @@ +class Solution { + public int minFlips(String s) { + int n = s.length(); + s += s; + + StringBuilder alt1 = new StringBuilder(); + StringBuilder alt2 = new StringBuilder(); + + for (int i = 0; i < s.length(); i++) { + alt1.append(i % 2 == 0 ? '0' : '1'); + alt2.append(i % 2 == 0 ? '1' : '0'); + } + + int res = Integer.MAX_VALUE; + int diff1 = 0, diff2 = 0; + int l = 0; + for (int r = 0; r < s.length(); r++) { + if (s.charAt(r) != alt1.charAt(r)) { + diff1++; + } + if (s.charAt(r) != alt2.charAt(r)) { + diff2++; + } + if ((r - l + 1) > n) { + if (s.charAt(l) != alt1.charAt(l)) { + diff1--; + } + if (s.charAt(l) != alt2.charAt(l)) { + diff2--; + } + l++; + } + if ((r - l + 1) == n) { + res = Math.min(res, Math.min(diff1, diff2)); + } + } + return res; + } +} \ No newline at end of file