Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@
3808\.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add description

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
35 changes: 35 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,35 @@
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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
3810\. Minimum Operations to Reach Target Array

Medium

You are given two integer arrays `nums` and `target`, each of length `n`, where `nums[i]` is the current value at index `i` and `target[i]` is the desired value at index `i`.

You may perform the following operation any number of times (including zero):

* Choose an integer value `x`
* Find all **maximal contiguous segments** where `nums[i] == x` (a segment is **maximal** if it cannot be extended to the left or right while keeping all values equal to `x`)
* For each such segment `[l, r]`, update **simultaneously**:
* `nums[l] = target[l], nums[l + 1] = target[l + 1], ..., nums[r] = target[r]`

Return the **minimum** number of operations required to make `nums` equal to `target`.

**Example 1:**

**Input:** nums = [1,2,3], target = [2,1,3]

**Output:** 2

**Explanation:**

* Choose `x = 1`: maximal segment `[0, 0]` updated -> nums becomes `[2, 2, 3]`
* Choose `x = 2`: maximal segment `[0, 1]` updated (`nums[0]` stays 2, `nums[1]` becomes 1) -> `nums` becomes `[2, 1, 3]`
* Thus, 2 operations are required to convert `nums` to `target`.

**Example 2:**

**Input:** nums = [4,1,4], target = [5,1,4]

**Output:** 1

**Explanation:**

* Choose `x = 4`: maximal segments `[0, 0]` and `[2, 2]` updated (`nums[2]` stays 4) -> `nums` becomes `[5, 1, 4]`
* Thus, 1 operation is required to convert `nums` to `target`.

**Example 3:**

**Input:** nums = [7,3,7], target = [5,5,9]

**Output:** 2

**Explanation:**

* Choose `x = 7`: maximal segments `[0, 0]` and `[2, 2]` updated -> `nums` becomes `[5, 3, 9]`
* Choose `x = 3`: maximal segment `[1, 1]` updated -> `nums` becomes `[5, 5, 9]`
* Thus, 2 operations are required to convert `nums` to `target`.

**Constraints:**

* <code>1 <= n == nums.length == target.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i], target[i] <= 10<sup>5</sup></code>
Loading
Loading