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,51 @@
package g3701_3800.s3780_maximum_sum_of_three_numbers_divisible_by_three;

// #Medium #Array #Sorting #Greedy #Heap_Priority_Queue #Senior #Biweekly_Contest_172
// #2026_05_06_Time_7_ms_(98.17%)_Space_168.14_MB_(89.91%)

public class Solution {
public int maximumSum(int[] nums) {
int[][] group = new int[3][3];
for (int num : nums) {
int m = num % 3;
int max1 = group[m][0];
int max2 = group[m][1];
int max3 = group[m][2];
if (num >= max1) {
max3 = max2;
max2 = max1;
max1 = num;
} else if (num >= max2) {
max3 = max2;
max2 = num;
} else if (num > max3) {
max3 = num;
}
group[m][0] = max1;
group[m][1] = max2;
group[m][2] = max3;
}
int res = 0;
for (int[] g : group) {
int sum = 0;
for (int i = 0; i < 3; i += 1) {
if (g[i] == 0) {
sum = 0;
break;
}
sum += g[i];
}
res = Math.max(res, sum);
}
int max = 0;
for (int[] g : group) {
if (g[0] == 0) {
max = 0;
break;
}
max += g[0];
}
res = Math.max(res, max);
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
3780\. Maximum Sum of Three Numbers Divisible by Three

Medium

You are given an integer array `nums`.

Your task is to choose **exactly three** integers from `nums` such that their sum is divisible by three.

Return the **maximum** possible sum of such a triplet. If no such triplet exists, return 0.

**Example 1:**

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

**Output:** 9

**Explanation:**

The valid triplets whose sum is divisible by 3 are:

* `(4, 2, 3)` with a sum of `4 + 2 + 3 = 9`.
* `(2, 3, 1)` with a sum of `2 + 3 + 1 = 6`.

Thus, the answer is 9.

**Example 2:**

**Input:** nums = [2,1,5]

**Output:** 0

**Explanation:**

No triplet forms a sum divisible by 3, so the answer is 0.

**Constraints:**

* <code>3 <= nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package g3701_3800.s3781_maximum_score_after_binary_swaps;

// #Medium #Array #String #Greedy #Heap_Priority_Queue #Staff #Biweekly_Contest_172
// #2026_05_06_Time_39_ms_(96.47%)_Space_118.00_MB_(29.41%)

import java.util.PriorityQueue;

public class Solution {
public long maximumScore(int[] nums, String s) {
long sum = 0;
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
for (int i = 0; i < nums.length; i++) {
if (s.charAt(i) == '1') {
if (pq.isEmpty() || pq.peek() <= nums[i]) {
sum += nums[i];
} else {
sum += pq.poll();
pq.add(nums[i]);
}
} else {
pq.add(nums[i]);
}
}
return sum;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
3781\. Maximum Score After Binary Swaps

Medium

You are given an integer array `nums` of length `n` and a binary string `s` of the same length.

Initially, your score is 0. Each index `i` where `s[i] = '1'` contributes `nums[i]` to the score.

You may perform **any** number of operations (including zero). In one operation, you may choose an index `i` such that `0 <= i < n - 1`, where `s[i] = '0'`, and `s[i + 1] = '1'`, and swap these two characters.

Return an integer denoting the **maximum possible score** you can achieve.

**Example 1:**

**Input:** nums = [2,1,5,2,3], s = "01010"

**Output:** 7

**Explanation:**

We can perform the following swaps:

* Swap at index `i = 0`: `"01010"` changes to `"10010"`
* Swap at index `i = 2`: `"10010"` changes to `"10100"`

Positions 0 and 2 contain `'1'`, contributing `nums[0] + nums[2] = 2 + 5 = 7`. This is maximum score achievable.

**Example 2:**

**Input:** nums = [4,7,2,9], s = "0000"

**Output:** 0

**Explanation:**

There are no `'1'` characters in `s`, so no swaps can be performed. The score remains 0.

**Constraints:**

* `n == nums.length == s.length`
* <code>1 <= n <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
* `s[i]` is either `'0'` or `'1'`
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package g3701_3800.s3782_last_remaining_integer_after_alternating_deletion_operations;

// #Hard #Math #Recursion #Senior_Staff #Biweekly_Contest_172
// #2026_05_06_Time_1_ms_(100.00%)_Space_43.05_MB_(9.38%)

public class Solution {
public long lastInteger(long n) {
final long mask = 0xAAAAAAAAAAAAAAAL;
return ((n - 1) & mask) + 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
3782\. Last Remaining Integer After Alternating Deletion Operations

Hard

You are given an integer `n`.

We write the integers from 1 to `n` in a sequence from left to right. Then, **alternately** apply the following two operations until only one integer remains, starting with operation 1:

* **Operation 1**: Starting from the left, delete every second number.
* **Operation 2**: Starting from the right, delete every second number.

Return the last remaining integer.

**Example 1:**

**Input:** n = 8

**Output:** 3

**Explanation:**

* Write `[1, 2, 3, 4, 5, 6, 7, 8]` in a sequence.
* Starting from the left, we delete every second number: <code>[1, <ins>**2**</ins>, 3, <ins>**4**</ins>, 5, <ins>**6**</ins>, 7, <ins>**8**</ins>]</code>. The remaining integers are `[1, 3, 5, 7]`.
* Starting from the right, we delete every second number: <code>[<ins>**1**</ins>, 3, <ins>**5**</ins>, 7]</code>. The remaining integers are `[3, 7]`.
* Starting from the left, we delete every second number: <code>[3, <ins>**7**</ins>]</code>. The remaining integer is `[3]`.

**Example 2:**

**Input:** n = 5

**Output:** 1

**Explanation:**

* Write `[1, 2, 3, 4, 5]` in a sequence.
* Starting from the left, we delete every second number: <code>[1, <ins>**2**</ins>, 3, <ins>**4**</ins>, 5]</code>. The remaining integers are `[1, 3, 5]`.
* Starting from the right, we delete every second number: <code>[1, <ins>**3**</ins>, 5]</code>. The remaining integers are `[1, 5]`.
* Starting from the left, we delete every second number: <code>[1, <ins>**5**</ins>]</code>. The remaining integer is `[1]`.

**Example 3:**

**Input:** n = 1

**Output:** 1

**Explanation:**

* Write `[1]` in a sequence.
* The last remaining integer is 1.

**Constraints:**

* <code>1 <= n <= 10<sup>15</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g3701_3800.s3780_maximum_sum_of_three_numbers_divisible_by_three;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void maximumSum() {
assertThat(new Solution().maximumSum(new int[] {4, 2, 3, 1}), equalTo(9));
}

@Test
void maximumSum2() {
assertThat(new Solution().maximumSum(new int[] {2, 1, 5}), equalTo(0));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g3701_3800.s3781_maximum_score_after_binary_swaps;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void maximumScore() {
assertThat(new Solution().maximumScore(new int[] {2, 1, 5, 2, 3}, "01010"), equalTo(7L));
}

@Test
void maximumScore2() {
assertThat(new Solution().maximumScore(new int[] {4, 7, 2, 9}, "0000"), equalTo(0L));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g3701_3800.s3782_last_remaining_integer_after_alternating_deletion_operations;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void lastInteger() {
assertThat(new Solution().lastInteger(8L), equalTo(3L));
}

@Test
void lastInteger2() {
assertThat(new Solution().lastInteger(5L), equalTo(1L));
}

@Test
void lastInteger3() {
assertThat(new Solution().lastInteger(1L), equalTo(1L));
}
}
Loading