Skip to content

Added tasks 3627-3630 #866

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

Merged
merged 3 commits into from
Aug 3, 2025
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,18 @@
package g3601_3700.s3627_maximum_median_sum_of_subsequences_of_size_3

// #Medium #Weekly_Contest_460 #2025_07_27_Time_46_ms_(91.67%)_Space_133.87_MB_(16.67%)

class Solution {
fun maximumMedianSum(nums: IntArray): Long {
nums.sort()
var i = 0
var j = nums.size
var sum = 0L
while (i < j) {
sum += nums[j - 2]
j = j - 2
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_13_ms_(100.00%)_Space_48.00_MB_(75.00%)

class Solution {
fun numOfSubsequences(s: String): Long {
var tc: Long = 0
val chs = s.toCharArray()
for (c in chs) {
tc += (if (c == 'T') 1 else 0).toLong()
}
var ls: Long = 0
var cs: Long = 0
var lcf: Long = 0
var ctf: Long = 0
var lct: Long = 0
var ocg: Long = 0
var tp: Long = 0
for (curr in chs) {
val rt = tc - tp
val cg = ls * rt
ocg = if (cg > ocg) cg else ocg
if (curr == 'L') {
ls++
} else {
if (curr == 'C') {
cs++
lcf += ls
} else {
if (curr == 'T') {
lct += lcf
ctf += cs
tp++
}
}
}
}
val fcg = ls * (tc - tp)
ocg = if (fcg > ocg) fcg else ocg
var maxi: Long = 0
val bo = longArrayOf(lcf, ctf, ocg)
for (op in bo) {
maxi = if (op > maxi) op else 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,87 @@
package g3601_3700.s3629_minimum_jumps_to_reach_end_via_prime_teleportation

// #Medium #Weekly_Contest_460 #2025_07_27_Time_406_ms_(100.00%)_Space_153.64_MB_(100.00%)

import java.util.ArrayDeque
import kotlin.math.max

class Solution {
fun minJumps(nums: IntArray): Int {
val n = nums.size
if (n == 1) {
return 0
}
var maxVal = 0
for (v in nums) {
maxVal = max(maxVal, v)
}
val isPrime = sieve(maxVal)
val posOfValue: Array<ArrayList<Int>> = Array<ArrayList<Int>>(maxVal + 1) { ArrayList<Int>() }
for (i in 0..<n) {
val v = nums[i]
posOfValue[v].add(i)
}
val primeProcessed = BooleanArray(maxVal + 1)
val dist = IntArray(n)
dist.fill(-1)
val q = ArrayDeque<Int>()
q.add(0)
dist[0] = 0
while (q.isNotEmpty()) {
val i: Int = q.poll()!!
val 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)
}
val v = nums[i]
if (v <= maxVal && isPrime[v] && !primeProcessed[v]) {
var mult = v
while (mult <= maxVal) {
val list = posOfValue[mult]
for (idx in list) {
if (dist[idx] == -1) {
dist[idx] = d + 1
q.add(idx)
}
}
mult += v
}
primeProcessed[v] = true
}
}
return -1
}

private fun sieve(n: Int): BooleanArray {
val prime = BooleanArray(n + 1)
if (n >= 2) {
prime.fill(true)
}
if (n >= 0) {
prime[0] = false
}
if (n >= 1) {
prime[1] = false
}
var i = 2
while (i.toLong() * i <= n) {
if (prime[i]) {
var j = i * i
while (j <= n) {
prime[j] = false
j += i
}
}
i++
}
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,44 @@
package g3601_3700.s3630_partition_array_for_maximum_xor_and_and

// #Hard #Array #Math #Greedy #Enumeration #Weekly_Contest_460
// #2025_08_03_Time_57_ms_(100.00%)_Space_51.81_MB_(100.00%)

class Solution {
fun maximizeXorAndXor(nums: IntArray): Long {
val n = nums.size
val full = 1 shl n
val xorMask = IntArray(full)
val andMask = IntArray(full)
val orMask = IntArray(full)
for (mask in 1..<full) {
val lb = mask and -mask
val i = Integer.numberOfTrailingZeros(lb)
val prev = mask xor lb
xorMask[mask] = xorMask[prev] xor nums[i]
andMask[mask] = if (prev == 0) nums[i] else andMask[prev] and nums[i]
orMask[mask] = orMask[prev] or nums[i]
}
var best: Long = 0
val all = full - 1
for (b in 0..<full) {
val andB = andMask[b].toLong()
val rest = all xor b
if (andB + 2L * orMask[rest] <= best) {
continue
}
var a = rest
while (true) {
val c = rest xor a
val sum = xorMask[a] + andB + xorMask[c]
if (sum > best) {
best = sum
}
if (a == 0) {
break
}
a = (a - 1) and rest
}
}
return best
}
}
Loading
Loading