Skip to content
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,40 @@
package g3801_3900.s3806_maximum_bitwise_and_after_increment_operations;

// #Hard #Array #Sorting #Greedy #Bit_Manipulation #Senior_Staff #Weekly_Contest_484
// #2026_06_09_Time_107_ms_(78.72%)_Space_51.33_MB_(44.68%)

import java.util.Arrays;

public class Solution {
public int maximumAND(int[] a, int b, int c) {
long e = 0;
int f = a.length;
long[] g = new long[f];
for (int h = 30; h >= 0; --h) {
long i = e | (1L << h);
for (int j = 0; j < f; ++j) {
long k = a[j];
long l = i & ~k;
if (l == 0) {
g[j] = 0;
} else {
int n = 63 - Long.numberOfLeadingZeros(l);
while (((k >> n) & 1) == 1) {
n++;
}
long o = (1L << n) - 1;
g[j] = ((k & ~o) | (1L << n) | (i & o)) - k;
}
}
Arrays.sort(g);
long p = 0;
for (int q = 0; q < c; ++q) {
p += g[q];
}
if (p <= b) {
e = i;
}
}
return (int) e;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
3806\. Maximum Bitwise AND After Increment Operations

Hard

You are given an integer array `nums` and two integers `k` and `m`.

You may perform **at most** `k` operations. In one operation, you may choose any index `i` and **increase** `nums[i]` by 1.

Return an integer denoting the **maximum** possible **bitwise AND** of any **subset** of size `m` after performing up to `k` operations optimally.

**Example 1:**

**Input:** nums = [3,1,2], k = 8, m = 2

**Output:** 6

**Explanation:**

* We need a subset of size `m = 2`. Choose indices `[0, 2]`.
* Increase `nums[0] = 3` to 6 using 3 operations, and increase `nums[2] = 2` to 6 using 4 operations.
* The total number of operations used is 7, which is not greater than `k = 8`.
* The two chosen values become `[6, 6]`, and their bitwise AND is `6`, which is the maximum possible.

**Example 2:**

**Input:** nums = [1,2,8,4], k = 7, m = 3

**Output:** 4

**Explanation:**

* We need a subset of size `m = 3`. Choose indices `[0, 1, 3]`.
* Increase `nums[0] = 1` to 4 using 3 operations, increase `nums[1] = 2` to 4 using 2 operations, and keep `nums[3] = 4`.
* The total number of operations used is 5, which is not greater than `k = 7`.
* The three chosen values become `[4, 4, 4]`, and their bitwise AND is 4, which is the maximum possible.

**Example 3:**

**Input:** nums = [1,1], k = 3, m = 2

**Output:** 2

**Explanation:**

* We need a subset of size `m = 2`. Choose indices `[0, 1]`.
* Increase both values from 1 to 2 using 1 operation each.
* The total number of operations used is 2, which is not greater than `k = 3`.
* The two chosen values become `[2, 2]`, and their bitwise AND is 2, which is the maximum possible.

**Constraints:**

* <code>1 <= n == nums.length <= 5 * 10<sup>4</sup></code>
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
* <code>1 <= k <= 10<sup>9</sup></code>
* `1 <= m <= n`
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
3808\. Find Emotionally Consistent Users

Medium

Table: `reactions`

+--------------+---------+
| Column Name | Type |
+--------------+---------+
| user_id | int |
| content_id | int |
| reaction | varchar |
+--------------+---------+

(user_id, content_id) is the primary key (unique value) for this table.
Each row represents a reaction given by a user to a piece of content.

Write a solution to identify **emotionally consistent users** based on the following requirements:

* For each user, count the total number of reactions they have given.
* Only include users who have reacted to **at least** `5` **different content items**.
* A user is considered **emotionally consistent** if **at least** `60%` of their reactions are of the **same type**.

Return _the result table ordered by_ `reaction_ratio` _in **descending** order and then by_ `user_id` _in **ascending** order_.

**Note:**

* `reaction_ratio` should be rounded to `2` decimal places

The result format is in the following example.

**Example:**

**Input:**

reactions table:

+---------+------------+----------+
| user_id | content_id | reaction |
+---------+------------+----------+
| 1 | 101 | like |
| 1 | 102 | like |
| 1 | 103 | like |
| 1 | 104 | wow |
| 1 | 105 | like |
| 2 | 201 | like |
| 2 | 202 | wow |
| 2 | 203 | sad |
| 2 | 204 | like |
| 2 | 205 | wow |
| 3 | 301 | love |
| 3 | 302 | love |
| 3 | 303 | love |
| 3 | 304 | love |
| 3 | 305 | love |
+---------+------------+----------+

**Output:**

+---------+-------------------+----------------+
| user_id | dominant_reaction | reaction_ratio |
+---------+-------------------+----------------+
| 3 | love | 1.00 |
| 1 | like | 0.80 |
+---------+-------------------+----------------+

**Explanation:**

* **User 1**:
* Total reactions = 5
* like appears 4 times
* reaction_ratio = 4 / 5 = 0.80
* Meets the 60% consistency requirement
* **User 2**:
* Total reactions = 5
* Most frequent reaction appears only 2 times
* reaction_ratio = 2 / 5 = 0.40
* Does not meet the consistency requirement
* **User 3**:
* Total reactions = 5
* 'love' appears 5 times
* reaction_ratio = 5 / 5 = 1.00
* Meets the consistency requirement

The Results table is ordered by reaction_ratio in descending order, then by user_id in ascending order.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# #Medium #2026_06_09_Time_290_ms_(87.69%)_Space_0.0_MB_(100.00%)
# Write your MySQL query statement below
WITH user_selection AS
(SELECT
user_id,
COUNT(reaction) AS total_reaction_count
FROM
reactions
GROUP BY
user_id
HAVING
COUNT(DISTINCT content_id) >= 5
),
reaction_counts
AS
(SELECT
user_id,
reaction,
COUNT(*) AS reaction_count
FROM
reactions
group by
user_id,
reaction
),
ranked_reactions AS (
-- Step 2: Use a window function to find the max for each user
SELECT
user_id,
reaction,
reaction_count,
RANK() OVER(PARTITION BY user_id ORDER BY reaction_count DESC) as rnk
FROM reaction_counts
)
SELECT
rc.user_id,
rc.reaction AS dominant_reaction,
ROUND(reaction_count / total_reaction_count, 2) AS reaction_ratio
FROM
ranked_reactions rc
INNER JOIN
user_selection us
ON
rc.user_id = us.user_id
WHERE
rc.rnk = 1
AND ROUND(reaction_count / total_reaction_count, 2) >= 0.60
ORDER BY
3 DESC,
rc.user_id
30 changes: 30 additions & 0 deletions src/main/java/g3801_3900/s3809_best_reachable_tower/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package g3801_3900.s3809_best_reachable_tower;

// #Medium #Array #Senior #Biweekly_Contest_174
// #2026_06_09_Time_3_ms_(70.30%)_Space_219.56_MB_(26.73%)

public class Solution {
public int[] bestTower(int[][] towers, int[] center, int radius) {
int bestX = -1;
int bestY = -1;
int bestQ = -1;
int cx = center[0];
int cy = center[1];
for (int[] t : towers) {
int x = t[0];
int y = t[1];
int q = t[2];
long dx = Math.abs((long) x - cx);
long dy = Math.abs((long) y - cy);
if (dx + dy <= radius
&& (q > bestQ
|| (q == bestQ
&& (bestX == -1 || x < bestX || (x == bestX && y < bestY))))) {
bestQ = q;
bestX = x;
bestY = y;
}
}
return bestQ == -1 ? new int[] {-1, -1} : new int[] {bestX, bestY};
}
}
69 changes: 69 additions & 0 deletions src/main/java/g3801_3900/s3809_best_reachable_tower/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
3809\. Best Reachable Tower

Medium

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.

You are also given an integer array `center = [cx, cy]` representing your location, and an integer `radius`.

A tower is **reachable** if its **Manhattan distance** from `center` is **less than or equal** to `radius`.

Among all reachable towers:

* Return the coordinates of the tower with the **maximum** quality factor.
* If there is a tie, return the tower with the **lexicographically smallest** coordinate. If no tower is reachable, return `[-1, -1]`.

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>.

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>.

`|x|` denotes the **absolute** **value** of `x`.

**Example 1:**

**Input:** towers = [[1,2,5], [2,1,7], [3,1,9]], center = [1,1], radius = 2

**Output:** [3,1]

**Explanation:**

* Tower `[1, 2, 5]`: Manhattan distance = `|1 - 1| + |2 - 1| = 1`, reachable.
* Tower `[2, 1, 7]`: Manhattan distance = `|2 - 1| + |1 - 1| = 1`, reachable.
* Tower `[3, 1, 9]`: Manhattan distance = `|3 - 1| + |1 - 1| = 2`, reachable.

All towers are reachable. The maximum quality factor is 9, which corresponds to tower `[3, 1]`.

**Example 2:**

**Input:** towers = [[1,3,4], [2,2,4], [4,4,7]], center = [0,0], radius = 5

**Output:** [1,3]

**Explanation:**

* Tower `[1, 3, 4]`: Manhattan distance = `|1 - 0| + |3 - 0| = 4`, reachable.
* Tower `[2, 2, 4]`: Manhattan distance = `|2 - 0| + |2 - 0| = 4`, reachable.
* Tower `[4, 4, 7]`: Manhattan distance = `|4 - 0| + |4 - 0| = 8`, not reachable.

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]`.

**Example 3:**

**Input:** towers = [[5,6,8], [0,3,5]], center = [1,2], radius = 1

**Output:** [-1,-1]

**Explanation:**

* Tower `[5, 6, 8]`: Manhattan distance = `|5 - 1| + |6 - 2| = 8`, not reachable.
* Tower `[0, 3, 5]`: Manhattan distance = `|0 - 1| + |3 - 2| = 2`, not reachable.

No tower is reachable within the given radius, so `[-1, -1]` is returned.

**Constraints:**

* <code>1 <= towers.length <= 10<sup>5</sup></code>
* <code>towers[i] = [x<sub>i</sub>, y<sub>i</sub>, q<sub>i</sub>]</code>
* `center = [cx, cy]`
* <code>0 <= x<sub>i</sub>, y<sub>i</sub>, q<sub>i</sub>, cx, cy <= 10<sup>5</sup></code>
* <code>0 <= radius <= 10<sup>5</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package g3801_3900.s3810_minimum_operations_to_reach_target_array;

// #Medium #Array #Hash_Table #Greedy #Senior #Biweekly_Contest_174
// #2026_06_09_Time_24_ms_(89.58%)_Space_123.26_MB_(72.92%)

import java.util.HashSet;
import java.util.Set;

public class Solution {
public int minOperations(int[] nums, int[] target) {
Set<Integer> virelantos = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
if (nums[i] != target[i]) {
virelantos.add(nums[i]);
}
}
return virelantos.size();
}
}
Loading
Loading