Skip to content

Commit d59ae46

Browse files
committed
Added 3618-3625
1 parent 6c46525 commit d59ae46

File tree

9 files changed

+1004
-0
lines changed
  • src/main/kotlin/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

+1004
-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/kotlin/g3601_3700/s3625_count_number_of_trapezoids_ii)| Hard | Array, Hash_Table, Math, Geometry, Weekly_Contest_459 | 377 | 100.00
2092+
| 3624 |[Number of Integers With Popcount-Depth Equal to K II](src/main/kotlin/g3601_3700/s3624_number_of_integers_with_popcount_depth_equal_to_k_ii)| Hard | Array, Segment_Tree, Weekly_Contest_459 | 38 | 100.00
2093+
| 3623 |[Count Number of Trapezoids I](src/main/kotlin/g3601_3700/s3623_count_number_of_trapezoids_i)| Medium | Array, Hash_Table, Math, Geometry, Weekly_Contest_459 | 58 | 68.00
2094+
| 3622 |[Check Divisibility by Digit Sum and Product](src/main/kotlin/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/kotlin/g3601_3700/s3621_number_of_integers_with_popcount_depth_equal_to_k_i)| Hard | Dynamic_Programming, Math, Combinatorics, Biweekly_Contest_161 | 2 | 100.00
2096+
| 3620 |[Network Recovery Pathways](src/main/kotlin/g3601_3700/s3620_network_recovery_pathways)| Hard | Array, Dynamic_Programming, Binary_Search, Heap_Priority_Queue, Graph, Topological_Sort, Shortest_Path, Biweekly_Contest_161 | 212 | 66.67
2097+
| 3619 |[Count Islands With Total Value Divisible by K](src/main/kotlin/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 | 14 | 100.00
2098+
| 3618 |[Split Array by Prime Indices](src/main/kotlin/g3601_3700/s3618_split_array_by_prime_indices)| Medium | Array, Math, Number_Theory, Biweekly_Contest_161 | 6 | 100.00
20912099
| 3617 |[Find Students with Study Spiral Pattern](src/main/kotlin/g3601_3700/s3617_find_students_with_study_spiral_pattern)| Hard | Database | 553 | 100.00
20922100
| 3615 |[Longest Palindromic Path in Graph](src/main/kotlin/g3601_3700/s3615_longest_palindromic_path_in_graph)| Hard | String, Dynamic_Programming, Bit_Manipulation, Graph | 635 | 100.00
20932101
| 3614 |[Process String with Special Operations II](src/main/kotlin/g3601_3700/s3614_process_string_with_special_operations_ii)| Hard | String, Simulation | 37 | 100.00
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/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+
```kotlin
53+
import kotlin.math.abs
54+
55+
class Solution {
56+
fun splitArray(nums: IntArray): Long {
57+
val n = nums.size
58+
val isPrime = sieve(n)
59+
var sumA: Long = 0
60+
var sumB: Long = 0
61+
for (i in 0..<n) {
62+
if (isPrime[i]) {
63+
sumA += nums[i].toLong()
64+
} else {
65+
sumB += nums[i].toLong()
66+
}
67+
}
68+
return abs(sumA - sumB)
69+
}
70+
71+
// Sieve of Eratosthenes to find all prime indices up to n
72+
private fun sieve(n: Int): BooleanArray {
73+
val isPrime = BooleanArray(n)
74+
if (n > 2) {
75+
isPrime[2] = true
76+
}
77+
run {
78+
var i = 3
79+
while (i < n) {
80+
isPrime[i] = true
81+
i += 2
82+
}
83+
}
84+
if (n > 2) {
85+
isPrime[2] = true
86+
}
87+
var i = 3
88+
while (i * i < n) {
89+
if (isPrime[i]) {
90+
var j = i * i
91+
while (j < n) {
92+
isPrime[j] = false
93+
j += i * 2
94+
}
95+
}
96+
i += 2
97+
}
98+
isPrime[0] = false
99+
if (n > 1) {
100+
isPrime[1] = false
101+
}
102+
return isPrime
103+
}
104+
}
105+
```
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-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/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+
```kotlin
50+
class Solution {
51+
private var m = 0
52+
private var n = 0
53+
54+
fun countIslands(grid: Array<IntArray>, k: Int): Int {
55+
var count = 0
56+
m = grid.size
57+
n = grid[0].size
58+
for (i in 0..<m) {
59+
for (j in 0..<n) {
60+
if (grid[i][j] != 0) {
61+
val curr = dfs(i, j, grid)
62+
if (curr % k == 0) {
63+
count++
64+
}
65+
}
66+
}
67+
}
68+
return count
69+
}
70+
71+
private fun dfs(i: Int, j: Int, grid: Array<IntArray>): Int {
72+
if (i >= m || j >= n || i < 0 || j < 0 || grid[i][j] == 0) {
73+
return Int.Companion.MAX_VALUE
74+
}
75+
var count = grid[i][j]
76+
grid[i][j] = 0
77+
val x = dfs(i + 1, j, grid)
78+
val y = dfs(i, j + 1, grid)
79+
val a = dfs(i - 1, j, grid)
80+
val b = dfs(i, j - 1, grid)
81+
if (x != Int.Companion.MAX_VALUE) {
82+
count += x
83+
}
84+
if (y != Int.Companion.MAX_VALUE) {
85+
count += y
86+
}
87+
if (a != Int.Companion.MAX_VALUE) {
88+
count += a
89+
}
90+
if (b != Int.Companion.MAX_VALUE) {
91+
count += b
92+
}
93+
return count
94+
}
95+
}
96+
```

0 commit comments

Comments
 (0)