Skip to content

Added tasks 3627-3630 #2024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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,18 @@
package g3601_3700.s3627_maximum_median_sum_of_subsequences_of_size_3;

// #Medium #Weekly_Contest_460 #2025_07_27_Time_22_ms_(100.00%)_Space_129.50_MB_(86.95%)

import java.util.Arrays;

public class Solution {
public long maximumMedianSum(int[] nums) {
int n = nums.length;
Arrays.sort(nums);
int m = n / 3;
long sum = 0;
for (int i = n - 2; i >= n - 2 * m; i = i - 2) {
sum = sum + nums[i];
}
return sum;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
3627\. Maximum Median Sum of Subsequences of Size 3

Medium

You are given an integer array `nums` with a length divisible by 3.

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.

The **median** of an odd-length sequence is defined as the middle element of the sequence when it is sorted in non-decreasing order.

Return the **maximum** possible sum of the medians computed from the selected elements.

**Example 1:**

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

**Output:** 5

**Explanation:**

* 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]`.
* In the second step, select elements at indices 0, 1, and 2, which have a median 2. After removing these elements, `nums` becomes empty.

Hence, the sum of the medians is `3 + 2 = 5`.

**Example 2:**

**Input:** nums = [1,1,10,10,10,10]

**Output:** 20

**Explanation:**

* 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]`.
* In the second step, select elements at indices 0, 1, and 2, which have a median 10. After removing these elements, `nums` becomes empty.

Hence, the sum of the medians is `10 + 10 = 20`.

**Constraints:**

* <code>1 <= nums.length <= 5 * 10<sup>5</sup></code>
* `nums.length % 3 == 0`
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package g3601_3700.s3628_maximum_number_of_subsequences_after_one_inserting;

// #Medium #Weekly_Contest_460 #2025_07_27_Time_12_ms_(100.00%)_Space_45.76_MB_(72.28%)

public class Solution {
public long numOfSubsequences(String s) {
long tc = 0;
char[] chs = s.toCharArray();
for (char c : chs) {
tc += (c == 'T') ? 1 : 0;
}
long ls = 0;
long cs = 0;
long lcf = 0;
long ctf = 0;
long lct = 0;
long ocg = 0;
long tp = 0;
for (char curr : chs) {
long rt = tc - tp;
long cg = ls * rt;
ocg = (cg > ocg) ? cg : ocg;
if (curr == 'L') {
ls++;
} else {
if (curr == 'C') {
cs++;
lcf += ls;
} else {
if (curr == 'T') {
lct += lcf;
ctf += cs;
tp++;
}
}
}
}
long fcg = ls * (tc - tp);
ocg = fcg > ocg ? fcg : ocg;
long maxi = 0;
long[] bo = {lcf, ctf, ocg};
for (long op : bo) {
maxi = op > maxi ? op : maxi;
}
return lct + maxi;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
3628\. Maximum Number of Subsequences After One Inserting

Medium

You are given a string `s` consisting of uppercase English letters.

You are allowed to insert **at most one** uppercase English letter at **any** position (including the beginning or end) of the string.

Return the **maximum** number of `"LCT"` subsequences that can be formed in the resulting string after **at most one insertion**.

**Example 1:**

**Input:** s = "LMCT"

**Output:** 2

**Explanation:**

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

**Example 2:**

**Input:** s = "LCCT"

**Output:** 4

**Explanation:**

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

**Example 3:**

**Input:** s = "L"

**Output:** 0

**Explanation:**

Since it is not possible to obtain the subsequence `"LCT"` by inserting a single letter, the result is 0.

**Constraints:**

* <code>1 <= s.length <= 10<sup>5</sup></code>
* `s` consists of uppercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package g3601_3700.s3629_minimum_jumps_to_reach_end_via_prime_teleportation;

// #Medium #Weekly_Contest_460 #2025_07_27_Time_116_ms_(99.81%)_Space_76.00_MB_(67.96%)

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;

public class Solution {
public int minJumps(int[] nums) {
int n = nums.length;
if (n == 1) {
return 0;
}
int maxVal = 0;
for (int v : nums) {
maxVal = Math.max(maxVal, v);
}
boolean[] isPrime = sieve(maxVal);
@SuppressWarnings("unchecked")
ArrayList<Integer>[] posOfValue = new ArrayList[maxVal + 1];
for (int i = 0; i < n; i++) {
int v = nums[i];
if (posOfValue[v] == null) {
posOfValue[v] = new ArrayList<>();
}
posOfValue[v].add(i);
}
boolean[] primeProcessed = new boolean[maxVal + 1];
int[] dist = new int[n];
Arrays.fill(dist, -1);
ArrayDeque<Integer> q = new ArrayDeque<>();
q.add(0);
dist[0] = 0;
while (!q.isEmpty()) {
int i = q.poll();
int d = dist[i];
if (i == n - 1) {
return d;
}
if (i + 1 < n && dist[i + 1] == -1) {
dist[i + 1] = d + 1;
q.add(i + 1);
}
if (i - 1 >= 0 && dist[i - 1] == -1) {
dist[i - 1] = d + 1;
q.add(i - 1);
}
int v = nums[i];
if (v <= maxVal && isPrime[v] && !primeProcessed[v]) {
for (int mult = v; mult <= maxVal; mult += v) {
ArrayList<Integer> list = posOfValue[mult];
if (list != null) {
for (int idx : list) {
if (dist[idx] == -1) {
dist[idx] = d + 1;
q.add(idx);
}
}
}
}
primeProcessed[v] = true;
}
}
return -1;
}

private boolean[] sieve(int n) {
boolean[] prime = new boolean[n + 1];
if (n >= 2) {
Arrays.fill(prime, true);
}
if (n >= 0) {
prime[0] = false;
}
if (n >= 1) {
prime[1] = false;
}
for (int i = 2; (long) i * i <= n; i++) {
if (prime[i]) {
for (int j = i * i; j <= n; j += i) {
prime[j] = false;
}
}
}
return prime;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
3629\. Minimum Jumps to Reach End via Prime Teleportation

Medium

You are given an integer array `nums` of length `n`.

You start at index 0, and your goal is to reach index `n - 1`.

From any index `i`, you may perform one of the following operations:

* **Adjacent Step**: Jump to index `i + 1` or `i - 1`, if the index is within bounds.
* **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`.

Return the **minimum** number of jumps required to reach index `n - 1`.

**Example 1:**

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

**Output:** 2

**Explanation:**

One optimal sequence of jumps is:

* Start at index `i = 0`. Take an adjacent step to index 1.
* 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.

Thus, the answer is 2.

**Example 2:**

**Input:** nums = [2,3,4,7,9]

**Output:** 2

**Explanation:**

One optimal sequence of jumps is:

* Start at index `i = 0`. Take an adjacent step to index `i = 1`.
* 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.

Thus, the answer is 2.

**Example 3:**

**Input:** nums = [4,6,5,8]

**Output:** 3

**Explanation:**

* Since no teleportation is possible, we move through `0 → 1 → 2 → 3`. Thus, the answer is 3.

**Constraints:**

* <code>1 <= n == nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package g3601_3700.s3630_partition_array_for_maximum_xor_and_and;

// #Hard #Array #Math #Greedy #Enumeration #Weekly_Contest_460
// #2025_07_31_Time_82_ms_(96.35%)_Space_50.76_MB_(39.58%)

public class Solution {
public long maximizeXorAndXor(int[] nums) {
int n = nums.length;
int full = 1 << n;
int[] xorMask = new int[full];
int[] andMask = new int[full];
int[] orMask = new int[full];
for (int mask = 1; mask < full; mask++) {
int lb = mask & -mask;
int i = Integer.numberOfTrailingZeros(lb);
int prev = mask ^ lb;
xorMask[mask] = xorMask[prev] ^ nums[i];
andMask[mask] = prev == 0 ? nums[i] : andMask[prev] & nums[i];
orMask[mask] = orMask[prev] | nums[i];
}
long best = 0;
int all = full - 1;
for (int b = 0; b < full; b++) {
long andB = andMask[b];
int rest = all ^ b;
if (andB + 2L * orMask[rest] <= best) {
continue;
}
for (int a = rest; ; a = (a - 1) & rest) {
int c = rest ^ a;
long sum = xorMask[a] + andB + xorMask[c];
if (sum > best) {
best = sum;
}
if (a == 0) {
break;
}
}
}
return best;
}
}
Loading
Loading