Skip to content

Added tasks 3618-3625 #860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 22, 2025
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,57 @@
package g3601_3700.s3618_split_array_by_prime_indices

// #Medium #Array #Math #Number_Theory #Biweekly_Contest_161
// #2025_07_22_Time_6_ms_(100.00%)_Space_78.20_MB_(81.48%)

import kotlin.math.abs

class Solution {
fun splitArray(nums: IntArray): Long {
val n = nums.size
val isPrime = sieve(n)
var sumA: Long = 0
var sumB: Long = 0
for (i in 0..<n) {
if (isPrime[i]) {
sumA += nums[i].toLong()
} else {
sumB += nums[i].toLong()
}
}
return abs(sumA - sumB)
}

// Sieve of Eratosthenes to find all prime indices up to n
private fun sieve(n: Int): BooleanArray {
val isPrime = BooleanArray(n)
if (n > 2) {
isPrime[2] = true
}
run {
var i = 3
while (i < n) {
isPrime[i] = true
i += 2
}
}
if (n > 2) {
isPrime[2] = true
}
var i = 3
while (i * i < n) {
if (isPrime[i]) {
var j = i * i
while (j < n) {
isPrime[j] = false
j += i * 2
}
}
i += 2
}
isPrime[0] = false
if (n > 1) {
isPrime[1] = false
}
return isPrime
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
3618\. Split Array by Prime Indices

Medium

You are given an integer array `nums`.

Split `nums` into two arrays `A` and `B` using the following rule:

* Elements at **prime** indices in `nums` must go into array `A`.
* All other elements must go into array `B`.

Return the **absolute** difference between the sums of the two arrays: `|sum(A) - sum(B)|`.

**Note:** An empty array has a sum of 0.

**Example 1:**

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

**Output:** 1

**Explanation:**

* The only prime index in the array is 2, so `nums[2] = 4` is placed in array `A`.
* The remaining elements, `nums[0] = 2` and `nums[1] = 3` are placed in array `B`.
* `sum(A) = 4`, `sum(B) = 2 + 3 = 5`.
* The absolute difference is `|4 - 5| = 1`.

**Example 2:**

**Input:** nums = [-1,5,7,0]

**Output:** 3

**Explanation:**

* The prime indices in the array are 2 and 3, so `nums[2] = 7` and `nums[3] = 0` are placed in array `A`.
* The remaining elements, `nums[0] = -1` and `nums[1] = 5` are placed in array `B`.
* `sum(A) = 7 + 0 = 7`, `sum(B) = -1 + 5 = 4`.
* The absolute difference is `|7 - 4| = 3`.

**Constraints:**

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

// #Medium #Array #Depth_First_Search #Breadth_First_Search #Matrix #Union_Find
// #Biweekly_Contest_161 #2025_07_22_Time_14_ms_(100.00%)_Space_86.20_MB_(47.83%)

class Solution {
private var m = 0
private var n = 0

fun countIslands(grid: Array<IntArray>, k: Int): Int {
var count = 0
m = grid.size
n = grid[0].size
for (i in 0..<m) {
for (j in 0..<n) {
if (grid[i][j] != 0) {
val curr = dfs(i, j, grid)
if (curr % k == 0) {
count++
}
}
}
}
return count
}

private fun dfs(i: Int, j: Int, grid: Array<IntArray>): Int {
if (i >= m || j >= n || i < 0 || j < 0 || grid[i][j] == 0) {
return Int.Companion.MAX_VALUE
}
var count = grid[i][j]
grid[i][j] = 0
val x = dfs(i + 1, j, grid)
val y = dfs(i, j + 1, grid)
val a = dfs(i - 1, j, grid)
val b = dfs(i, j - 1, grid)
if (x != Int.Companion.MAX_VALUE) {
count += x
}
if (y != Int.Companion.MAX_VALUE) {
count += y
}
if (a != Int.Companion.MAX_VALUE) {
count += a
}
if (b != Int.Companion.MAX_VALUE) {
count += b
}
return count
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
3619\. Count Islands With Total Value Divisible by K

Medium

You are given an `m x n` matrix `grid` and a positive integer `k`. An **island** is a group of **positive** integers (representing land) that are **4-directionally** connected (horizontally or vertically).

The **total value** of an island is the sum of the values of all cells in the island.

Return the number of islands with a total value **divisible by** `k`.

**Example 1:**

![](https://assets.leetcode.com/uploads/2025/03/06/example1griddrawio-1.png)

**Input:** grid = [[0,2,1,0,0],[0,5,0,0,5],[0,0,1,0,0],[0,1,4,7,0],[0,2,0,0,8]], k = 5

**Output:** 2

**Explanation:**

The grid contains four islands. The islands highlighted in blue have a total value that is divisible by 5, while the islands highlighted in red do not.

**Example 2:**

![](https://assets.leetcode.com/uploads/2025/03/06/example2griddrawio.png)

**Input:** grid = [[3,0,3,0], [0,3,0,3], [3,0,3,0]], k = 3

**Output:** 6

**Explanation:**

The grid contains six islands, each with a total value that is divisible by 3.

**Constraints:**

* `m == grid.length`
* `n == grid[i].length`
* `1 <= m, n <= 1000`
* <code>1 <= m * n <= 10<sup>5</sup></code>
* <code>0 <= grid[i][j] <= 10<sup>6</sup></code>
* <code>1 <= k <= 10<sup>6</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package g3601_3700.s3620_network_recovery_pathways

// #Hard #Array #Dynamic_Programming #Binary_Search #Heap_Priority_Queue #Graph #Topological_Sort
// #Shortest_Path #Biweekly_Contest_161 #2025_07_22_Time_212_ms_(66.67%)_Space_150.09_MB_(13.33%)

import java.util.LinkedList
import java.util.Queue
import kotlin.math.max

class Solution {
private fun topologicalSort(n: Int, g: Array<ArrayList<Int>>): List<Int> {
val indeg = IntArray(n)
for (i in 0 until n) {
for (adjNode in g[i]) {
indeg[adjNode]++
}
}
val q: Queue<Int> = LinkedList()
val ts = ArrayList<Int>()
for (i in 0 until n) {
if (indeg[i] == 0) {
q.offer(i)
}
}
while (!q.isEmpty()) {
val u = q.poll()
ts.add(u)
for (v in g[u]) {
indeg[v]--
if (indeg[v] == 0) {
q.offer(v)
}
}
}
return ts
}

private fun check(
x: Int,
n: Int,
adj: Array<ArrayList<IntArray>>,
ts: List<Int>,
online: BooleanArray,
k: Long,
): Boolean {
val d = LongArray(n)
d.fill(Long.MAX_VALUE)
d[0] = 0
for (u in ts) {
if (d[u] != Long.MAX_VALUE) {
for (p in adj[u]) {
val v = p[0]
val c = p[1]
if (c < x || !online[v]) {
continue
}
if (d[u] + c < d[v]) {
d[v] = d[u] + c
}
}
}
}
return d[n - 1] <= k
}

fun findMaxPathScore(edges: Array<IntArray>, online: BooleanArray, k: Long): Int {
val n = online.size
// Adjacency list for graph with edge weights
val adj = Array<ArrayList<IntArray>>(n) { ArrayList() }
val g = Array<ArrayList<Int>>(n) { ArrayList() }
for (e in edges) {
val u = e[0]
val v = e[1]
val c = e[2]
adj[u].add(intArrayOf(v, c))
g[u].add(v)
}
val ts = topologicalSort(n, g)
if (!check(0, n, adj, ts, online, k)) {
return -1
}
var l = 0
var h = 0
for (e in edges) {
h = max(h, e[2])
}
while (l < h) {
val md = l + (h - l + 1) / 2
if (check(md, n, adj, ts, online, k)) {
l = md
} else {
h = md - 1
}
}
return l
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
3620\. Network Recovery Pathways

Hard

You are given a directed acyclic graph of `n` nodes numbered from 0 to `n − 1`. This is represented by a 2D array `edges` of length `m`, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, cost<sub>i</sub>]</code> indicates a one‑way communication from node <code>u<sub>i</sub></code> to node <code>v<sub>i</sub></code> with a recovery cost of <code>cost<sub>i</sub></code>.

Some nodes may be offline. You are given a boolean array `online` where `online[i] = true` means node `i` is online. Nodes 0 and `n − 1` are always online.

A path from 0 to `n − 1` is **valid** if:

* All intermediate nodes on the path are online.
* The total recovery cost of all edges on the path does not exceed `k`.

For each valid path, define its **score** as the minimum edge‑cost along that path.

Return the **maximum** path score (i.e., the largest **minimum**\-edge cost) among all valid paths. If no valid path exists, return -1.

**Example 1:**

**Input:** edges = [[0,1,5],[1,3,10],[0,2,3],[2,3,4]], online = [true,true,true,true], k = 10

**Output:** 3

**Explanation:**

![](https://assets.leetcode.com/uploads/2025/06/06/graph-10.png)

* The graph has two possible routes from node 0 to node 3:

1. Path `0 → 1 → 3`

* Total cost = `5 + 10 = 15`, which exceeds k (`15 > 10`), so this path is invalid.

2. Path `0 → 2 → 3`

* Total cost = `3 + 4 = 7 <= k`, so this path is valid.

* The minimum edge‐cost along this path is `min(3, 4) = 3`.

* There are no other valid paths. Hence, the maximum among all valid path‐scores is 3.


**Example 2:**

**Input:** edges = [[0,1,7],[1,4,5],[0,2,6],[2,3,6],[3,4,2],[2,4,6]], online = [true,true,true,false,true], k = 12

**Output:** 6

**Explanation:**

![](https://assets.leetcode.com/uploads/2025/06/06/graph-11.png)

* Node 3 is offline, so any path passing through 3 is invalid.

* Consider the remaining routes from 0 to 4:

1. Path `0 → 1 → 4`

* Total cost = `7 + 5 = 12 <= k`, so this path is valid.

* The minimum edge‐cost along this path is `min(7, 5) = 5`.

2. Path `0 → 2 → 3 → 4`

* Node 3 is offline, so this path is invalid regardless of cost.

3. Path `0 → 2 → 4`

* Total cost = `6 + 6 = 12 <= k`, so this path is valid.

* The minimum edge‐cost along this path is `min(6, 6) = 6`.

* Among the two valid paths, their scores are 5 and 6. Therefore, the answer is 6.


**Constraints:**

* `n == online.length`
* <code>2 <= n <= 5 * 10<sup>4</sup></code>
* `0 <= m == edges.length <=` <code>min(10<sup>5</sup>, n * (n - 1) / 2)</code>
* <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, cost<sub>i</sub>]</code>
* <code>0 <= u<sub>i</sub>, v<sub>i</sub> < n</code>
* <code>u<sub>i</sub> != v<sub>i</sub></code>
* <code>0 <= cost<sub>i</sub> <= 10<sup>9</sup></code>
* <code>0 <= k <= 5 * 10<sup>13</sup></code>
* `online[i]` is either `true` or `false`, and both `online[0]` and `online[n − 1]` are `true`.
* The given graph is a directed acyclic graph.
Loading
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy