Skip to content

Commit 6e0a483

Browse files
authored
Added tasks 3678-3686
1 parent 3f527d4 commit 6e0a483

File tree

24 files changed

+842
-0
lines changed

24 files changed

+842
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g3601_3700.s3678_smallest_absent_positive_greater_than_average
2+
3+
// #Easy #Biweekly_Contest_165 #2025_09_20_Time_3_ms_(100.00%)_Space_47.84_MB_(100.00%)
4+
5+
class Solution {
6+
fun smallestAbsent(nums: IntArray): Int {
7+
var sum = 0
8+
for (j in nums) {
9+
sum += j
10+
}
11+
val avg = sum.toDouble() / nums.size
12+
var num: Int
13+
if (avg < 0) {
14+
num = 1
15+
} else {
16+
num = avg.toInt() + 1
17+
}
18+
while (true) {
19+
var flag = false
20+
for (j in nums) {
21+
if (num == j) {
22+
flag = true
23+
break
24+
}
25+
}
26+
if (!flag && num > avg) {
27+
return num
28+
}
29+
num++
30+
}
31+
}
32+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
3678\. Smallest Absent Positive Greater Than Average
2+
3+
Easy
4+
5+
You are given an integer array `nums`.
6+
7+
Return the **smallest absent positive** integer in `nums` such that it is **strictly greater** than the **average** of all elements in `nums`.
8+
9+
The **average** of an array is defined as the sum of all its elements divided by the number of elements.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [3,5]
14+
15+
**Output:** 6
16+
17+
**Explanation:**
18+
19+
* The average of `nums` is `(3 + 5) / 2 = 8 / 2 = 4`.
20+
* The smallest absent positive integer greater than 4 is 6.
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [-1,1,2]
25+
26+
**Output:** 3
27+
28+
**Explanation:**
29+
30+
* The average of `nums` is `(-1 + 1 + 2) / 3 = 2 / 3 = 0.667`.
31+
* The smallest absent positive integer greater than 0.667 is 3.
32+
33+
**Example 3:**
34+
35+
**Input:** nums = [4,-1]
36+
37+
**Output:** 2
38+
39+
**Explanation:**
40+
41+
* The average of `nums` is `(4 + (-1)) / 2 = 3 / 2 = 1.50`.
42+
* The smallest absent positive integer greater than 1.50 is 2.
43+
44+
**Constraints:**
45+
46+
* `1 <= nums.length <= 100`
47+
* `-100 <= nums[i] <= 100`
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g3601_3700.s3679_minimum_discards_to_balance_inventory
2+
3+
// #Medium #Biweekly_Contest_165 #2025_09_20_Time_6_ms_(100.00%)_Space_68.59_MB_(76.92%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
fun minArrivalsToDiscard(arrivals: IntArray, w: Int, m: Int): Int {
9+
val n = arrivals.size
10+
var dis = 0
11+
val removed = BooleanArray(n)
12+
var maxVal = 0
13+
for (v in arrivals) {
14+
maxVal = max(maxVal, v)
15+
}
16+
val freq = IntArray(maxVal + 1)
17+
for (i in 0..<n) {
18+
val outIdx = i - w
19+
if (outIdx >= 0 && !removed[outIdx]) {
20+
val oldVal = arrivals[outIdx]
21+
freq[oldVal]--
22+
}
23+
val `val` = arrivals[i]
24+
if (freq[`val`] >= m) {
25+
dis++
26+
removed[i] = true
27+
} else {
28+
freq[`val`]++
29+
}
30+
}
31+
return dis
32+
}
33+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
3679\. Minimum Discards to Balance Inventory
2+
3+
Medium
4+
5+
You are given two integers `w` and `m`, and an integer array `arrivals`, where `arrivals[i]` is the type of item arriving on day `i` (days are **1-indexed**).
6+
7+
Items are managed according to the following rules:
8+
9+
* Each arrival may be **kept** or **discarded**; an item may only be discarded on its arrival day.
10+
* For each day `i`, consider the window of days `[max(1, i - w + 1), i]` (the `w` most recent days up to day `i`):
11+
* For **any** such window, each item type may appear **at most** `m` times among kept arrivals whose arrival day lies in that window.
12+
* If keeping the arrival on day `i` would cause its type to appear **more than** `m` times in the window, that arrival **must** be discarded.
13+
14+
Return the **minimum** number of arrivals to be discarded so that every `w`\-day window contains at most `m` occurrences of each type.
15+
16+
**Example 1:**
17+
18+
**Input:** arrivals = [1,2,1,3,1], w = 4, m = 2
19+
20+
**Output:** 0
21+
22+
**Explanation:**
23+
24+
* On day 1, Item 1 arrives; the window contains no more than `m` occurrences of this type, so we keep it.
25+
* On day 2, Item 2 arrives; the window of days 1 - 2 is fine.
26+
* On day 3, Item 1 arrives, window `[1, 2, 1]` has item 1 twice, within limit.
27+
* On day 4, Item 3 arrives, window `[1, 2, 1, 3]` has item 1 twice, allowed.
28+
* On day 5, Item 1 arrives, window `[2, 1, 3, 1]` has item 1 twice, still valid.
29+
30+
There are no discarded items, so return 0.
31+
32+
**Example 2:**
33+
34+
**Input:** arrivals = [1,2,3,3,3,4], w = 3, m = 2
35+
36+
**Output:** 1
37+
38+
**Explanation:**
39+
40+
* On day 1, Item 1 arrives. We keep it.
41+
* On day 2, Item 2 arrives, window `[1, 2]` is fine.
42+
* On day 3, Item 3 arrives, window `[1, 2, 3]` has item 3 once.
43+
* On day 4, Item 3 arrives, window `[2, 3, 3]` has item 3 twice, allowed.
44+
* On day 5, Item 3 arrives, window `[3, 3, 3]` has item 3 three times, exceeds limit, so the arrival must be discarded.
45+
* On day 6, Item 4 arrives, window `[3, 4]` is fine.
46+
47+
Item 3 on day 5 is discarded, and this is the minimum number of arrivals to discard, so return 1.
48+
49+
**Constraints:**
50+
51+
* <code>1 <= arrivals.length <= 10<sup>5</sup></code>
52+
* <code>1 <= arrivals[i] <= 10<sup>5</sup></code>
53+
* `1 <= w <= arrivals.length`
54+
* `1 <= m <= w`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3601_3700.s3680_generate_schedule
2+
3+
// #Medium #Biweekly_Contest_165 #2025_09_20_Time_3_ms_(100.00%)_Space_48.94_MB_(100.00%)
4+
5+
class Solution {
6+
fun generateSchedule(n: Int): Array<IntArray> {
7+
if (n < 5) {
8+
return Array<IntArray>(0) { IntArray(0) }
9+
}
10+
val res = Array<IntArray>(n * (n - 1)) { IntArray(2) }
11+
var idx = 0
12+
for (i in 2..<n - 1) {
13+
for (j in 0..<n) {
14+
res[idx++] = intArrayOf(j, (j + i) % n)
15+
}
16+
}
17+
for (i in 0..<n) {
18+
res[idx++] = intArrayOf(i, (i + 1) % n)
19+
res[idx++] = intArrayOf((i + 4) % n, (i + 3) % n)
20+
}
21+
return res
22+
}
23+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
3680\. Generate Schedule
2+
3+
Medium
4+
5+
You are given an integer `n` representing `n` teams. You are asked to generate a schedule such that:
6+
7+
* Each team plays every other team **exactly twice**: once at home and once away.
8+
* There is **exactly one** match per day; the schedule is a list of **consecutive** days and `schedule[i]` is the match on day `i`.
9+
* No team plays on **consecutive** days.
10+
11+
Return a 2D integer array `schedule`, where `schedule[i][0]` represents the home team and `schedule[i][1]` represents the away team. If multiple schedules meet the conditions, return **any** one of them.
12+
13+
If no schedule exists that meets the conditions, return an empty array.
14+
15+
**Example 1:**
16+
17+
**Input:** n = 3
18+
19+
**Output:** []
20+
21+
**Explanation:**
22+
23+
Since each team plays every other team exactly twice, a total of 6 matches need to be played: `[0,1],[0,2],[1,2],[1,0],[2,0],[2,1]`.
24+
25+
It's not possible to create a schedule without at least one team playing consecutive days.
26+
27+
**Example 2:**
28+
29+
**Input:** n = 5
30+
31+
**Output:** [[0,1],[2,3],[0,4],[1,2],[3,4],[0,2],[1,3],[2,4],[0,3],[1,4],[2,0],[3,1],[4,0],[2,1],[4,3],[1,0],[3,2],[4,1],[3,0],[4,2]]
32+
33+
**Explanation:**
34+
35+
Since each team plays every other team exactly twice, a total of 20 matches need to be played.
36+
37+
The output shows one of the schedules that meet the conditions. No team plays on consecutive days.
38+
39+
**Constraints:**
40+
41+
* `2 <= n <= 50`
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g3601_3700.s3681_maximum_xor_of_subsequences
2+
3+
// #Hard #Biweekly_Contest_165 #2025_09_20_Time_26_ms_(100.00%)_Space_76.00_MB_(77.78%)
4+
5+
import kotlin.math.max
6+
import kotlin.math.min
7+
8+
class Solution {
9+
fun maxXorSubsequences(nums: IntArray): Int {
10+
val n = nums.size
11+
if (n == 0) {
12+
return 0
13+
}
14+
var x = 0
15+
while (true) {
16+
var y = 0
17+
for (v in nums) {
18+
if (v > y) {
19+
y = v
20+
}
21+
}
22+
if (y == 0) {
23+
return x
24+
}
25+
x = max(x, x xor y)
26+
for (i in 0..<n) {
27+
val v = nums[i]
28+
nums[i] = min(v, v xor y)
29+
}
30+
}
31+
}
32+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
3681\. Maximum XOR of Subsequences
2+
3+
Hard
4+
5+
You are given an integer array `nums` of length `n` where each element is a non-negative integer.
6+
7+
Select **two** **subsequences** of `nums` (they may be empty and are **allowed** to **overlap**), each preserving the original order of elements, and let:
8+
9+
* `X` be the bitwise XOR of all elements in the first subsequence.
10+
* `Y` be the bitwise XOR of all elements in the second subsequence.
11+
12+
Return the **maximum** possible value of `X XOR Y`.
13+
14+
**Note:** The XOR of an **empty** subsequence is 0.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [1,2,3]
19+
20+
**Output:** 3
21+
22+
**Explanation:**
23+
24+
Choose subsequences:
25+
26+
* First subsequence `[2]`, whose XOR is 2.
27+
* Second subsequence `[2,3]`, whose XOR is 1.
28+
29+
Then, XOR of both subsequences = `2 XOR 1 = 3`.
30+
31+
This is the maximum XOR value achievable from any two subsequences.
32+
33+
**Example 2:**
34+
35+
**Input:** nums = [5,2]
36+
37+
**Output:** 7
38+
39+
**Explanation:**
40+
41+
Choose subsequences:
42+
43+
* First subsequence `[5]`, whose XOR is 5.
44+
* Second subsequence `[2]`, whose XOR is 2.
45+
46+
Then, XOR of both subsequences = `5 XOR 2 = 7`.
47+
48+
This is the maximum XOR value achievable from any two subsequences.
49+
50+
**Constraints:**
51+
52+
* <code>2 <= nums.length <= 10<sup>5</sup></code>
53+
* <code>0 <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g3601_3700.s3683_earliest_time_to_finish_one_task
2+
3+
// #Easy #Weekly_Contest_467 #2025_09_20_Time_1_ms_(100.00%)_Space_52.19_MB_(60.38%)
4+
5+
import kotlin.math.min
6+
7+
class Solution {
8+
fun earliestTime(tasks: Array<IntArray>): Int {
9+
var ans = 1000
10+
for (i in tasks.indices) {
11+
val st = tasks[i][0]
12+
val tm = tasks[i][1]
13+
ans = min(ans, st + tm)
14+
}
15+
return ans
16+
}
17+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
3683\. Earliest Time to Finish One Task
2+
3+
Easy
4+
5+
You are given a 2D integer array `tasks` where <code>tasks[i] = [s<sub>i</sub>, t<sub>i</sub>]</code>.
6+
7+
Each <code>[s<sub>i</sub>, t<sub>i</sub>]</code> in `tasks` represents a task with start time <code>s<sub>i</sub></code> that takes <code>t<sub>i</sub></code> units of time to finish.
8+
9+
Return the earliest time at which at least one task is finished.
10+
11+
**Example 1:**
12+
13+
**Input:** tasks = [[1,6],[2,3]]
14+
15+
**Output:** 5
16+
17+
**Explanation:**
18+
19+
The first task starts at time `t = 1` and finishes at time `1 + 6 = 7`. The second task finishes at time `2 + 3 = 5`. You can finish one task at time 5.
20+
21+
**Example 2:**
22+
23+
**Input:** tasks = [[100,100],[100,100],[100,100]]
24+
25+
**Output:** 200
26+
27+
**Explanation:**
28+
29+
All three tasks finish at time `100 + 100 = 200`.
30+
31+
**Constraints:**
32+
33+
* `1 <= tasks.length <= 100`
34+
* <code>tasks[i] = [s<sub>i</sub>, t<sub>i</sub>]</code>
35+
* <code>1 <= s<sub>i</sub>, t<sub>i</sub> <= 100</code>

0 commit comments

Comments
 (0)