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
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package g3601_3700.s3678_smallest_absent_positive_greater_than_average

// #Easy #Biweekly_Contest_165 #2025_09_20_Time_3_ms_(100.00%)_Space_47.84_MB_(100.00%)

class Solution {
fun smallestAbsent(nums: IntArray): Int {
var sum = 0
for (j in nums) {
sum += j
}
val avg = sum.toDouble() / nums.size
var num: Int
if (avg < 0) {
num = 1
} else {
num = avg.toInt() + 1
}
while (true) {
var flag = false
for (j in nums) {
if (num == j) {
flag = true
break
}
}
if (!flag && num > avg) {
return num
}
num++
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
3678\. Smallest Absent Positive Greater Than Average

Easy

You are given an integer array `nums`.

Return the **smallest absent positive** integer in `nums` such that it is **strictly greater** than the **average** of all elements in `nums`.

The **average** of an array is defined as the sum of all its elements divided by the number of elements.

**Example 1:**

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

**Output:** 6

**Explanation:**

* The average of `nums` is `(3 + 5) / 2 = 8 / 2 = 4`.
* The smallest absent positive integer greater than 4 is 6.

**Example 2:**

**Input:** nums = [-1,1,2]

**Output:** 3

**Explanation:**

* The average of `nums` is `(-1 + 1 + 2) / 3 = 2 / 3 = 0.667`.
* The smallest absent positive integer greater than 0.667 is 3.

**Example 3:**

**Input:** nums = [4,-1]

**Output:** 2

**Explanation:**

* The average of `nums` is `(4 + (-1)) / 2 = 3 / 2 = 1.50`.
* The smallest absent positive integer greater than 1.50 is 2.

**Constraints:**

* `1 <= nums.length <= 100`
* `-100 <= nums[i] <= 100`
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package g3601_3700.s3679_minimum_discards_to_balance_inventory

// #Medium #Biweekly_Contest_165 #2025_09_20_Time_6_ms_(100.00%)_Space_68.59_MB_(76.92%)

import kotlin.math.max

class Solution {
fun minArrivalsToDiscard(arrivals: IntArray, w: Int, m: Int): Int {
val n = arrivals.size
var dis = 0
val removed = BooleanArray(n)
var maxVal = 0
for (v in arrivals) {
maxVal = max(maxVal, v)
}
val freq = IntArray(maxVal + 1)
for (i in 0..<n) {
val outIdx = i - w
if (outIdx >= 0 && !removed[outIdx]) {
val oldVal = arrivals[outIdx]
freq[oldVal]--
}
val `val` = arrivals[i]
if (freq[`val`] >= m) {
dis++
removed[i] = true
} else {
freq[`val`]++
}
}
return dis
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
3679\. Minimum Discards to Balance Inventory

Medium

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**).

Items are managed according to the following rules:

* Each arrival may be **kept** or **discarded**; an item may only be discarded on its arrival day.
* For each day `i`, consider the window of days `[max(1, i - w + 1), i]` (the `w` most recent days up to day `i`):
* For **any** such window, each item type may appear **at most** `m` times among kept arrivals whose arrival day lies in that window.
* 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.

Return the **minimum** number of arrivals to be discarded so that every `w`\-day window contains at most `m` occurrences of each type.

**Example 1:**

**Input:** arrivals = [1,2,1,3,1], w = 4, m = 2

**Output:** 0

**Explanation:**

* On day 1, Item 1 arrives; the window contains no more than `m` occurrences of this type, so we keep it.
* On day 2, Item 2 arrives; the window of days 1 - 2 is fine.
* On day 3, Item 1 arrives, window `[1, 2, 1]` has item 1 twice, within limit.
* On day 4, Item 3 arrives, window `[1, 2, 1, 3]` has item 1 twice, allowed.
* On day 5, Item 1 arrives, window `[2, 1, 3, 1]` has item 1 twice, still valid.

There are no discarded items, so return 0.

**Example 2:**

**Input:** arrivals = [1,2,3,3,3,4], w = 3, m = 2

**Output:** 1

**Explanation:**

* On day 1, Item 1 arrives. We keep it.
* On day 2, Item 2 arrives, window `[1, 2]` is fine.
* On day 3, Item 3 arrives, window `[1, 2, 3]` has item 3 once.
* On day 4, Item 3 arrives, window `[2, 3, 3]` has item 3 twice, allowed.
* On day 5, Item 3 arrives, window `[3, 3, 3]` has item 3 three times, exceeds limit, so the arrival must be discarded.
* On day 6, Item 4 arrives, window `[3, 4]` is fine.

Item 3 on day 5 is discarded, and this is the minimum number of arrivals to discard, so return 1.

**Constraints:**

* <code>1 <= arrivals.length <= 10<sup>5</sup></code>
* <code>1 <= arrivals[i] <= 10<sup>5</sup></code>
* `1 <= w <= arrivals.length`
* `1 <= m <= w`
23 changes: 23 additions & 0 deletions src/main/kotlin/g3601_3700/s3680_generate_schedule/Solution.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g3601_3700.s3680_generate_schedule

// #Medium #Biweekly_Contest_165 #2025_09_20_Time_3_ms_(100.00%)_Space_48.94_MB_(100.00%)

class Solution {
fun generateSchedule(n: Int): Array<IntArray> {
if (n < 5) {
return Array<IntArray>(0) { IntArray(0) }
}
val res = Array<IntArray>(n * (n - 1)) { IntArray(2) }
var idx = 0
for (i in 2..<n - 1) {
for (j in 0..<n) {
res[idx++] = intArrayOf(j, (j + i) % n)
}
}
for (i in 0..<n) {
res[idx++] = intArrayOf(i, (i + 1) % n)
res[idx++] = intArrayOf((i + 4) % n, (i + 3) % n)
}
return res
}
}
41 changes: 41 additions & 0 deletions src/main/kotlin/g3601_3700/s3680_generate_schedule/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
3680\. Generate Schedule

Medium

You are given an integer `n` representing `n` teams. You are asked to generate a schedule such that:

* Each team plays every other team **exactly twice**: once at home and once away.
* There is **exactly one** match per day; the schedule is a list of **consecutive** days and `schedule[i]` is the match on day `i`.
* No team plays on **consecutive** days.

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.

If no schedule exists that meets the conditions, return an empty array.

**Example 1:**

**Input:** n = 3

**Output:** []

**Explanation:**

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]`.

It's not possible to create a schedule without at least one team playing consecutive days.

**Example 2:**

**Input:** n = 5

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

**Explanation:**

Since each team plays every other team exactly twice, a total of 20 matches need to be played.

The output shows one of the schedules that meet the conditions. No team plays on consecutive days.

**Constraints:**

* `2 <= n <= 50`
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package g3601_3700.s3681_maximum_xor_of_subsequences

// #Hard #Biweekly_Contest_165 #2025_09_20_Time_26_ms_(100.00%)_Space_76.00_MB_(77.78%)

import kotlin.math.max
import kotlin.math.min

class Solution {
fun maxXorSubsequences(nums: IntArray): Int {
val n = nums.size
if (n == 0) {
return 0
}
var x = 0
while (true) {
var y = 0
for (v in nums) {
if (v > y) {
y = v
}
}
if (y == 0) {
return x
}
x = max(x, x xor y)
for (i in 0..<n) {
val v = nums[i]
nums[i] = min(v, v xor y)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
3681\. Maximum XOR of Subsequences

Hard

You are given an integer array `nums` of length `n` where each element is a non-negative integer.

Select **two** **subsequences** of `nums` (they may be empty and are **allowed** to **overlap**), each preserving the original order of elements, and let:

* `X` be the bitwise XOR of all elements in the first subsequence.
* `Y` be the bitwise XOR of all elements in the second subsequence.

Return the **maximum** possible value of `X XOR Y`.

**Note:** The XOR of an **empty** subsequence is 0.

**Example 1:**

**Input:** nums = [1,2,3]

**Output:** 3

**Explanation:**

Choose subsequences:

* First subsequence `[2]`, whose XOR is 2.
* Second subsequence `[2,3]`, whose XOR is 1.

Then, XOR of both subsequences = `2 XOR 1 = 3`.

This is the maximum XOR value achievable from any two subsequences.

**Example 2:**

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

**Output:** 7

**Explanation:**

Choose subsequences:

* First subsequence `[5]`, whose XOR is 5.
* Second subsequence `[2]`, whose XOR is 2.

Then, XOR of both subsequences = `5 XOR 2 = 7`.

This is the maximum XOR value achievable from any two subsequences.

**Constraints:**

* <code>2 <= nums.length <= 10<sup>5</sup></code>
* <code>0 <= nums[i] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package g3601_3700.s3683_earliest_time_to_finish_one_task

// #Easy #Weekly_Contest_467 #2025_09_20_Time_1_ms_(100.00%)_Space_52.19_MB_(60.38%)

import kotlin.math.min

class Solution {
fun earliestTime(tasks: Array<IntArray>): Int {
var ans = 1000
for (i in tasks.indices) {
val st = tasks[i][0]
val tm = tasks[i][1]
ans = min(ans, st + tm)
}
return ans
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
3683\. Earliest Time to Finish One Task

Easy

You are given a 2D integer array `tasks` where <code>tasks[i] = [s<sub>i</sub>, t<sub>i</sub>]</code>.

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.

Return the earliest time at which at least one task is finished.

**Example 1:**

**Input:** tasks = [[1,6],[2,3]]

**Output:** 5

**Explanation:**

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.

**Example 2:**

**Input:** tasks = [[100,100],[100,100],[100,100]]

**Output:** 200

**Explanation:**

All three tasks finish at time `100 + 100 = 200`.

**Constraints:**

* `1 <= tasks.length <= 100`
* <code>tasks[i] = [s<sub>i</sub>, t<sub>i</sub>]</code>
* <code>1 <= s<sub>i</sub>, t<sub>i</sub> <= 100</code>
Loading
Loading