Skip to content

Commit 8c77536

Browse files
authored
Added tasks 3806-3828
1 parent 8aeb79b commit 8c77536

57 files changed

Lines changed: 2325 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g3801_3900.s3806_maximum_bitwise_and_after_increment_operations;
2+
3+
// #Hard #Array #Sorting #Greedy #Bit_Manipulation #Senior_Staff #Weekly_Contest_484
4+
// #2026_06_09_Time_107_ms_(78.72%)_Space_51.33_MB_(44.68%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
public int maximumAND(int[] a, int b, int c) {
10+
long e = 0;
11+
int f = a.length;
12+
long[] g = new long[f];
13+
for (int h = 30; h >= 0; --h) {
14+
long i = e | (1L << h);
15+
for (int j = 0; j < f; ++j) {
16+
long k = a[j];
17+
long l = i & ~k;
18+
if (l == 0) {
19+
g[j] = 0;
20+
} else {
21+
int n = 63 - Long.numberOfLeadingZeros(l);
22+
while (((k >> n) & 1) == 1) {
23+
n++;
24+
}
25+
long o = (1L << n) - 1;
26+
g[j] = ((k & ~o) | (1L << n) | (i & o)) - k;
27+
}
28+
}
29+
Arrays.sort(g);
30+
long p = 0;
31+
for (int q = 0; q < c; ++q) {
32+
p += g[q];
33+
}
34+
if (p <= b) {
35+
e = i;
36+
}
37+
}
38+
return (int) e;
39+
}
40+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
3806\. Maximum Bitwise AND After Increment Operations
2+
3+
Hard
4+
5+
You are given an integer array `nums` and two integers `k` and `m`.
6+
7+
You may perform **at most** `k` operations. In one operation, you may choose any index `i` and **increase** `nums[i]` by 1.
8+
9+
Return an integer denoting the **maximum** possible **bitwise AND** of any **subset** of size `m` after performing up to `k` operations optimally.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [3,1,2], k = 8, m = 2
14+
15+
**Output:** 6
16+
17+
**Explanation:**
18+
19+
* We need a subset of size `m = 2`. Choose indices `[0, 2]`.
20+
* Increase `nums[0] = 3` to 6 using 3 operations, and increase `nums[2] = 2` to 6 using 4 operations.
21+
* The total number of operations used is 7, which is not greater than `k = 8`.
22+
* The two chosen values become `[6, 6]`, and their bitwise AND is `6`, which is the maximum possible.
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [1,2,8,4], k = 7, m = 3
27+
28+
**Output:** 4
29+
30+
**Explanation:**
31+
32+
* We need a subset of size `m = 3`. Choose indices `[0, 1, 3]`.
33+
* Increase `nums[0] = 1` to 4 using 3 operations, increase `nums[1] = 2` to 4 using 2 operations, and keep `nums[3] = 4`.
34+
* The total number of operations used is 5, which is not greater than `k = 7`.
35+
* The three chosen values become `[4, 4, 4]`, and their bitwise AND is 4, which is the maximum possible.
36+
37+
**Example 3:**
38+
39+
**Input:** nums = [1,1], k = 3, m = 2
40+
41+
**Output:** 2
42+
43+
**Explanation:**
44+
45+
* We need a subset of size `m = 2`. Choose indices `[0, 1]`.
46+
* Increase both values from 1 to 2 using 1 operation each.
47+
* The total number of operations used is 2, which is not greater than `k = 3`.
48+
* The two chosen values become `[2, 2]`, and their bitwise AND is 2, which is the maximum possible.
49+
50+
**Constraints:**
51+
52+
* <code>1 <= n == nums.length <= 5 * 10<sup>4</sup></code>
53+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
54+
* <code>1 <= k <= 10<sup>9</sup></code>
55+
* `1 <= m <= n`
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
3808\. Find Emotionally Consistent Users
2+
3+
Medium
4+
5+
Table: `reactions`
6+
7+
+--------------+---------+
8+
| Column Name | Type |
9+
+--------------+---------+
10+
| user_id | int |
11+
| content_id | int |
12+
| reaction | varchar |
13+
+--------------+---------+
14+
15+
(user_id, content_id) is the primary key (unique value) for this table.
16+
Each row represents a reaction given by a user to a piece of content.
17+
18+
Write a solution to identify **emotionally consistent users** based on the following requirements:
19+
20+
* For each user, count the total number of reactions they have given.
21+
* Only include users who have reacted to **at least** `5` **different content items**.
22+
* A user is considered **emotionally consistent** if **at least** `60%` of their reactions are of the **same type**.
23+
24+
Return _the result table ordered by_ `reaction_ratio` _in **descending** order and then by_ `user_id` _in **ascending** order_.
25+
26+
**Note:**
27+
28+
* `reaction_ratio` should be rounded to `2` decimal places
29+
30+
The result format is in the following example.
31+
32+
**Example:**
33+
34+
**Input:**
35+
36+
reactions table:
37+
38+
+---------+------------+----------+
39+
| user_id | content_id | reaction |
40+
+---------+------------+----------+
41+
| 1 | 101 | like |
42+
| 1 | 102 | like |
43+
| 1 | 103 | like |
44+
| 1 | 104 | wow |
45+
| 1 | 105 | like |
46+
| 2 | 201 | like |
47+
| 2 | 202 | wow |
48+
| 2 | 203 | sad |
49+
| 2 | 204 | like |
50+
| 2 | 205 | wow |
51+
| 3 | 301 | love |
52+
| 3 | 302 | love |
53+
| 3 | 303 | love |
54+
| 3 | 304 | love |
55+
| 3 | 305 | love |
56+
+---------+------------+----------+
57+
58+
**Output:**
59+
60+
+---------+-------------------+----------------+
61+
| user_id | dominant_reaction | reaction_ratio |
62+
+---------+-------------------+----------------+
63+
| 3 | love | 1.00 |
64+
| 1 | like | 0.80 |
65+
+---------+-------------------+----------------+
66+
67+
**Explanation:**
68+
69+
* **User 1**:
70+
* Total reactions = 5
71+
* like appears 4 times
72+
* reaction_ratio = 4 / 5 = 0.80
73+
* Meets the 60% consistency requirement
74+
* **User 2**:
75+
* Total reactions = 5
76+
* Most frequent reaction appears only 2 times
77+
* reaction_ratio = 2 / 5 = 0.40
78+
* Does not meet the consistency requirement
79+
* **User 3**:
80+
* Total reactions = 5
81+
* 'love' appears 5 times
82+
* reaction_ratio = 5 / 5 = 1.00
83+
* Meets the consistency requirement
84+
85+
The Results table is ordered by reaction_ratio in descending order, then by user_id in ascending order.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# #Medium #2026_06_09_Time_290_ms_(87.69%)_Space_0.0_MB_(100.00%)
2+
# Write your MySQL query statement below
3+
WITH user_selection AS
4+
(SELECT
5+
user_id,
6+
COUNT(reaction) AS total_reaction_count
7+
FROM
8+
reactions
9+
GROUP BY
10+
user_id
11+
HAVING
12+
COUNT(DISTINCT content_id) >= 5
13+
),
14+
reaction_counts
15+
AS
16+
(SELECT
17+
user_id,
18+
reaction,
19+
COUNT(*) AS reaction_count
20+
FROM
21+
reactions
22+
group by
23+
user_id,
24+
reaction
25+
),
26+
ranked_reactions AS (
27+
-- Step 2: Use a window function to find the max for each user
28+
SELECT
29+
user_id,
30+
reaction,
31+
reaction_count,
32+
RANK() OVER(PARTITION BY user_id ORDER BY reaction_count DESC) as rnk
33+
FROM reaction_counts
34+
)
35+
SELECT
36+
rc.user_id,
37+
rc.reaction AS dominant_reaction,
38+
ROUND(reaction_count / total_reaction_count, 2) AS reaction_ratio
39+
FROM
40+
ranked_reactions rc
41+
INNER JOIN
42+
user_selection us
43+
ON
44+
rc.user_id = us.user_id
45+
WHERE
46+
rc.rnk = 1
47+
AND ROUND(reaction_count / total_reaction_count, 2) >= 0.60
48+
ORDER BY
49+
3 DESC,
50+
rc.user_id
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g3801_3900.s3809_best_reachable_tower;
2+
3+
// #Medium #Array #Senior #Biweekly_Contest_174
4+
// #2026_06_09_Time_3_ms_(70.30%)_Space_219.56_MB_(26.73%)
5+
6+
public class Solution {
7+
public int[] bestTower(int[][] towers, int[] center, int radius) {
8+
int bestX = -1;
9+
int bestY = -1;
10+
int bestQ = -1;
11+
int cx = center[0];
12+
int cy = center[1];
13+
for (int[] t : towers) {
14+
int x = t[0];
15+
int y = t[1];
16+
int q = t[2];
17+
long dx = Math.abs((long) x - cx);
18+
long dy = Math.abs((long) y - cy);
19+
if (dx + dy <= radius
20+
&& (q > bestQ
21+
|| (q == bestQ
22+
&& (bestX == -1 || x < bestX || (x == bestX && y < bestY))))) {
23+
bestQ = q;
24+
bestX = x;
25+
bestY = y;
26+
}
27+
}
28+
return bestQ == -1 ? new int[] {-1, -1} : new int[] {bestX, bestY};
29+
}
30+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
3809\. Best Reachable Tower
2+
3+
Medium
4+
5+
You are given a 2D integer array `towers`, where <code>towers[i] = [x<sub>i</sub>, y<sub>i</sub>, q<sub>i</sub>]</code> represents the coordinates <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and quality factor <code>q<sub>i</sub></code> of the <code>i<sup>th</sup></code> tower.
6+
7+
You are also given an integer array `center = [cx, cy]` representing your location, and an integer `radius`.
8+
9+
A tower is **reachable** if its **Manhattan distance** from `center` is **less than or equal** to `radius`.
10+
11+
Among all reachable towers:
12+
13+
* Return the coordinates of the tower with the **maximum** quality factor.
14+
* If there is a tie, return the tower with the **lexicographically smallest** coordinate. If no tower is reachable, return `[-1, -1]`.
15+
16+
The **Manhattan Distance** between two cells <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and <code>(x<sub>j</sub>, y<sub>j</sub>)</code> is <code>|x<sub>i</sub> - x<sub>j</sub>| + |y<sub>i</sub> - y<sub>j</sub>|</code>.
17+
18+
A coordinate <code>[x<sub>i</sub>, y<sub>i</sub>]</code> is **lexicographically smaller** than <code>[x<sub>j</sub>, y<sub>j</sub>]</code> if <code>x<sub>i</sub> < x<sub>j</sub></code>, or <code>x<sub>i</sub> == x<sub>j</sub></code> and <code>y<sub>i</sub> < y<sub>j</sub></code>.
19+
20+
`|x|` denotes the **absolute** **value** of `x`.
21+
22+
**Example 1:**
23+
24+
**Input:** towers = [[1,2,5], [2,1,7], [3,1,9]], center = [1,1], radius = 2
25+
26+
**Output:** [3,1]
27+
28+
**Explanation:**
29+
30+
* Tower `[1, 2, 5]`: Manhattan distance = `|1 - 1| + |2 - 1| = 1`, reachable.
31+
* Tower `[2, 1, 7]`: Manhattan distance = `|2 - 1| + |1 - 1| = 1`, reachable.
32+
* Tower `[3, 1, 9]`: Manhattan distance = `|3 - 1| + |1 - 1| = 2`, reachable.
33+
34+
All towers are reachable. The maximum quality factor is 9, which corresponds to tower `[3, 1]`.
35+
36+
**Example 2:**
37+
38+
**Input:** towers = [[1,3,4], [2,2,4], [4,4,7]], center = [0,0], radius = 5
39+
40+
**Output:** [1,3]
41+
42+
**Explanation:**
43+
44+
* Tower `[1, 3, 4]`: Manhattan distance = `|1 - 0| + |3 - 0| = 4`, reachable.
45+
* Tower `[2, 2, 4]`: Manhattan distance = `|2 - 0| + |2 - 0| = 4`, reachable.
46+
* Tower `[4, 4, 7]`: Manhattan distance = `|4 - 0| + |4 - 0| = 8`, not reachable.
47+
48+
Among the reachable towers, the maximum quality factor is 4. Both `[1, 3]` and `[2, 2]` have the same quality, so the lexicographically smaller coordinate is `[1, 3]`.
49+
50+
**Example 3:**
51+
52+
**Input:** towers = [[5,6,8], [0,3,5]], center = [1,2], radius = 1
53+
54+
**Output:** [-1,-1]
55+
56+
**Explanation:**
57+
58+
* Tower `[5, 6, 8]`: Manhattan distance = `|5 - 1| + |6 - 2| = 8`, not reachable.
59+
* Tower `[0, 3, 5]`: Manhattan distance = `|0 - 1| + |3 - 2| = 2`, not reachable.
60+
61+
No tower is reachable within the given radius, so `[-1, -1]` is returned.
62+
63+
**Constraints:**
64+
65+
* <code>1 <= towers.length <= 10<sup>5</sup></code>
66+
* <code>towers[i] = [x<sub>i</sub>, y<sub>i</sub>, q<sub>i</sub>]</code>
67+
* `center = [cx, cy]`
68+
* <code>0 <= x<sub>i</sub>, y<sub>i</sub>, q<sub>i</sub>, cx, cy <= 10<sup>5</sup></code>
69+
* <code>0 <= radius <= 10<sup>5</sup></code>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g3801_3900.s3810_minimum_operations_to_reach_target_array;
2+
3+
// #Medium #Array #Hash_Table #Greedy #Senior #Biweekly_Contest_174
4+
// #2026_06_09_Time_24_ms_(89.58%)_Space_123.26_MB_(72.92%)
5+
6+
import java.util.HashSet;
7+
import java.util.Set;
8+
9+
public class Solution {
10+
public int minOperations(int[] nums, int[] target) {
11+
Set<Integer> virelantos = new HashSet<>();
12+
for (int i = 0; i < nums.length; i++) {
13+
if (nums[i] != target[i]) {
14+
virelantos.add(nums[i]);
15+
}
16+
}
17+
return virelantos.size();
18+
}
19+
}

0 commit comments

Comments
 (0)