Skip to content

Added tasks 3618-3625 #2019

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 13 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,47 @@
package g3601_3700.s3618_split_array_by_prime_indices;

// #Medium #Array #Math #Number_Theory #Biweekly_Contest_161
// #2025_07_22_Time_3_ms_(100.00%)_Space_62.61_MB_(10.13%)

public class Solution {
public long splitArray(int[] nums) {
int n = nums.length;
boolean[] isPrime = sieve(n);
long sumA = 0;
long sumB = 0;
for (int i = 0; i < n; i++) {
if (isPrime[i]) {
sumA += nums[i];
} else {
sumB += nums[i];
}
}
return Math.abs(sumA - sumB);
}

// Sieve of Eratosthenes to find all prime indices up to n
private boolean[] sieve(int n) {
boolean[] isPrime = new boolean[n];
if (n > 2) {
isPrime[2] = true;
}
for (int i = 3; i < n; i += 2) {
isPrime[i] = true;
}
if (n > 2) {
isPrime[2] = true;
}
for (int i = 3; i * i < n; i += 2) {
if (isPrime[i]) {
for (int j = i * i; j < n; j += i * 2) {
isPrime[j] = false;
}
}
}
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 #Matrix #Union_Find #Biweekly_Contest_161 #Depth_First_Search
// #Breadth_First_Search #2025_07_22_Time_16_ms_(96.65%)_Space_70.96_MB_(50.08%)

public class Solution {
private int m;
private int n;

public int countIslands(int[][] grid, int k) {
int count = 0;
m = grid.length;
n = grid[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] != 0) {
int curr = dfs(i, j, grid);
if (curr % k == 0) {
count++;
}
}
}
}
return count;
}

private int dfs(int i, int j, int[][] grid) {
if (i >= m || j >= n || i < 0 || j < 0 || grid[i][j] == 0) {
return Integer.MAX_VALUE;
}
int count = grid[i][j];
grid[i][j] = 0;
int x = dfs(i + 1, j, grid);
int y = dfs(i, j + 1, grid);
int a = dfs(i - 1, j, grid);
int b = dfs(i, j - 1, grid);
if (x != Integer.MAX_VALUE) {
count += x;
}
if (y != Integer.MAX_VALUE) {
count += y;
}
if (a != Integer.MAX_VALUE) {
count += a;
}
if (b != Integer.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>
100 changes: 100 additions & 0 deletions src/main/java/g3601_3700/s3620_network_recovery_pathways/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
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_151_ms_(66.08%)_Space_108.87_MB_(63.77%)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Solution {
private List<Integer> topologicalSort(int n, List<List<Integer>> g) {
int[] indeg = new int[n];
for (int i = 0; i < n; ++i) {
for (int adjNode : g.get(i)) {
indeg[adjNode]++;
}
}
Queue<Integer> q = new LinkedList<>();
List<Integer> ts = new ArrayList<>();
for (int i = 0; i < n; ++i) {
if (indeg[i] == 0) {
q.offer(i);
}
}
while (!q.isEmpty()) {
int u = q.poll();
ts.add(u);
for (int v : g.get(u)) {
indeg[v]--;
if (indeg[v] == 0) {
q.offer(v);
}
}
}
return ts;
}

private boolean check(
int x, int n, List<List<int[]>> adj, List<Integer> ts, boolean[] online, long k) {
long[] d = new long[n];
Arrays.fill(d, Long.MAX_VALUE);
d[0] = 0;
for (int u : ts) {
// If d[u] is reachable
if (d[u] != Long.MAX_VALUE) {
for (int[] p : adj.get(u)) {
int v = p[0];
int 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;
}

public int findMaxPathScore(int[][] edges, boolean[] online, long k) {
int n = online.length;
// Adjacency list for graph with edge weights
List<List<int[]>> adj = new ArrayList<>();
for (int i = 0; i < n; i++) {
adj.add(new ArrayList<>());
}
List<List<Integer>> g = new ArrayList<>();
for (int i = 0; i < n; i++) {
g.add(new ArrayList<>());
}
for (int[] e : edges) {
int u = e[0];
int v = e[1];
int c = e[2];
adj.get(u).add(new int[] {v, c});
g.get(u).add(v);
}
List<Integer> ts = topologicalSort(n, g);
if (!check(0, n, adj, ts, online, k)) {
return -1;
}
int l = 0;
int h = 0;
for (int[] e : edges) {
h = Math.max(h, e[2]);
}
while (l < h) {
int md = l + (h - l + 1) / 2;
if (check(md, n, adj, ts, online, k)) {
l = md;
} else {
h = md - 1;
}
}
return l;
}
}
87 changes: 87 additions & 0 deletions src/main/java/g3601_3700/s3620_network_recovery_pathways/readme.md
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