diff --git a/src/main/kotlin/g3701_3800/s3731_find_missing_elements/Solution.kt b/src/main/kotlin/g3701_3800/s3731_find_missing_elements/Solution.kt new file mode 100644 index 00000000..799091fe --- /dev/null +++ b/src/main/kotlin/g3701_3800/s3731_find_missing_elements/Solution.kt @@ -0,0 +1,29 @@ +package g3701_3800.s3731_find_missing_elements + +// #Easy #Array #Hash_Table #Sorting #Weekly_Contest_474 +// #2025_11_05_Time_2_ms_(100.00%)_Space_46.48_MB_(84.44%) + +class Solution { + fun findMissingElements(nums: IntArray): List { + var maxi = 0 + var mini = 101 + val list: MutableList = ArrayList() + val array = BooleanArray(101) + for (num in nums) { + array[num] = true + if (maxi < num) { + maxi = num + } + if (mini > num) { + mini = num + } + } + for (index in mini + 1..= a) { + b = a + a = ax + } else if (ax > b) { + b = ax + } + } + return 100000L * a * b + } +} diff --git a/src/main/kotlin/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/readme.md b/src/main/kotlin/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/readme.md new file mode 100644 index 00000000..48ad50d9 --- /dev/null +++ b/src/main/kotlin/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/readme.md @@ -0,0 +1,51 @@ +3732\. Maximum Product of Three Elements After One Replacement + +Medium + +You are given an integer array `nums`. + +You **must** replace **exactly one** element in the array with **any** integer value in the range [-105, 105] (inclusive). + +After performing this single replacement, determine the **maximum possible product** of **any three** elements at **distinct indices** from the modified array. + +Return an integer denoting the **maximum product** achievable. + +**Example 1:** + +**Input:** nums = [-5,7,0] + +**Output:** 3500000 + +**Explanation:** + +Replacing 0 with -105 gives the array [-5, 7, -105], which has a product (-5) * 7 * (-105) = 3500000. The maximum product is 3500000. + +**Example 2:** + +**Input:** nums = [-4,-2,-1,-3] + +**Output:** 1200000 + +**Explanation:** + +Two ways to achieve the maximum product include: + +* `[-4, -2, -3]` → replace -2 with 105 → product = (-4) * 105 * (-3) = 1200000. +* `[-4, -1, -3]` → replace -1 with 105 → product = (-4) * 105 * (-3) = 1200000. + +The maximum product is 1200000. + +**Example 3:** + +**Input:** nums = [0,10,0] + +**Output:** 0 + +**Explanation:** + +There is no way to replace an element with another integer and not have a 0 in the array. Hence, the product of all three elements will always be 0, and the maximum product is 0. + +**Constraints:** + +* 3 <= nums.length <= 105 +* -105 <= nums[i] <= 105 \ No newline at end of file diff --git a/src/main/kotlin/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/Solution.kt b/src/main/kotlin/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/Solution.kt new file mode 100644 index 00000000..35aa3448 --- /dev/null +++ b/src/main/kotlin/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/Solution.kt @@ -0,0 +1,49 @@ +package g3701_3800.s3733_minimum_time_to_complete_all_deliveries + +// #Medium #Math #Binary_Search #Weekly_Contest_474 +// #2025_11_05_Time_2_ms_(100.00%)_Space_42.47_MB_(100.00%) + +import kotlin.math.max + +class Solution { + private fun pos(k: Long, n1: Long, n2: Long, p1: Int, p2: Int, lVal: Long): Boolean { + val kP1 = k / p1 + val kP2 = k / p2 + val kL = k / lVal + val s1 = kP2 - kL + val s2 = kP1 - kL + val sB = k - kP1 - kP2 + kL + val w1 = max(0L, n1 - s1) + val w2 = max(0L, n2 - s2) + return (w1 + w2) <= sB + } + + private fun findGcd(a: Long, b: Long): Long { + if (b == 0L) { + return a + } + return findGcd(b, a % b) + } + + fun minimumTime(d: IntArray, r: IntArray): Long { + val n1 = d[0].toLong() + val n2 = d[1].toLong() + val p1 = r[0] + val p2 = r[1] + val g = findGcd(p1.toLong(), p2.toLong()) + val l = p1.toLong() * p2 / g + var lo = n1 + n2 + var hi = (n1 + n2) * max(p1, p2) + var res = hi + while (lo <= hi) { + val mid = lo + (hi - lo) / 2 + if (pos(mid, n1, n2, p1, p2, l)) { + res = mid + hi = mid - 1 + } else { + lo = mid + 1 + } + } + return res + } +} diff --git a/src/main/kotlin/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/readme.md b/src/main/kotlin/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/readme.md new file mode 100644 index 00000000..4fbcff72 --- /dev/null +++ b/src/main/kotlin/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/readme.md @@ -0,0 +1,53 @@ +3733\. Minimum Time to Complete All Deliveries + +Medium + +You are given two integer arrays of size 2: d = [d1, d2] and r = [r1, r2]. + +Two delivery drones are tasked with completing a specific number of deliveries. Drone `i` must complete di deliveries. + +Each delivery takes **exactly** one hour and **only one** drone can make a delivery at any given hour. + +Additionally, both drones require recharging at specific intervals during which they cannot make deliveries. Drone `i` must recharge every ri hours (i.e. at hours that are multiples of ri). + +Return an integer denoting the **minimum** total time (in hours) required to complete all deliveries. + +**Example 1:** + +**Input:** d = [3,1], r = [2,3] + +**Output:** 5 + +**Explanation:** + +* The first drone delivers at hours 1, 3, 5 (recharges at hours 2, 4). +* The second drone delivers at hour 2 (recharges at hour 3). + +**Example 2:** + +**Input:** d = [1,3], r = [2,2] + +**Output:** 7 + +**Explanation:** + +* The first drone delivers at hour 3 (recharges at hours 2, 4, 6). +* The second drone delivers at hours 1, 5, 7 (recharges at hours 2, 4, 6). + +**Example 3:** + +**Input:** d = [2,1], r = [3,4] + +**Output:** 3 + +**Explanation:** + +* The first drone delivers at hours 1, 2 (recharges at hour 3). +* The second drone delivers at hour 3. + +**Constraints:** + +* d = [d1, d2] +* 1 <= di <= 109 +* r = [r1, r2] +* 2 <= ri <= 3 * 104 \ No newline at end of file diff --git a/src/main/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.kt b/src/main/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.kt new file mode 100644 index 00000000..2728cdde --- /dev/null +++ b/src/main/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/Solution.kt @@ -0,0 +1,94 @@ +package g3701_3800.s3734_lexicographically_smallest_palindromic_permutation_greater_than_target + +// #Hard #String #Two_Pointers #Enumeration #Weekly_Contest_474 +// #2025_11_05_Time_4_ms_(100.00%)_Space_46.12_MB_(77.78%) + +class Solution { + internal fun func(i: Int, target: String, ans: CharArray, l: Int, r: Int, freq: IntArray, end: Boolean): Boolean { + if (l > r) { + return String(ans).compareTo(target) > 0 + } + if (l == r) { + var left = '#' + for (k in 0 until 26) { + if (freq[k] > 0) { + left = (k + 'a'.code).toChar() + } + } + freq[left.code - 'a'.code]-- + ans[l] = left + if (func(i + 1, target, ans, l + 1, r - 1, freq, end)) { + return true + } + freq[left.code - 'a'.code]++ + ans[l] = '#' + return false + } + if (end) { + for (j in 0 until 26) { + if (freq[j] > 1) { + freq[j] -= 2 + val charJ = (j + 'a'.code).toChar() + ans[l] = charJ + ans[r] = charJ + if (func(i + 1, target, ans, l + 1, r - 1, freq, end)) { + return true + } + ans[l] = '#' + ans[r] = '#' + freq[j] += 2 + } + } + return false + } + val curr = target[i] + var next = '1' + for (k in (curr.code - 'a'.code + 1) until 26) { + if (freq[k] > 1) { + next = (k + 'a'.code).toChar() + break + } + } + if (freq[curr.code - 'a'.code] > 1) { + ans[l] = curr + ans[r] = curr + freq[curr.code - 'a'.code] -= 2 + if (func(i + 1, target, ans, l + 1, r - 1, freq, false)) { // end = false + return true + } + freq[curr.code - 'a'.code] += 2 + } + if (next != '1') { + ans[l] = next + ans[r] = next + freq[next.code - 'a'.code] -= 2 + if (func(i + 1, target, ans, l + 1, r - 1, freq, true)) { // end = true + return true + } + } + ans[l] = '#' + ans[r] = '#' + return false + } + + fun lexPalindromicPermutation(s: String, target: String): String { + val freq = IntArray(26) + for (char in s) { + freq[char.code - 'a'.code]++ + } + var oddc = 0 + for (i in 0 until 26) { + if (freq[i] % 2 == 1) { + oddc++ + } + } + if (oddc > 1) { + return "" + } + val ans = CharArray(s.length) { '#' } + if (func(0, target, ans, 0, s.length - 1, freq, false)) { + return String(ans) + } + return "" + } +} diff --git a/src/main/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/readme.md b/src/main/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/readme.md new file mode 100644 index 00000000..fbf7ec28 --- /dev/null +++ b/src/main/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/readme.md @@ -0,0 +1,55 @@ +3734\. Lexicographically Smallest Palindromic Permutation Greater Than Target + +Hard + +You are given two strings `s` and `target`, each of length `n`, consisting of lowercase English letters. + +Return the **lexicographically smallest string** that is **both** a **palindromic permutation** of `s` and **strictly** greater than `target`. If no such permutation exists, return an empty string. + +**Example 1:** + +**Input:** s = "baba", target = "abba" + +**Output:** "baab" + +**Explanation:** + +* The palindromic permutations of `s` (in lexicographical order) are `"abba"` and `"baab"`. +* The lexicographically smallest permutation that is strictly greater than `target` is `"baab"`. + +**Example 2:** + +**Input:** s = "baba", target = "bbaa" + +**Output:** "" + +**Explanation:** + +* The palindromic permutations of `s` (in lexicographical order) are `"abba"` and `"baab"`. +* None of them is lexicographically strictly greater than `target`. Therefore, the answer is `""`. + +**Example 3:** + +**Input:** s = "abc", target = "abb" + +**Output:** "" + +**Explanation:** + +`s` has no palindromic permutations. Therefore, the answer is `""`. + +**Example 4:** + +**Input:** s = "aac", target = "abb" + +**Output:** "aca" + +**Explanation:** + +* The only palindromic permutation of `s` is `"aca"`. +* `"aca"` is strictly greater than `target`. Therefore, the answer is `"aca"`. + +**Constraints:** + +* `1 <= n == s.length == target.length <= 300` +* `s` and `target` consist of only lowercase English letters. \ No newline at end of file diff --git a/src/test/kotlin/g3701_3800/s3731_find_missing_elements/SolutionTest.kt b/src/test/kotlin/g3701_3800/s3731_find_missing_elements/SolutionTest.kt new file mode 100644 index 00000000..1053f307 --- /dev/null +++ b/src/test/kotlin/g3701_3800/s3731_find_missing_elements/SolutionTest.kt @@ -0,0 +1,31 @@ +package g3701_3800.s3731_find_missing_elements + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun findMissingElements() { + assertThat>( + Solution().findMissingElements(intArrayOf(1, 4, 2, 5)), + equalTo>(mutableListOf(3)), + ) + } + + @Test + fun findMissingElements2() { + assertThat>( + Solution().findMissingElements(intArrayOf(7, 8, 6, 9)), + equalTo>(mutableListOf()), + ) + } + + @Test + fun findMissingElements3() { + assertThat>( + Solution().findMissingElements(intArrayOf(5, 1)), + equalTo>(mutableListOf(2, 3, 4)), + ) + } +} diff --git a/src/test/kotlin/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/SolutionTest.kt b/src/test/kotlin/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/SolutionTest.kt new file mode 100644 index 00000000..7269907e --- /dev/null +++ b/src/test/kotlin/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement/SolutionTest.kt @@ -0,0 +1,28 @@ +package g3701_3800.s3732_maximum_product_of_three_elements_after_one_replacement + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxProduct() { + assertThat( + Solution().maxProduct(intArrayOf(-5, 7, 0)), + equalTo(3500000L), + ) + } + + @Test + fun maxProduct2() { + assertThat( + Solution().maxProduct(intArrayOf(-4, -2, -1, -3)), + equalTo(1200000L), + ) + } + + @Test + fun maxProduct3() { + assertThat(Solution().maxProduct(intArrayOf(0, 10, 0)), equalTo(0L)) + } +} diff --git a/src/test/kotlin/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/SolutionTest.kt b/src/test/kotlin/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/SolutionTest.kt new file mode 100644 index 00000000..ad9dfc07 --- /dev/null +++ b/src/test/kotlin/g3701_3800/s3733_minimum_time_to_complete_all_deliveries/SolutionTest.kt @@ -0,0 +1,31 @@ +package g3701_3800.s3733_minimum_time_to_complete_all_deliveries + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minimumTime() { + assertThat( + Solution().minimumTime(intArrayOf(3, 1), intArrayOf(2, 3)), + equalTo(5L), + ) + } + + @Test + fun minimumTime2() { + assertThat( + Solution().minimumTime(intArrayOf(1, 3), intArrayOf(2, 2)), + equalTo(7L), + ) + } + + @Test + fun minimumTime3() { + assertThat( + Solution().minimumTime(intArrayOf(2, 1), intArrayOf(3, 4)), + equalTo(3L), + ) + } +} diff --git a/src/test/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/SolutionTest.kt b/src/test/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/SolutionTest.kt new file mode 100644 index 00000000..7d52f275 --- /dev/null +++ b/src/test/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target/SolutionTest.kt @@ -0,0 +1,336 @@ +package g3701_3800.s3734_lexicographically_smallest_palindromic_permutation_greater_than_target + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.allOf +import org.hamcrest.Matchers.anyOf +import org.hamcrest.Matchers.containsString +import org.hamcrest.Matchers.greaterThanOrEqualTo +import org.hamcrest.Matchers.hasLength +import org.hamcrest.Matchers.not +import org.hamcrest.Matchers.nullValue +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun lexPalindromicPermutation() { + assertThat( + Solution().lexPalindromicPermutation("baba", "abba"), + equalTo("baab"), + ) + } + + @Test + fun lexPalindromicPermutation2() { + assertThat( + Solution().lexPalindromicPermutation("baba", "bbaa"), + equalTo(""), + ) + } + + @Test + fun lexPalindromicPermutation3() { + assertThat( + Solution().lexPalindromicPermutation("abc", "abb"), + equalTo(""), + ) + } + + @Test + fun lexPalindromicPermutation4() { + assertThat( + Solution().lexPalindromicPermutation("aac", "abb"), + equalTo("aca"), + ) + } + + @Test + fun lexPalindromicPermutation5() { + // Branch: oddc > 1 + val result = Solution().lexPalindromicPermutation("abc", "a") + assertThat(result, equalTo("")) + } + + @Test + fun lexPalindromicPermutation6() { + // Branch: oddc = 1 + val result = Solution().lexPalindromicPermutation("aab", "a") + assertThat( + result, + allOf(not(equalTo("")), hasLength(3)), + ) + } + + @Test + fun lexPalindromicPermutation7() { + // Branch: oddc = 0 + val result = Solution().lexPalindromicPermutation("aabb", "ab") + assertThat(result, not(equalTo(""))) + } + + @Test + fun lexPalindromicPermutation8() { + // Branch: func returns false + val result = Solution().lexPalindromicPermutation("abc", "xyz") + assertThat(result, equalTo("")) + } + + @Test + fun lexPalindromicPermutation9() { + // Edge case: length = 1 + val result = Solution().lexPalindromicPermutation("a", "a") + assertThat(result, equalTo("")) + } + + @Test + fun lexPalindromicPermutation10() { + // Branch: l > r and comparison result > 0 + val target = "a" + val freq = IntArray(26) + val ans = charArrayOf('b', 'b') + + val result = Solution().func(0, target, ans, 1, 0, freq, false) + assertThat(result, equalTo(true)) + } + + @Test + fun lexPalindromicPermutation11() { + // Branch: l > r and comparison result <= 0 + val target = "z" + val freq = IntArray(26) + val ans = charArrayOf('a', 'a') + + val result = Solution().func(0, target, ans, 1, 0, freq, false) + assertThat(result, equalTo(false)) + } + + @Test + fun lexPalindromicPermutation12() { + // Branch: l == r with available character + val target = "a" + val freq = IntArray(26) + // 'a' has frequency 1 + freq[0] = 1 + val ans = CharArray(1) + + val result = Solution().func(0, target, ans, 0, 0, freq, false) + assertThat(result, equalTo(false)) + assertThat(ans[0], equalTo('#')) + } + + @Test + fun lexPalindromicPermutation13() { + // Branch: end = true, finds char with freq > 1 + val target = "ab" + val freq = IntArray(26) + // 'a' can form a pair + freq[0] = 2 + freq[1] = 0 + val ans = CharArray(2) + + val result = Solution().func(1, target, ans, 0, 1, freq, true) + assertThat( + result, + anyOf(equalTo(true), equalTo(false)), + ) + // Frequency restored + assertThat(freq[0], equalTo(2)) + } + + @Test + fun lexPalindromicPermutation14() { + // Branch: end = true, no char has freq > 1 + val target = "ab" + val freq = IntArray(26) + freq[0] = 1 + freq[1] = 1 + val ans = CharArray(2) + + val result = Solution().func(1, target, ans, 0, 1, freq, true) + assertThat(result, equalTo(false)) + } + + @Test + fun lexPalindromicPermutation15() { + // Branch: end = true, tries different pairs + val target = "abc" + val freq = IntArray(26) + freq[0] = 2 + freq[1] = 2 + freq[2] = 2 + val ans = CharArray(3) + + Solution().func(1, target, ans, 0, 2, freq, true) + assertThat(freq[0], equalTo(0)) + assertThat(freq[1], equalTo(2)) + assertThat(freq[2], equalTo(1)) + } + + @Test + fun lexPalindromicPermutation16() { + // Branch: end = false, curr has freq > 1 + val target = "a" + val freq = IntArray(26) + freq[0] = 2 + val ans = CharArray(2) + + Solution().func(0, target, ans, 0, 1, freq, false) + assertThat(freq[0], equalTo(0)) + } + + @Test + fun lexPalindromicPermutation17() { + // Branch: end = false, curr has freq <= 1 + val target = "a" + val freq = IntArray(26) + freq[0] = 0 + val ans = CharArray(2) + + Solution().func(0, target, ans, 0, 1, freq, false) + assertThat(freq[0], equalTo(0)) + } + + @Test + fun lexPalindromicPermutation18() { + // Branch: end = false, finds next char with freq > 1 + val target = "a" + val freq = IntArray(26) + freq[0] = 0 + freq[1] = 2 + val ans = CharArray(2) + + Solution().func(0, target, ans, 0, 1, freq, false) + assertThat(freq[0], equalTo(0)) + assertThat(freq[1], equalTo(0)) + } + + @Test + fun lexPalindromicPermutation19() { + // Branch: end = false, no next char with freq > 1 + val target = "z" + val freq = IntArray(26) + freq[25] = 1 + val ans = CharArray(2) + + val result = Solution().func(0, target, ans, 0, 1, freq, false) + assertThat(result, equalTo(false)) + } + + @Test + fun lexPalindromicPermutation20() { + // Branch: end = false transitions to end = true + val target = "ab" + val freq = IntArray(26) + freq[0] = 2 + freq[1] = 2 + val ans = CharArray(2) + + Solution().func(0, target, ans, 0, 1, freq, false) + + assertThat(freq[0], equalTo(2)) + assertThat(freq[1], equalTo(0)) + } + + @Test + fun lexPalindromicPermutation21() { + // Verify result is always a palindrome + val result = Solution().lexPalindromicPermutation("aabbcc", "abc") + if (!result.isEmpty()) { + val reversed = StringBuilder(result).reverse().toString() + assertThat(result, equalTo(reversed)) + } + } + + @Test + fun lexPalindromicPermutation22() { + // Verify character frequencies are preserved + val input = "aabbcc" + val result = Solution().lexPalindromicPermutation(input, "abc") + + if (!result.isEmpty()) { + val inputFreq = IntArray(26) + val resultFreq = IntArray(26) + + for (c in input.toCharArray()) { + inputFreq[c.code - 'a'.code]++ + } + for (c in result.toCharArray()) { + resultFreq[c.code - 'a'.code]++ + } + + assertThat(resultFreq, equalTo(inputFreq)) + } + } + + @Test + fun lexPalindromicPermutation23() { + // Result length should match input length + val input = "aabbccdd" + val result = Solution().lexPalindromicPermutation(input, "abcd") + + if (!result.isEmpty()) { + assertThat(result.length, equalTo(input.length)) + } + } + + @Test + fun lexPalindromicPermutation24() { + // Result should be >= target in lexicographical order + val result = Solution().lexPalindromicPermutation("aabbcc", "abc") + + if (!result.isEmpty()) { + assertThat(result.compareTo("abc"), greaterThanOrEqualTo(0)) + } + } + + @Test + fun lexPalindromicPermutation25() { + // Complex scenario with multiple characters + val result = Solution().lexPalindromicPermutation("aabbccdd", "abcd") + + assertThat( + result, + anyOf( + equalTo(""), + allOf( + hasLength(8), + containsString("a"), + containsString("b"), + ), + ), + ) + } + + @Test + fun lexPalindromicPermutation26() { + // Edge case: empty string (if applicable) + val result = Solution().lexPalindromicPermutation("", "") + assertThat( + result, + anyOf(equalTo(""), not(nullValue())), + ) + } + + @Test + fun lexPalindromicPermutation27() { + // Verify frequency array is restored after recursion + val target = "aabb" + val freq = IntArray(26) + val freqCopy: IntArray = freq.clone() + + val ans = CharArray(4) + Solution().func(0, target, ans, 0, 3, freq, false) + + assertThat(freq, equalTo(freqCopy)) + } + + @Test + fun lexPalindromicPermutation28() { + // Verify char array is properly initialized + val result = Solution().lexPalindromicPermutation("aa", "a") + + if (!result.isEmpty()) { + assertThat(result, not(containsString("#"))) + } + } +}