Skip to content

Commit 50412d9

Browse files
committed
Added tasks 3618-3625
1 parent 2ebc91c commit 50412d9

File tree

9 files changed

+1031
-0
lines changed
  • src/main/java/g3601_3700
    • s3618_split_array_by_prime_indices
    • s3619_count_islands_with_total_value_divisible_by_k
    • s3620_network_recovery_pathways
    • s3621_number_of_integers_with_popcount_depth_equal_to_k_i
    • s3622_check_divisibility_by_digit_sum_and_product
    • s3623_count_number_of_trapezoids_i
    • s3624_number_of_integers_with_popcount_depth_equal_to_k_ii
    • s3625_count_number_of_trapezoids_ii

9 files changed

+1031
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,14 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3625 |[Count Number of Trapezoids II](src/main/java/g3601_3700/s3625_count_number_of_trapezoids_ii)| Hard | Array, Hash_Table, Math, Geometry, Weekly_Contest_459 | 347 | 99.10
2092+
| 3624 |[Number of Integers With Popcount-Depth Equal to K II](src/main/java/g3601_3700/s3624_number_of_integers_with_popcount_depth_equal_to_k_ii)| Hard | Array, Segment_Tree, Weekly_Contest_459 | 27 | 96.44
2093+
| 3623 |[Count Number of Trapezoids I](src/main/java/g3601_3700/s3623_count_number_of_trapezoids_i)| Medium | Array, Hash_Table, Math, Geometry, Weekly_Contest_459 | 30 | 99.92
2094+
| 3622 |[Check Divisibility by Digit Sum and Product](src/main/java/g3601_3700/s3622_check_divisibility_by_digit_sum_and_product)| Easy | Math, Weekly_Contest_459 | 0 | 100.00
2095+
| 3621 |[Number of Integers With Popcount-Depth Equal to K I](src/main/java/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i)| Hard | Dynamic_Programming, Math, Combinatorics, Biweekly_Contest_161 | 9 | 70.67
2096+
| 3620 |[Network Recovery Pathways](src/main/java/g3601_3700/s3620_network_recovery_pathways)| Hard | Array, Dynamic_Programming, Binary_Search, Heap_Priority_Queue, Graph, Topological_Sort, Shortest_Path, Biweekly_Contest_161 | 151 | 66.08
2097+
| 3619 |[Count Islands With Total Value Divisible by K](src/main/java/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 | 16 | 96.65
2098+
| 3618 |[Split Array by Prime Indices](src/main/java/g3601_3700/s3618_split_array_by_prime_indices)| Medium | Array, Math, Number_Theory, Biweekly_Contest_161 | 3 | 100.00
20912099
| 3617 |[Find Students with Study Spiral Pattern](src/main/java/g3601_3700/s3617_find_students_with_study_spiral_pattern)| Hard | Database | 553 | 100.00
20922100
| 3615 |[Longest Palindromic Path in Graph](src/main/java/g3601_3700/s3615_longest_palindromic_path_in_graph)| Hard | String, Dynamic_Programming, Bit_Manipulation, Graph | 641 | 100.00
20932101
| 3614 |[Process String with Special Operations II](src/main/java/g3601_3700/s3614_process_string_with_special_operations_ii)| Hard | String, Simulation | 33 | 100.00
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3618\. Split Array by Prime Indices
5+
6+
Medium
7+
8+
You are given an integer array `nums`.
9+
10+
Split `nums` into two arrays `A` and `B` using the following rule:
11+
12+
* Elements at **prime** indices in `nums` must go into array `A`.
13+
* All other elements must go into array `B`.
14+
15+
Return the **absolute** difference between the sums of the two arrays: `|sum(A) - sum(B)|`.
16+
17+
**Note:** An empty array has a sum of 0.
18+
19+
**Example 1:**
20+
21+
**Input:** nums = [2,3,4]
22+
23+
**Output:** 1
24+
25+
**Explanation:**
26+
27+
* The only prime index in the array is 2, so `nums[2] = 4` is placed in array `A`.
28+
* The remaining elements, `nums[0] = 2` and `nums[1] = 3` are placed in array `B`.
29+
* `sum(A) = 4`, `sum(B) = 2 + 3 = 5`.
30+
* The absolute difference is `|4 - 5| = 1`.
31+
32+
**Example 2:**
33+
34+
**Input:** nums = [-1,5,7,0]
35+
36+
**Output:** 3
37+
38+
**Explanation:**
39+
40+
* The prime indices in the array are 2 and 3, so `nums[2] = 7` and `nums[3] = 0` are placed in array `A`.
41+
* The remaining elements, `nums[0] = -1` and `nums[1] = 5` are placed in array `B`.
42+
* `sum(A) = 7 + 0 = 7`, `sum(B) = -1 + 5 = 4`.
43+
* The absolute difference is `|7 - 4| = 3`.
44+
45+
**Constraints:**
46+
47+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
48+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
49+
50+
## Solution
51+
52+
```java
53+
public class Solution {
54+
public long splitArray(int[] nums) {
55+
int n = nums.length;
56+
boolean[] isPrime = sieve(n);
57+
long sumA = 0;
58+
long sumB = 0;
59+
for (int i = 0; i < n; i++) {
60+
if (isPrime[i]) {
61+
sumA += nums[i];
62+
} else {
63+
sumB += nums[i];
64+
}
65+
}
66+
return Math.abs(sumA - sumB);
67+
}
68+
69+
// Sieve of Eratosthenes to find all prime indices up to n
70+
private boolean[] sieve(int n) {
71+
boolean[] isPrime = new boolean[n];
72+
if (n > 2) {
73+
isPrime[2] = true;
74+
}
75+
for (int i = 3; i < n; i += 2) {
76+
isPrime[i] = true;
77+
}
78+
if (n > 2) {
79+
isPrime[2] = true;
80+
}
81+
for (int i = 3; i * i < n; i += 2) {
82+
if (isPrime[i]) {
83+
for (int j = i * i; j < n; j += i * 2) {
84+
isPrime[j] = false;
85+
}
86+
}
87+
}
88+
isPrime[0] = false;
89+
if (n > 1) {
90+
isPrime[1] = false;
91+
}
92+
return isPrime;
93+
}
94+
}
95+
```
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3619\. Count Islands With Total Value Divisible by K
5+
6+
Medium
7+
8+
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).
9+
10+
The **total value** of an island is the sum of the values of all cells in the island.
11+
12+
Return the number of islands with a total value **divisible by** `k`.
13+
14+
**Example 1:**
15+
16+
![](https://assets.leetcode.com/uploads/2025/03/06/example1griddrawio-1.png)
17+
18+
**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
19+
20+
**Output:** 2
21+
22+
**Explanation:**
23+
24+
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.
25+
26+
**Example 2:**
27+
28+
![](https://assets.leetcode.com/uploads/2025/03/06/example2griddrawio.png)
29+
30+
**Input:** grid = \[\[3,0,3,0], [0,3,0,3], [3,0,3,0]], k = 3
31+
32+
**Output:** 6
33+
34+
**Explanation:**
35+
36+
The grid contains six islands, each with a total value that is divisible by 3.
37+
38+
**Constraints:**
39+
40+
* `m == grid.length`
41+
* `n == grid[i].length`
42+
* `1 <= m, n <= 1000`
43+
* <code>1 <= m * n <= 10<sup>5</sup></code>
44+
* <code>0 <= grid[i][j] <= 10<sup>6</sup></code>
45+
* <code>1 <= k <= 10<sup>6</sup></code>
46+
47+
## Solution
48+
49+
```java
50+
public class Solution {
51+
private int m;
52+
private int n;
53+
54+
public int countIslands(int[][] grid, int k) {
55+
int count = 0;
56+
m = grid.length;
57+
n = grid[0].length;
58+
for (int i = 0; i < m; i++) {
59+
for (int j = 0; j < n; j++) {
60+
if (grid[i][j] != 0) {
61+
int curr = dfs(i, j, grid);
62+
if (curr % k == 0) {
63+
count++;
64+
}
65+
}
66+
}
67+
}
68+
return count;
69+
}
70+
71+
private int dfs(int i, int j, int[][] grid) {
72+
if (i >= m || j >= n || i < 0 || j < 0 || grid[i][j] == 0) {
73+
return Integer.MAX_VALUE;
74+
}
75+
int count = grid[i][j];
76+
grid[i][j] = 0;
77+
int x = dfs(i + 1, j, grid);
78+
int y = dfs(i, j + 1, grid);
79+
int a = dfs(i - 1, j, grid);
80+
int b = dfs(i, j - 1, grid);
81+
if (x != Integer.MAX_VALUE) {
82+
count += x;
83+
}
84+
if (y != Integer.MAX_VALUE) {
85+
count += y;
86+
}
87+
if (a != Integer.MAX_VALUE) {
88+
count += a;
89+
}
90+
if (b != Integer.MAX_VALUE) {
91+
count += b;
92+
}
93+
return count;
94+
}
95+
}
96+
```

0 commit comments

Comments
 (0)