Skip to content

Commit d8ebc44

Browse files
authored
Added tasks 3731-3734
1 parent eae5dc7 commit d8ebc44

File tree

12 files changed

+826
-0
lines changed

12 files changed

+826
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g3701_3800.s3731_find_missing_elements
2+
3+
// #Easy #Array #Hash_Table #Sorting #Weekly_Contest_474
4+
// #2025_11_05_Time_2_ms_(100.00%)_Space_46.48_MB_(84.44%)
5+
6+
class Solution {
7+
fun findMissingElements(nums: IntArray): List<Int> {
8+
var maxi = 0
9+
var mini = 101
10+
val list: MutableList<Int> = ArrayList()
11+
val array = BooleanArray(101)
12+
for (num in nums) {
13+
array[num] = true
14+
if (maxi < num) {
15+
maxi = num
16+
}
17+
if (mini > num) {
18+
mini = num
19+
}
20+
}
21+
for (index in mini + 1..<maxi) {
22+
if (array[index]) {
23+
continue
24+
}
25+
list.add(index)
26+
}
27+
return list
28+
}
29+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
3731\. Find Missing Elements
2+
3+
Easy
4+
5+
You are given an integer array `nums` consisting of **unique** integers.
6+
7+
Originally, `nums` contained **every integer** within a certain range. However, some integers might have gone **missing** from the array.
8+
9+
The **smallest** and **largest** integers of the original range are still present in `nums`.
10+
11+
Return a **sorted** list of all the missing integers in this range. If no integers are missing, return an **empty** list.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,4,2,5]
16+
17+
**Output:** [3]
18+
19+
**Explanation:**
20+
21+
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.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [7,8,6,9]
26+
27+
**Output:** []
28+
29+
**Explanation:**
30+
31+
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.
32+
33+
**Example 3:**
34+
35+
**Input:** nums = [5,1]
36+
37+
**Output:** [2,3,4]
38+
39+
**Explanation:**
40+
41+
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.
42+
43+
**Constraints:**
44+
45+
* `2 <= nums.length <= 100`
46+
* `1 <= nums[i] <= 100`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3701_3800.s3732_maximum_product_of_three_elements_after_one_replacement
2+
3+
// #Medium #Array #Math #Sorting #Greedy #Weekly_Contest_474
4+
// #2025_11_05_Time_6_ms_(88.00%)_Space_74.51_MB_(84.00%)
5+
6+
import kotlin.math.abs
7+
8+
class Solution {
9+
fun maxProduct(nums: IntArray): Long {
10+
var a: Long = 0
11+
var b: Long = 0
12+
for (x in nums) {
13+
val ax = abs(x).toLong()
14+
if (ax >= a) {
15+
b = a
16+
a = ax
17+
} else if (ax > b) {
18+
b = ax
19+
}
20+
}
21+
return 100000L * a * b
22+
}
23+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
3732\. Maximum Product of Three Elements After One Replacement
2+
3+
Medium
4+
5+
You are given an integer array `nums`.
6+
7+
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).
8+
9+
After performing this single replacement, determine the **maximum possible product** of **any three** elements at **distinct indices** from the modified array.
10+
11+
Return an integer denoting the **maximum product** achievable.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [-5,7,0]
16+
17+
**Output:** 3500000
18+
19+
**Explanation:**
20+
21+
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.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [-4,-2,-1,-3]
26+
27+
**Output:** 1200000
28+
29+
**Explanation:**
30+
31+
Two ways to achieve the maximum product include:
32+
33+
* `[-4, -2, -3]` → replace -2 with 10<sup>5</sup> → product = <code>(-4) * 10<sup>5</sup> * (-3) = 1200000</code>.
34+
* `[-4, -1, -3]` → replace -1 with 10<sup>5</sup> → product = <code>(-4) * 10<sup>5</sup> * (-3) = 1200000</code>.
35+
36+
The maximum product is 1200000.
37+
38+
**Example 3:**
39+
40+
**Input:** nums = [0,10,0]
41+
42+
**Output:** 0
43+
44+
**Explanation:**
45+
46+
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.
47+
48+
**Constraints:**
49+
50+
* <code>3 <= nums.length <= 10<sup>5</sup></code>
51+
* <code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package g3701_3800.s3733_minimum_time_to_complete_all_deliveries
2+
3+
// #Medium #Math #Binary_Search #Weekly_Contest_474
4+
// #2025_11_05_Time_2_ms_(100.00%)_Space_42.47_MB_(100.00%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
private fun pos(k: Long, n1: Long, n2: Long, p1: Int, p2: Int, lVal: Long): Boolean {
10+
val kP1 = k / p1
11+
val kP2 = k / p2
12+
val kL = k / lVal
13+
val s1 = kP2 - kL
14+
val s2 = kP1 - kL
15+
val sB = k - kP1 - kP2 + kL
16+
val w1 = max(0L, n1 - s1)
17+
val w2 = max(0L, n2 - s2)
18+
return (w1 + w2) <= sB
19+
}
20+
21+
private fun findGcd(a: Long, b: Long): Long {
22+
if (b == 0L) {
23+
return a
24+
}
25+
return findGcd(b, a % b)
26+
}
27+
28+
fun minimumTime(d: IntArray, r: IntArray): Long {
29+
val n1 = d[0].toLong()
30+
val n2 = d[1].toLong()
31+
val p1 = r[0]
32+
val p2 = r[1]
33+
val g = findGcd(p1.toLong(), p2.toLong())
34+
val l = p1.toLong() * p2 / g
35+
var lo = n1 + n2
36+
var hi = (n1 + n2) * max(p1, p2)
37+
var res = hi
38+
while (lo <= hi) {
39+
val mid = lo + (hi - lo) / 2
40+
if (pos(mid, n1, n2, p1, p2, l)) {
41+
res = mid
42+
hi = mid - 1
43+
} else {
44+
lo = mid + 1
45+
}
46+
}
47+
return res
48+
}
49+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
3733\. Minimum Time to Complete All Deliveries
2+
3+
Medium
4+
5+
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>.
6+
7+
Two delivery drones are tasked with completing a specific number of deliveries. Drone `i` must complete <code>d<sub>i</sub></code> deliveries.
8+
9+
Each delivery takes **exactly** one hour and **only one** drone can make a delivery at any given hour.
10+
11+
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>).
12+
13+
Return an integer denoting the **minimum** total time (in hours) required to complete all deliveries.
14+
15+
**Example 1:**
16+
17+
**Input:** d = [3,1], r = [2,3]
18+
19+
**Output:** 5
20+
21+
**Explanation:**
22+
23+
* The first drone delivers at hours 1, 3, 5 (recharges at hours 2, 4).
24+
* The second drone delivers at hour 2 (recharges at hour 3).
25+
26+
**Example 2:**
27+
28+
**Input:** d = [1,3], r = [2,2]
29+
30+
**Output:** 7
31+
32+
**Explanation:**
33+
34+
* The first drone delivers at hour 3 (recharges at hours 2, 4, 6).
35+
* The second drone delivers at hours 1, 5, 7 (recharges at hours 2, 4, 6).
36+
37+
**Example 3:**
38+
39+
**Input:** d = [2,1], r = [3,4]
40+
41+
**Output:** 3
42+
43+
**Explanation:**
44+
45+
* The first drone delivers at hours 1, 2 (recharges at hour 3).
46+
* The second drone delivers at hour 3.
47+
48+
**Constraints:**
49+
50+
* <code>d = [d<sub>1</sub>, d<sub>2</sub>]</code>
51+
* <code>1 <= d<sub>i</sub> <= 10<sup>9</sup></code>
52+
* <code>r = [r<sub>1</sub>, r<sub>2</sub>]</code>
53+
* <code>2 <= r<sub>i</sub> <= 3 * 10<sup>4</sup></code>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package g3701_3800.s3734_lexicographically_smallest_palindromic_permutation_greater_than_target
2+
3+
// #Hard #String #Two_Pointers #Enumeration #Weekly_Contest_474
4+
// #2025_11_05_Time_4_ms_(100.00%)_Space_46.12_MB_(77.78%)
5+
6+
class Solution {
7+
internal fun func(i: Int, target: String, ans: CharArray, l: Int, r: Int, freq: IntArray, end: Boolean): Boolean {
8+
if (l > r) {
9+
return String(ans).compareTo(target) > 0
10+
}
11+
if (l == r) {
12+
var left = '#'
13+
for (k in 0 until 26) {
14+
if (freq[k] > 0) {
15+
left = (k + 'a'.code).toChar()
16+
}
17+
}
18+
freq[left.code - 'a'.code]--
19+
ans[l] = left
20+
if (func(i + 1, target, ans, l + 1, r - 1, freq, end)) {
21+
return true
22+
}
23+
freq[left.code - 'a'.code]++
24+
ans[l] = '#'
25+
return false
26+
}
27+
if (end) {
28+
for (j in 0 until 26) {
29+
if (freq[j] > 1) {
30+
freq[j] -= 2
31+
val charJ = (j + 'a'.code).toChar()
32+
ans[l] = charJ
33+
ans[r] = charJ
34+
if (func(i + 1, target, ans, l + 1, r - 1, freq, end)) {
35+
return true
36+
}
37+
ans[l] = '#'
38+
ans[r] = '#'
39+
freq[j] += 2
40+
}
41+
}
42+
return false
43+
}
44+
val curr = target[i]
45+
var next = '1'
46+
for (k in (curr.code - 'a'.code + 1) until 26) {
47+
if (freq[k] > 1) {
48+
next = (k + 'a'.code).toChar()
49+
break
50+
}
51+
}
52+
if (freq[curr.code - 'a'.code] > 1) {
53+
ans[l] = curr
54+
ans[r] = curr
55+
freq[curr.code - 'a'.code] -= 2
56+
if (func(i + 1, target, ans, l + 1, r - 1, freq, false)) { // end = false
57+
return true
58+
}
59+
freq[curr.code - 'a'.code] += 2
60+
}
61+
if (next != '1') {
62+
ans[l] = next
63+
ans[r] = next
64+
freq[next.code - 'a'.code] -= 2
65+
if (func(i + 1, target, ans, l + 1, r - 1, freq, true)) { // end = true
66+
return true
67+
}
68+
}
69+
ans[l] = '#'
70+
ans[r] = '#'
71+
return false
72+
}
73+
74+
fun lexPalindromicPermutation(s: String, target: String): String {
75+
val freq = IntArray(26)
76+
for (char in s) {
77+
freq[char.code - 'a'.code]++
78+
}
79+
var oddc = 0
80+
for (i in 0 until 26) {
81+
if (freq[i] % 2 == 1) {
82+
oddc++
83+
}
84+
}
85+
if (oddc > 1) {
86+
return ""
87+
}
88+
val ans = CharArray(s.length) { '#' }
89+
if (func(0, target, ans, 0, s.length - 1, freq, false)) {
90+
return String(ans)
91+
}
92+
return ""
93+
}
94+
}

0 commit comments

Comments
 (0)