From 973d49482d56efd707c93f6318e41f569feb8a81 Mon Sep 17 00:00:00 2001 From: ahmedayyad1410 Date: Wed, 3 Apr 2024 12:02:24 +0200 Subject: [PATCH 1/3] Create 1888-minimum-number-of-flips-to-make-the-binary-string-alternating.java --- ...to-make-the-binary-string-alternating.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 java/1888-minimum-number-of-flips-to-make-the-binary-string-alternating.java 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 From b8259c625b783cdd446608628a6c0817bc206aee Mon Sep 17 00:00:00 2001 From: ahmedayyad1410 Date: Wed, 3 Apr 2024 12:43:45 +0200 Subject: [PATCH 2/3] Create 1498-number-of-subsequences-that-satisfy-the-given-sum-condition.java --- .problemSiteData.json | 3 +- ...-that-satisfy-the-given-sum-condition.java | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 java/1498-number-of-subsequences-that-satisfy-the-given-sum-condition.java diff --git a/.problemSiteData.json b/.problemSiteData.json index 129e689f9..a60bc5297 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", 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 From 26ddd68dac59735a5edc487da9af1c466294da48 Mon Sep 17 00:00:00 2001 From: ahmedayyad1410 Date: Wed, 3 Apr 2024 12:48:00 +0200 Subject: [PATCH 3/3] assign java for 1888-minimum-number-of-flips-to-make-the-binary-string-alternating Create 1498-number-of-subsequences-that-satisfy-the-given-sum-condition.java --- .problemSiteData.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.problemSiteData.json b/.problemSiteData.json index a60bc5297..883726dd2 100644 --- a/.problemSiteData.json +++ b/.problemSiteData.json @@ -1517,7 +1517,8 @@ "javascript":true, "typescript":true, "kotlin":true, - "python":true + "python":true, + "java": true }, { "problem":"Minimum Size Subarray Sum",