Skip to content

Commit d718e36

Browse files
committed
Added tasks 3622-3625
1 parent c0f9d8f commit d718e36

File tree

12 files changed

+602
-0
lines changed

12 files changed

+602
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g3601_3700.s3622_check_divisibility_by_digit_sum_and_product;
2+
3+
// #Easy #2025_07_20_Time_0_ms_(100.00%)_Space_40.53_MB_(_%)
4+
5+
public class Solution {
6+
public boolean checkDivisibility(int n) {
7+
int x = n;
8+
int sum = 0;
9+
int mul = 1;
10+
while (x != 0) {
11+
sum += x % 10;
12+
mul *= x % 10;
13+
x = x / 10;
14+
}
15+
return n % (sum + mul) == 0;
16+
}
17+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
3622\. Check Divisibility by Digit Sum and Product
2+
3+
Easy
4+
5+
You are given a positive integer `n`. Determine whether `n` is divisible by the **sum** of the following two values:
6+
7+
* The **digit sum** of `n` (the sum of its digits).
8+
9+
* The **digit** **product** of `n` (the product of its digits).
10+
11+
12+
Return `true` if `n` is divisible by this sum; otherwise, return `false`.
13+
14+
**Example 1:**
15+
16+
**Input:** n = 99
17+
18+
**Output:** true
19+
20+
**Explanation:**
21+
22+
Since 99 is divisible by the sum (9 + 9 = 18) plus product (9 \* 9 = 81) of its digits (total 99), the output is true.
23+
24+
**Example 2:**
25+
26+
**Input:** n = 23
27+
28+
**Output:** false
29+
30+
**Explanation:**
31+
32+
Since 23 is not divisible by the sum (2 + 3 = 5) plus product (2 \* 3 = 6) of its digits (total 11), the output is false.
33+
34+
**Constraints:**
35+
36+
* <code>1 <= n <= 10<sup>6</sup></code>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g3601_3700.s3623_count_number_of_trapezoids_i;
2+
3+
// #Medium #2025_07_20_Time_30_ms_(99.91%)_Space_100.40_MB_(100.00%)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class Solution {
9+
public int countTrapezoids(int[][] points) {
10+
int mod = 1_000_000_007;
11+
long inv = 500_000_004L;
12+
Map<Integer, Integer> map = new HashMap<>(points.length);
13+
for (int[] p : points) {
14+
map.merge(p[1], 1, Integer::sum);
15+
}
16+
long sum = 0L;
17+
long sumPairs = 0L;
18+
for (int c : map.values()) {
19+
if (c > 1) {
20+
long pairs = ((long) c * (c - 1) / 2) % mod;
21+
sum = (sum + pairs) % mod;
22+
sumPairs = (sumPairs + pairs * pairs % mod) % mod;
23+
}
24+
}
25+
long res = (sum * sum % mod - sumPairs + mod) % mod;
26+
res = (res * inv) % mod;
27+
return (int) res;
28+
}
29+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
3623\. Count Number of Trapezoids I
2+
3+
Medium
4+
5+
You are given a 2D integer array `points`, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents the coordinates of the <code>i<sup>th</sup></code> point on the Cartesian plane.
6+
7+
A **horizontal** **trapezoid** is a convex quadrilateral with **at least one pair** of horizontal sides (i.e. parallel to the x-axis). Two lines are parallel if and only if they have the same slope.
8+
9+
Return the _number of unique_ **_horizontal_ _trapezoids_** that can be formed by choosing any four distinct points from `points`.
10+
11+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
12+
13+
**Example 1:**
14+
15+
**Input:** points = [[1,0],[2,0],[3,0],[2,2],[3,2]]
16+
17+
**Output:** 3
18+
19+
**Explanation:**
20+
21+
![](https://assets.leetcode.com/uploads/2025/05/01/desmos-graph-6.png) ![](https://assets.leetcode.com/uploads/2025/05/01/desmos-graph-7.png) ![](https://assets.leetcode.com/uploads/2025/05/01/desmos-graph-8.png)
22+
23+
There are three distinct ways to pick four points that form a horizontal trapezoid:
24+
25+
* Using points `[1,0]`, `[2,0]`, `[3,2]`, and `[2,2]`.
26+
* Using points `[2,0]`, `[3,0]`, `[3,2]`, and `[2,2]`.
27+
* Using points `[1,0]`, `[3,0]`, `[3,2]`, and `[2,2]`.
28+
29+
**Example 2:**
30+
31+
**Input:** points = [[0,0],[1,0],[0,1],[2,1]]
32+
33+
**Output:** 1
34+
35+
**Explanation:**
36+
37+
![](https://assets.leetcode.com/uploads/2025/04/29/desmos-graph-5.png)
38+
39+
There is only one horizontal trapezoid that can be formed.
40+
41+
**Constraints:**
42+
43+
* <code>4 <= points.length <= 10<sup>5</sup></code>
44+
* <code>–10<sup>8</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>8</sup></code>
45+
* All points are pairwise distinct.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package g3601_3700.s3624_number_of_integers_with_popcount_depth_equal_to_k_ii;
2+
3+
// #Hard #2025_07_20_Time_27_ms_(96.92%)_Space_125.98_MB_(100.00%)
4+
5+
import java.util.ArrayList;
6+
7+
public class Solution {
8+
private static final int[] DEPTH_TABLE = new int[65];
9+
10+
static {
11+
DEPTH_TABLE[1] = 0;
12+
for (int i = 2; i <= 64; ++i) {
13+
DEPTH_TABLE[i] = 1 + DEPTH_TABLE[Integer.bitCount(i)];
14+
}
15+
}
16+
17+
private int computeDepth(long number) {
18+
if (number == 1) {
19+
return 0;
20+
}
21+
return 1 + DEPTH_TABLE[Long.bitCount(number)];
22+
}
23+
24+
public int[] popcountDepth(long[] nums, long[][] queries) {
25+
int len = nums.length;
26+
int maxDepth = 6;
27+
FenwickTree[] trees = new FenwickTree[maxDepth];
28+
for (int d = 0; d < maxDepth; ++d) {
29+
trees[d] = new FenwickTree();
30+
trees[d].build(len);
31+
}
32+
for (int i = 0; i < len; ++i) {
33+
int depth = computeDepth(nums[i]);
34+
if (depth < maxDepth) {
35+
trees[depth].update(i + 1, 1);
36+
}
37+
}
38+
ArrayList<Integer> ansList = new ArrayList<>();
39+
for (long[] query : queries) {
40+
int type = (int) query[0];
41+
if (type == 1) {
42+
int left = (int) query[1];
43+
int right = (int) query[2];
44+
int depth = (int) query[3];
45+
if (depth >= 0 && depth < maxDepth) {
46+
ansList.add(trees[depth].queryRange(left + 1, right + 1));
47+
} else {
48+
ansList.add(0);
49+
}
50+
} else if (type == 2) {
51+
int index = (int) query[1];
52+
long newVal = query[2];
53+
int oldDepth = computeDepth(nums[index]);
54+
if (oldDepth < maxDepth) {
55+
trees[oldDepth].update(index + 1, -1);
56+
}
57+
nums[index] = newVal;
58+
int newDepth = computeDepth(newVal);
59+
if (newDepth < maxDepth) {
60+
trees[newDepth].update(index + 1, 1);
61+
}
62+
}
63+
}
64+
int[] ansArray = new int[ansList.size()];
65+
for (int i = 0; i < ansList.size(); i++) {
66+
ansArray[i] = ansList.get(i);
67+
}
68+
return ansArray;
69+
}
70+
71+
private static class FenwickTree {
72+
private int[] tree;
73+
private int size;
74+
75+
public FenwickTree() {
76+
this.size = 0;
77+
}
78+
79+
public void build(int n) {
80+
this.size = n;
81+
this.tree = new int[size + 1];
82+
}
83+
84+
public void update(int index, int value) {
85+
while (index <= size) {
86+
tree[index] += value;
87+
index += index & (-index);
88+
}
89+
}
90+
91+
public int query(int index) {
92+
int result = 0;
93+
while (index > 0) {
94+
result += tree[index];
95+
index -= index & (-index);
96+
}
97+
return result;
98+
}
99+
100+
public int queryRange(int left, int right) {
101+
if (left > right) {
102+
return 0;
103+
}
104+
return query(right) - query(left - 1);
105+
}
106+
}
107+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
3624\. Number of Integers With Popcount-Depth Equal to K II
2+
3+
Hard
4+
5+
You are given an integer array `nums`.
6+
7+
For any positive integer `x`, define the following sequence:
8+
9+
* <code>p<sub>0</sub> = x</code>
10+
* <code>p<sub>i+1</sub> = popcount(p<sub>i</sub>)</code> for all `i >= 0`, where `popcount(y)` is the number of set bits (1's) in the binary representation of `y`.
11+
12+
This sequence will eventually reach the value 1.
13+
14+
The **popcount-depth** of `x` is defined as the **smallest** integer `d >= 0` such that <code>p<sub>d</sub> = 1</code>.
15+
16+
For example, if `x = 7` (binary representation `"111"`). Then, the sequence is: `7 → 3 → 2 → 1`, so the popcount-depth of 7 is 3.
17+
18+
You are also given a 2D integer array `queries`, where each `queries[i]` is either:
19+
20+
* `[1, l, r, k]` - **Determine** the number of indices `j` such that `l <= j <= r` and the **popcount-depth** of `nums[j]` is equal to `k`.
21+
* `[2, idx, val]` - **Update** `nums[idx]` to `val`.
22+
23+
Return an integer array `answer`, where `answer[i]` is the number of indices for the <code>i<sup>th</sup></code> query of type `[1, l, r, k]`.
24+
25+
**Example 1:**
26+
27+
**Input:** nums = [2,4], queries = [[1,0,1,1],[2,1,1],[1,0,1,0]]
28+
29+
**Output:** [2,1]
30+
31+
**Explanation:**
32+
33+
| `i` | `queries[i]` | `nums` | binary(`nums`) | popcount-<br>depth | `[l, r]` | `k` | Valid<br>`nums[j]` | updated<br>`nums` | Answer |
34+
|-----|--------------|----------|----------------|---------------------|----------|-----|---------------------|--------------------|---------|
35+
| 0 | [1,0,1,1] | [2,4] | [10, 100] | [1, 1] | [0, 1] | 1 | [0, 1] || 2 |
36+
| 1 | [2,1,1] | [2,4] | [10, 100] | [1, 1] |||| [2,1] ||
37+
| 2 | [1,0,1,0] | [2,1] | [10, 1] | [1, 0] | [0, 1] | 0 | [1] || 1 |
38+
39+
Thus, the final `answer` is `[2, 1]`.
40+
41+
**Example 2:**
42+
43+
**Input:** nums = [3,5,6], queries = [[1,0,2,2],[2,1,4],[1,1,2,1],[1,0,1,0]]
44+
45+
**Output:** [3,1,0]
46+
47+
**Explanation:**
48+
49+
| `i` | `queries[i]` | `nums` | binary(`nums`) | popcount-<br>depth | `[l, r]` | `k` | Valid<br>`nums[j]` | updated<br>`nums` | Answer |
50+
|-----|----------------|----------------|-----------------------|---------------------|----------|-----|---------------------|--------------------|---------|
51+
| 0 | [1,0,2,2] | [3, 5, 6] | [11, 101, 110] | [2, 2, 2] | [0, 2] | 2 | [0, 1, 2] || 3 |
52+
| 1 | [2,1,4] | [3, 5, 6] | [11, 101, 110] | [2, 2, 2] |||| [3, 4, 6] ||
53+
| 2 | [1,1,2,1] | [3, 4, 6] | [11, 100, 110] | [2, 1, 2] | [1, 2] | 1 | [1] || 1 |
54+
| 3 | [1,0,1,0] | [3, 4, 6] | [11, 100, 110] | [2, 1, 2] | [0, 1] | 0 | [] || 0 |
55+
56+
Thus, the final `answer` is `[3, 1, 0]`.
57+
58+
**Example 3:**
59+
60+
**Input:** nums = [1,2], queries = [[1,0,1,1],[2,0,3],[1,0,0,1],[1,0,0,2]]
61+
62+
**Output:** [1,0,1]
63+
64+
**Explanation:**
65+
66+
| `i` | `queries[i]` | `nums` | binary(`nums`) | popcount-<br>depth | `[l, r]` | `k` | Valid<br>`nums[j]` | updated<br>`nums` | Answer |
67+
|-----|----------------|------------|----------------|---------------------|----------|-----|--------------------|--------------------|---------|
68+
| 0 | [1,0,1,1] | [1, 2] | [1, 10] | [0, 1] | [0, 1] | 1 | [1] || 1 |
69+
| 1 | [2,0,3] | [1, 2] | [1, 10] | [0, 1] |||| [3, 2] | |
70+
| 2 | [1,0,0,1] | [3, 2] | [11, 10] | [2, 1] | [0, 0] | 1 | [] || 0 |
71+
| 3 | [1,0,0,2] | [3, 2] | [11, 10] | [2, 1] | [0, 0] | 2 | [0] || 1 |
72+
73+
Thus, the final `answer` is `[1, 0, 1]`.
74+
75+
**Constraints:**
76+
77+
* <code>1 <= n == nums.length <= 10<sup>5</sup></code>
78+
* <code>1 <= nums[i] <= 10<sup>15</sup></code>
79+
* <code>1 <= queries.length <= 10<sup>5</sup></code>
80+
* `queries[i].length == 3` or `4`
81+
* `queries[i] == [1, l, r, k]` or,
82+
* `queries[i] == [2, idx, val]`
83+
* `0 <= l <= r <= n - 1`
84+
* `0 <= k <= 5`
85+
* `0 <= idx <= n - 1`
86+
* <code>1 <= val <= 10<sup>15</sup></code>

0 commit comments

Comments
 (0)