Skip to content

Commit 64ce679

Browse files
committed
Added tasks 3658-3661
1 parent 14e26bc commit 64ce679

File tree

12 files changed

+535
-0
lines changed

12 files changed

+535
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package g3601_3700.s3658_gcd_of_odd_and_even_sums
2+
3+
// #Easy #Weekly_Contest_464 #2025_08_25_Time_0_ms_(100.00%)_Space_40.78_MB_(100.00%)
4+
5+
class Solution {
6+
fun gcdOfOddEvenSums(n: Int): Int {
7+
return if (n < 0) -n else n
8+
}
9+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
3658\. GCD of Odd and Even Sums
2+
3+
Easy
4+
5+
You are given an integer `n`. Your task is to compute the **GCD** (greatest common divisor) of two values:
6+
7+
* `sumOdd`: the sum of the first `n` odd numbers.
8+
9+
* `sumEven`: the sum of the first `n` even numbers.
10+
11+
12+
Return the GCD of `sumOdd` and `sumEven`.
13+
14+
**Example 1:**
15+
16+
**Input:** n = 4
17+
18+
**Output:** 4
19+
20+
**Explanation:**
21+
22+
* Sum of the first 4 odd numbers `sumOdd = 1 + 3 + 5 + 7 = 16`
23+
* Sum of the first 4 even numbers `sumEven = 2 + 4 + 6 + 8 = 20`
24+
25+
Hence, `GCD(sumOdd, sumEven) = GCD(16, 20) = 4`.
26+
27+
**Example 2:**
28+
29+
**Input:** n = 5
30+
31+
**Output:** 5
32+
33+
**Explanation:**
34+
35+
* Sum of the first 5 odd numbers `sumOdd = 1 + 3 + 5 + 7 + 9 = 25`
36+
* Sum of the first 5 even numbers `sumEven = 2 + 4 + 6 + 8 + 10 = 30`
37+
38+
Hence, `GCD(sumOdd, sumEven) = GCD(25, 30) = 5`.
39+
40+
**Constraints:**
41+
42+
* `1 <= n <= 1000`
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3601_3700.s3659_partition_array_into_k_distinct_groups
2+
3+
// #Medium #Weekly_Contest_464 #2025_08_25_Time_96_ms_(100.00%)_Space_93.60_MB_(100.00%)
4+
5+
class Solution {
6+
fun partitionArray(nums: IntArray, k: Int): Boolean {
7+
val mpp = HashMap<Int, Int>()
8+
for (x in nums) {
9+
mpp.put(x, mpp.getOrDefault(x, 0) + 1)
10+
}
11+
for (count in mpp.values) {
12+
if (count > nums.size / k) {
13+
return false
14+
}
15+
}
16+
return nums.size % k == 0
17+
}
18+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
3659\. Partition Array Into K-Distinct Groups
2+
3+
Medium
4+
5+
You are given an integer array `nums` and an integer `k`.
6+
7+
Your task is to determine whether it is possible to partition all elements of `nums` into one or more groups such that:
8+
9+
* Each group contains **exactly** `k` **distinct** elements.
10+
* Each element in `nums` must be assigned to **exactly** one group.
11+
12+
Return `true` if such a partition is possible, otherwise return `false`.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [1,2,3,4], k = 2
17+
18+
**Output:** true
19+
20+
**Explanation:**
21+
22+
One possible partition is to have 2 groups:
23+
24+
* Group 1: `[1, 2]`
25+
* Group 2: `[3, 4]`
26+
27+
Each group contains `k = 2` distinct elements, and all elements are used exactly once.
28+
29+
**Example 2:**
30+
31+
**Input:** nums = [3,5,2,2], k = 2
32+
33+
**Output:** true
34+
35+
**Explanation:**
36+
37+
One possible partition is to have 2 groups:
38+
39+
* Group 1: `[2, 3]`
40+
* Group 2: `[2, 5]`
41+
42+
Each group contains `k = 2` distinct elements, and all elements are used exactly once.
43+
44+
**Example 3:**
45+
46+
**Input:** nums = [1,5,2,3], k = 3
47+
48+
**Output:** false
49+
50+
**Explanation:**
51+
52+
We cannot form groups of `k = 3` distinct elements using all values exactly once.
53+
54+
**Constraints:**
55+
56+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
57+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
58+
* `1 <= k <= nums.length`
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package g3601_3700.s3660_jump_game_ix
2+
3+
// #Medium #Weekly_Contest_464 #2025_08_25_Time_205_ms_(100.00%)_Space_89.04_MB_(100.00%)
4+
5+
import java.util.ArrayDeque
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun maxValue(nums: IntArray): IntArray {
10+
val n = nums.size
11+
val st = ArrayDeque<Int>()
12+
val uf = UnionFind(n)
13+
for (i in 0..<n) {
14+
var prev = i
15+
if (st.isNotEmpty()) {
16+
prev = st.peek()!!
17+
}
18+
while (!st.isEmpty() && nums[i] < nums[st.peek()!!]) {
19+
uf.union(st.pop(), i)
20+
}
21+
if (nums[i] > nums[prev]) {
22+
st.push(i)
23+
} else {
24+
st.push(prev)
25+
}
26+
}
27+
st.clear()
28+
for (i in n - 1 downTo 0) {
29+
var prev = i
30+
if (!st.isEmpty()) {
31+
prev = st.peek()!!
32+
}
33+
while (!st.isEmpty() && nums[i] > nums[st.peek()!!]) {
34+
uf.union(st.pop(), i)
35+
}
36+
if (nums[i] < nums[prev]) {
37+
st.push(i)
38+
} else {
39+
st.push(prev)
40+
}
41+
}
42+
val map = HashMap<Int?, Int?>()
43+
for (i in 0..<n) {
44+
val root = uf.find(i)
45+
map.put(root, max(map.getOrDefault(root, Int.Companion.MIN_VALUE)!!, nums[i]))
46+
}
47+
val ans = IntArray(n)
48+
for (i in 0..<n) {
49+
ans[i] = map.get(uf.find(i))!!
50+
}
51+
return ans
52+
}
53+
54+
private class UnionFind(n: Int) {
55+
var par: IntArray = IntArray(n)
56+
var rank: IntArray = IntArray(n)
57+
58+
init {
59+
for (i in 0..<n) {
60+
par[i] = i
61+
}
62+
}
63+
64+
fun find(x: Int): Int {
65+
if (par[x] != x) {
66+
par[x] = find(par[x])
67+
}
68+
return par[x]
69+
}
70+
71+
fun union(x: Int, y: Int) {
72+
var x = x
73+
var y = y
74+
x = find(x)
75+
y = find(y)
76+
if (x == y) {
77+
return
78+
}
79+
if (rank[x] < rank[y]) {
80+
par[x] = y
81+
} else if (rank[x] > rank[y]) {
82+
par[y] = x
83+
} else {
84+
par[y] = x
85+
rank[x]++
86+
}
87+
}
88+
}
89+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
3660\. Jump Game IX
2+
3+
Medium
4+
5+
You are given an integer array `nums`.
6+
7+
From any index `i`, you can jump to another index `j` under the following rules:
8+
9+
* Jump to index `j` where `j > i` is allowed only if `nums[j] < nums[i]`.
10+
* Jump to index `j` where `j < i` is allowed only if `nums[j] > nums[i]`.
11+
12+
For each index `i`, find the **maximum** **value** in `nums` that can be reached by following **any** sequence of valid jumps starting at `i`.
13+
14+
Return an array `ans` where `ans[i]` is the **maximum** **value** reachable starting from index `i`.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [2,1,3]
19+
20+
**Output:** [2,2,3]
21+
22+
**Explanation:**
23+
24+
* For `i = 0`: No jump increases the value.
25+
* For `i = 1`: Jump to `j = 0` as `nums[j] = 2` is greater than `nums[i]`.
26+
* For `i = 2`: Since `nums[2] = 3` is the maximum value in `nums`, no jump increases the value.
27+
28+
Thus, `ans = [2, 2, 3]`.
29+
30+
**Example 2:**
31+
32+
**Input:** nums = [2,3,1]
33+
34+
**Output:** [3,3,3]
35+
36+
**Explanation:**
37+
38+
* For `i = 0`: Jump forward to `j = 2` as `nums[j] = 1` is less than `nums[i] = 2`, then from `i = 2` jump to `j = 1` as `nums[j] = 3` is greater than `nums[2]`.
39+
* For `i = 1`: Since `nums[1] = 3` is the maximum value in `nums`, no jump increases the value.
40+
* For `i = 2`: Jump to `j = 1` as `nums[j] = 3` is greater than `nums[2] = 1`.
41+
42+
Thus, `ans = [3, 3, 3]`.
43+
44+
**Constraints:**
45+
46+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
47+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package g3601_3700.s3661_maximum_walls_destroyed_by_robots
2+
3+
// #Hard #Weekly_Contest_464 #2025_08_25_Time_233_ms_(100.00%)_Space_75.24_MB_(100.00%)
4+
5+
import kotlin.math.max
6+
import kotlin.math.min
7+
8+
class Solution {
9+
fun maxWalls(robots: IntArray, distance: IntArray, walls: IntArray): Int {
10+
val n = robots.size
11+
// Pair robots with distances and sort
12+
val rpair = Array<IntArray>(n) { IntArray(2) }
13+
for (i in 0..<n) {
14+
rpair[i][0] = robots[i]
15+
rpair[i][1] = distance[i]
16+
}
17+
rpair.sortWith { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) }
18+
val r = IntArray(n)
19+
val d = IntArray(n)
20+
for (i in 0..<n) {
21+
r[i] = rpair[i][0]
22+
d[i] = rpair[i][1]
23+
}
24+
walls.sort()
25+
// Count walls at robot positions
26+
var base = 0
27+
for (i in 0..<n) {
28+
val idx = walls.binarySearch(r[i])
29+
if (idx >= 0) {
30+
base++
31+
}
32+
}
33+
// Tail walls
34+
val leftTail = countRange(walls, r[0].toLong() - d[0], r[0] - 1L)
35+
val rightTail = countRange(walls, r[n - 1] + 1L, r[n - 1].toLong() + d[n - 1])
36+
// Precompute segment ranges
37+
val segs = n - 1
38+
val max = max(0, segs)
39+
val a = IntArray(max)
40+
val b = IntArray(max)
41+
val c = IntArray(max)
42+
for (i in 0..<segs) {
43+
val segL = r[i] + 1
44+
val segR = r[i + 1] - 1
45+
if (segL > segR) {
46+
c[i] = 0
47+
b[i] = c[i]
48+
a[i] = b[i]
49+
continue
50+
}
51+
val aHigh = min(segR, r[i] + d[i])
52+
a[i] = countRange(walls, segL.toLong(), aHigh.toLong())
53+
val bLow = max(segL, r[i + 1] - d[i + 1])
54+
b[i] = countRange(walls, bLow.toLong(), segR.toLong())
55+
val cLow = max(segL, r[i + 1] - d[i + 1])
56+
val cHigh = min(segR, r[i] + d[i])
57+
c[i] = countRange(walls, cLow.toLong(), cHigh.toLong())
58+
}
59+
val dp = Array<IntArray>(n) { IntArray(2) }
60+
dp[0].fill(Int.Companion.MIN_VALUE / 4)
61+
// first fires left
62+
dp[0][0] = base + leftTail
63+
// first fires right
64+
dp[0][1] = base
65+
for (i in 0..<n - 1) {
66+
dp[i + 1].fill(Int.Companion.MIN_VALUE / 4)
67+
for (choice in 0..1) {
68+
val cur = dp[i][choice]
69+
if (cur < 0) {
70+
continue
71+
}
72+
val addIfNextLeft = if (choice == 1) a[i] + b[i] - c[i] else b[i]
73+
dp[i + 1][0] = max(dp[i + 1][0], cur + addIfNextLeft)
74+
val addIfNextRight = if (choice == 1) a[i] else 0
75+
dp[i + 1][1] = max(dp[i + 1][1], cur + addIfNextRight)
76+
}
77+
}
78+
val res: Int = if (n == 1) {
79+
max(dp[0][0], dp[0][1] + rightTail)
80+
} else {
81+
max(dp[n - 1][0], dp[n - 1][1] + rightTail)
82+
}
83+
return res
84+
}
85+
86+
private fun countRange(arr: IntArray, l: Long, r: Long): Int {
87+
if (l > r || arr.isEmpty()) {
88+
return 0
89+
}
90+
val leftIdx = lowerBound(arr, l)
91+
val rightIdx = upperBound(arr, r)
92+
return max(0, rightIdx - leftIdx)
93+
}
94+
95+
private fun lowerBound(a: IntArray, x: Long): Int {
96+
var l = 0
97+
var r = a.size
98+
while (l < r) {
99+
val m = (l + r) ushr 1
100+
if (a[m] < x) {
101+
l = m + 1
102+
} else {
103+
r = m
104+
}
105+
}
106+
return l
107+
}
108+
109+
private fun upperBound(a: IntArray, x: Long): Int {
110+
var l = 0
111+
var r = a.size
112+
while (l < r) {
113+
val m = (l + r) ushr 1
114+
if (a[m] <= x) {
115+
l = m + 1
116+
} else {
117+
r = m
118+
}
119+
}
120+
return l
121+
}
122+
}

0 commit comments

Comments
 (0)