Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/main/kotlin/g3701_3800/s3731_find_missing_elements/Solution.kt
Original file line number Diff line number Diff line change
@@ -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<Int> {
var maxi = 0
var mini = 101
val list: MutableList<Int> = 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..<maxi) {
if (array[index]) {
continue
}
list.add(index)
}
return list
}
}
46 changes: 46 additions & 0 deletions src/main/kotlin/g3701_3800/s3731_find_missing_elements/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
3731\. Find Missing Elements

Easy

You are given an integer array `nums` consisting of **unique** integers.

Originally, `nums` contained **every integer** within a certain range. However, some integers might have gone **missing** from the array.

The **smallest** and **largest** integers of the original range are still present in `nums`.

Return a **sorted** list of all the missing integers in this range. If no integers are missing, return an **empty** list.

**Example 1:**

**Input:** nums = [1,4,2,5]

**Output:** [3]

**Explanation:**

The smallest integer is 1 and the largest is 5, so the full range should be `[1,2,3,4,5]`. Among these, only 3 is missing.

**Example 2:**

**Input:** nums = [7,8,6,9]

**Output:** []

**Explanation:**

The smallest integer is 6 and the largest is 9, so the full range is `[6,7,8,9]`. All integers are already present, so no integer is missing.

**Example 3:**

**Input:** nums = [5,1]

**Output:** [2,3,4]

**Explanation:**

The smallest integer is 1 and the largest is 5, so the full range should be `[1,2,3,4,5]`. The missing integers are 2, 3, and 4.

**Constraints:**

* `2 <= nums.length <= 100`
* `1 <= nums[i] <= 100`
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g3701_3800.s3732_maximum_product_of_three_elements_after_one_replacement

// #Medium #Array #Math #Sorting #Greedy #Weekly_Contest_474
// #2025_11_05_Time_6_ms_(88.00%)_Space_74.51_MB_(84.00%)

import kotlin.math.abs

class Solution {
fun maxProduct(nums: IntArray): Long {
var a: Long = 0
var b: Long = 0
for (x in nums) {
val ax = abs(x).toLong()
if (ax >= a) {
b = a
a = ax
} else if (ax > b) {
b = ax
}
}
return 100000L * a * b
}
}
Original file line number Diff line number Diff line change
@@ -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 <code>[-10<sup>5</sup>, 10<sup>5</sup>]</code> (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 -10<sup>5</sup> gives the array <code>[-5, 7, -10<sup>5</sup>]</code>, which has a product <code>(-5) * 7 * (-10<sup>5</sup>) = 3500000</code>. 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 10<sup>5</sup> → product = <code>(-4) * 10<sup>5</sup> * (-3) = 1200000</code>.
* `[-4, -1, -3]` → replace -1 with 10<sup>5</sup> → product = <code>(-4) * 10<sup>5</sup> * (-3) = 1200000</code>.

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:**

* <code>3 <= nums.length <= 10<sup>5</sup></code>
* <code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code>
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
3733\. Minimum Time to Complete All Deliveries

Medium

You are given two integer arrays of size 2: <code>d = [d<sub>1</sub>, d<sub>2</sub>]</code> and <code>r = [r<sub>1</sub>, r<sub>2</sub>]</code>.

Two delivery drones are tasked with completing a specific number of deliveries. Drone `i` must complete <code>d<sub>i</sub></code> 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 <code>r<sub>i</sub></code> hours (i.e. at hours that are multiples of <code>r<sub>i</sub></code>).

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:**

* <code>d = [d<sub>1</sub>, d<sub>2</sub>]</code>
* <code>1 <= d<sub>i</sub> <= 10<sup>9</sup></code>
* <code>r = [r<sub>1</sub>, r<sub>2</sub>]</code>
* <code>2 <= r<sub>i</sub> <= 3 * 10<sup>4</sup></code>
Original file line number Diff line number Diff line change
@@ -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 ""
}
}
Loading