Skip to content

Commit 726ca32

Browse files
committed
Added tasks 3627-3630
1 parent f624be3 commit 726ca32

File tree

12 files changed

+498
-0
lines changed

12 files changed

+498
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3601_3700.s3627_maximum_median_sum_of_subsequences_of_size_3;
2+
3+
// #Medium #Weekly_Contest_460 #2025_07_27_Time_22_ms_(100.00%)_Space_129.50_MB_(86.95%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public long maximumMedianSum(int[] nums) {
9+
int n = nums.length;
10+
Arrays.sort(nums);
11+
int m = n / 3;
12+
long sum = 0;
13+
for (int i = n - 2; i >= n - 2 * m; i = i - 2) {
14+
sum = sum + nums[i];
15+
}
16+
return sum;
17+
}
18+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
3627\. Maximum Median Sum of Subsequences of Size 3
2+
3+
Medium
4+
5+
You are given an integer array `nums` with a length divisible by 3.
6+
7+
You want to make the array empty in steps. In each step, you can select any three elements from the array, compute their **median**, and remove the selected elements from the array.
8+
9+
The **median** of an odd-length sequence is defined as the middle element of the sequence when it is sorted in non-decreasing order.
10+
11+
Return the **maximum** possible sum of the medians computed from the selected elements.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [2,1,3,2,1,3]
16+
17+
**Output:** 5
18+
19+
**Explanation:**
20+
21+
* In the first step, select elements at indices 2, 4, and 5, which have a median 3. After removing these elements, `nums` becomes `[2, 1, 2]`.
22+
* In the second step, select elements at indices 0, 1, and 2, which have a median 2. After removing these elements, `nums` becomes empty.
23+
24+
Hence, the sum of the medians is `3 + 2 = 5`.
25+
26+
**Example 2:**
27+
28+
**Input:** nums = [1,1,10,10,10,10]
29+
30+
**Output:** 20
31+
32+
**Explanation:**
33+
34+
* In the first step, select elements at indices 0, 2, and 3, which have a median 10. After removing these elements, `nums` becomes `[1, 10, 10]`.
35+
* In the second step, select elements at indices 0, 1, and 2, which have a median 10. After removing these elements, `nums` becomes empty.
36+
37+
Hence, the sum of the medians is `10 + 10 = 20`.
38+
39+
**Constraints:**
40+
41+
* <code>1 <= nums.length <= 5 * 10<sup>5</sup></code>
42+
* `nums.length % 3 == 0`
43+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package g3601_3700.s3628_maximum_number_of_subsequences_after_one_inserting;
2+
3+
// #Medium #Weekly_Contest_460 #2025_07_27_Time_12_ms_(100.00%)_Space_45.76_MB_(72.28%)
4+
5+
public class Solution {
6+
public long numOfSubsequences(String s) {
7+
long tc = 0;
8+
char[] chs = s.toCharArray();
9+
for (char c : chs) {
10+
tc += (c == 'T') ? 1 : 0;
11+
}
12+
long ls = 0;
13+
long cs = 0;
14+
long lcf = 0;
15+
long ctf = 0;
16+
long lct = 0;
17+
long ocg = 0;
18+
long tp = 0;
19+
for (char curr : chs) {
20+
long rt = tc - tp;
21+
long cg = ls * rt;
22+
ocg = (cg > ocg) ? cg : ocg;
23+
if (curr == 'L') {
24+
ls++;
25+
} else {
26+
if (curr == 'C') {
27+
cs++;
28+
lcf += ls;
29+
} else {
30+
if (curr == 'T') {
31+
lct += lcf;
32+
ctf += cs;
33+
tp++;
34+
}
35+
}
36+
}
37+
}
38+
long fcg = ls * (tc - tp);
39+
ocg = fcg > ocg ? fcg : ocg;
40+
long maxi = 0;
41+
long[] bo = {lcf, ctf, ocg};
42+
for (long op : bo) {
43+
maxi = op > maxi ? op : maxi;
44+
}
45+
return lct + maxi;
46+
}
47+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
3628\. Maximum Number of Subsequences After One Inserting
2+
3+
Medium
4+
5+
You are given a string `s` consisting of uppercase English letters.
6+
7+
You are allowed to insert **at most one** uppercase English letter at **any** position (including the beginning or end) of the string.
8+
9+
Return the **maximum** number of `"LCT"` subsequences that can be formed in the resulting string after **at most one insertion**.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "LMCT"
14+
15+
**Output:** 2
16+
17+
**Explanation:**
18+
19+
We can insert a `"L"` at the beginning of the string s to make `"LLMCT"`, which has 2 subsequences, at indices [0, 3, 4] and [1, 3, 4].
20+
21+
**Example 2:**
22+
23+
**Input:** s = "LCCT"
24+
25+
**Output:** 4
26+
27+
**Explanation:**
28+
29+
We can insert a `"L"` at the beginning of the string s to make `"LLCCT"`, which has 4 subsequences, at indices [0, 2, 4], [0, 3, 4], [1, 2, 4] and [1, 3, 4].
30+
31+
**Example 3:**
32+
33+
**Input:** s = "L"
34+
35+
**Output:** 0
36+
37+
**Explanation:**
38+
39+
Since it is not possible to obtain the subsequence `"LCT"` by inserting a single letter, the result is 0.
40+
41+
**Constraints:**
42+
43+
* <code>1 <= s.length <= 10<sup>5</sup></code>
44+
* `s` consists of uppercase English letters.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package g3601_3700.s3629_minimum_jumps_to_reach_end_via_prime_teleportation;
2+
3+
// #Medium #Weekly_Contest_460 #2025_07_27_Time_116_ms_(99.81%)_Space_76.00_MB_(67.96%)
4+
5+
import java.util.ArrayDeque;
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
9+
public class Solution {
10+
public int minJumps(int[] nums) {
11+
int n = nums.length;
12+
if (n == 1) {
13+
return 0;
14+
}
15+
int maxVal = 0;
16+
for (int v : nums) {
17+
maxVal = Math.max(maxVal, v);
18+
}
19+
boolean[] isPrime = sieve(maxVal);
20+
@SuppressWarnings("unchecked")
21+
ArrayList<Integer>[] posOfValue = new ArrayList[maxVal + 1];
22+
for (int i = 0; i < n; i++) {
23+
int v = nums[i];
24+
if (posOfValue[v] == null) {
25+
posOfValue[v] = new ArrayList<>();
26+
}
27+
posOfValue[v].add(i);
28+
}
29+
boolean[] primeProcessed = new boolean[maxVal + 1];
30+
int[] dist = new int[n];
31+
Arrays.fill(dist, -1);
32+
ArrayDeque<Integer> q = new ArrayDeque<>();
33+
q.add(0);
34+
dist[0] = 0;
35+
while (!q.isEmpty()) {
36+
int i = q.poll();
37+
int d = dist[i];
38+
if (i == n - 1) {
39+
return d;
40+
}
41+
if (i + 1 < n && dist[i + 1] == -1) {
42+
dist[i + 1] = d + 1;
43+
q.add(i + 1);
44+
}
45+
if (i - 1 >= 0 && dist[i - 1] == -1) {
46+
dist[i - 1] = d + 1;
47+
q.add(i - 1);
48+
}
49+
int v = nums[i];
50+
if (v <= maxVal && isPrime[v] && !primeProcessed[v]) {
51+
for (int mult = v; mult <= maxVal; mult += v) {
52+
ArrayList<Integer> list = posOfValue[mult];
53+
if (list != null) {
54+
for (int idx : list) {
55+
if (dist[idx] == -1) {
56+
dist[idx] = d + 1;
57+
q.add(idx);
58+
}
59+
}
60+
}
61+
}
62+
primeProcessed[v] = true;
63+
}
64+
}
65+
return -1;
66+
}
67+
68+
private boolean[] sieve(int n) {
69+
boolean[] prime = new boolean[n + 1];
70+
if (n >= 2) {
71+
Arrays.fill(prime, true);
72+
}
73+
if (n >= 0) {
74+
prime[0] = false;
75+
}
76+
if (n >= 1) {
77+
prime[1] = false;
78+
}
79+
for (int i = 2; (long) i * i <= n; i++) {
80+
if (prime[i]) {
81+
for (int j = i * i; j <= n; j += i) {
82+
prime[j] = false;
83+
}
84+
}
85+
}
86+
return prime;
87+
}
88+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
3629\. Minimum Jumps to Reach End via Prime Teleportation
2+
3+
Medium
4+
5+
You are given an integer array `nums` of length `n`.
6+
7+
You start at index 0, and your goal is to reach index `n - 1`.
8+
9+
From any index `i`, you may perform one of the following operations:
10+
11+
* **Adjacent Step**: Jump to index `i + 1` or `i - 1`, if the index is within bounds.
12+
* **Prime Teleportation**: If `nums[i]` is a prime number `p`, you may instantly jump to any index `j != i` such that `nums[j] % p == 0`.
13+
14+
Return the **minimum** number of jumps required to reach index `n - 1`.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [1,2,4,6]
19+
20+
**Output:** 2
21+
22+
**Explanation:**
23+
24+
One optimal sequence of jumps is:
25+
26+
* Start at index `i = 0`. Take an adjacent step to index 1.
27+
* At index `i = 1`, `nums[1] = 2` is a prime number. Therefore, we teleport to index `i = 3` as `nums[3] = 6` is divisible by 2.
28+
29+
Thus, the answer is 2.
30+
31+
**Example 2:**
32+
33+
**Input:** nums = [2,3,4,7,9]
34+
35+
**Output:** 2
36+
37+
**Explanation:**
38+
39+
One optimal sequence of jumps is:
40+
41+
* Start at index `i = 0`. Take an adjacent step to index `i = 1`.
42+
* At index `i = 1`, `nums[1] = 3` is a prime number. Therefore, we teleport to index `i = 4` since `nums[4] = 9` is divisible by 3.
43+
44+
Thus, the answer is 2.
45+
46+
**Example 3:**
47+
48+
**Input:** nums = [4,6,5,8]
49+
50+
**Output:** 3
51+
52+
**Explanation:**
53+
54+
* Since no teleportation is possible, we move through `0 → 1 → 2 → 3`. Thus, the answer is 3.
55+
56+
**Constraints:**
57+
58+
* <code>1 <= n == nums.length <= 10<sup>5</sup></code>
59+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g3601_3700.s3630_partition_array_for_maximum_xor_and_and;
2+
3+
// #Hard #Weekly_Contest_460 #2025_07_27_Time_77_ms_(100.00%)_Space_51.20_MB_(30.86%)
4+
5+
public class Solution {
6+
public long maximizeXorAndXor(int[] nums) {
7+
int n = nums.length;
8+
int full = 1 << n;
9+
int[] xorMask = new int[full];
10+
int[] andMask = new int[full];
11+
int[] orMask = new int[full];
12+
for (int mask = 1; mask < full; mask++) {
13+
int lb = mask & -mask;
14+
int i = Integer.numberOfTrailingZeros(lb);
15+
int prev = mask ^ lb;
16+
xorMask[mask] = xorMask[prev] ^ nums[i];
17+
andMask[mask] = prev == 0 ? nums[i] : andMask[prev] & nums[i];
18+
orMask[mask] = orMask[prev] | nums[i];
19+
}
20+
long best = 0;
21+
int all = full - 1;
22+
for (int b = 0; b < full; b++) {
23+
long andB = andMask[b];
24+
int rest = all ^ b;
25+
if (andB + 2L * orMask[rest] <= best) {
26+
continue;
27+
}
28+
for (int a = rest; ; a = (a - 1) & rest) {
29+
int c = rest ^ a;
30+
long sum = xorMask[a] + andB + xorMask[c];
31+
if (sum > best) {
32+
best = sum;
33+
}
34+
if (a == 0) {
35+
break;
36+
}
37+
}
38+
}
39+
return best;
40+
}
41+
}

0 commit comments

Comments
 (0)