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
60 changes: 60 additions & 0 deletions src/main/java/g3801_3900/s3899_angles_of_a_triangle/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package g3801_3900.s3899_angles_of_a_triangle;

// #Medium #Array #Math #Geometry #Senior #Weekly_Contest_497
// #2026_09_17_Time_1_ms_(100.00%)_Space_49.33_MB_(42.62%)

public class Solution {
public double[] internalAngles(int[] sides) {
if (sides[0] + sides[1] <= sides[2]
|| sides[1] + sides[2] <= sides[0]
|| sides[0] + sides[2] <= sides[1]) {
return new double[0];
}
double[] angle = new double[sides.length];
angle[0] =
Math.toDegrees(
Math.acos(
(double)
(sides[1] * sides[1]
+ sides[2] * sides[2]
- sides[0] * sides[0])
/ (2 * sides[1] * sides[2])));
angle[1] =
Math.toDegrees(
Math.acos(
(double)
(sides[0] * sides[0]
+ sides[2] * sides[2]
- sides[1] * sides[1])
/ (2 * sides[0] * sides[2])));
angle[2] =
Math.toDegrees(
Math.acos(
(double)
(sides[1] * sides[1]
+ sides[0] * sides[0]
- sides[2] * sides[2])
/ (2 * sides[1] * sides[0])));
double max = angle[0];
double mid = 0;
double min = 0;
for (int i = 1; i < angle.length; i++) {
if (angle[i] > max) {
min = mid;
mid = max;
max = angle[i];
} else {
if (angle[i] > mid) {
min = mid;
mid = angle[i];
} else {
min = angle[i];
}
}
}
angle[0] = min;
angle[1] = mid;
angle[2] = max;
return angle;
}
}
36 changes: 36 additions & 0 deletions src/main/java/g3801_3900/s3899_angles_of_a_triangle/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
3899\. Angles of a Triangle

Medium

You are given a positive integer array `sides` of length 3.

Determine if there exists a triangle with **positive** area whose three side lengths are given by the elements of `sides`.

If such a triangle exists, return an array of three floating-point numbers representing its internal angles (in **degrees**), **sorted** in **non-decreasing** order. Otherwise, return an empty array.

Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.

**Example 1:**

**Input:** sides = [3,4,5]

**Output:** [36.86990,53.13010,90.00000]

**Explanation:**

You can form a right-angled triangle with side lengths 3, 4, and 5. The internal angles of this triangle are approximately 36.869897646, 53.130102354, and 90 degrees respectively.

**Example 2:**

**Input:** sides = [2,4,2]

**Output:** []

**Explanation:**

You cannot form a triangle with positive area using side lengths 2, 4, and 2.

**Constraints:**

* `sides.length == 3`
* `1 <= sides[i] <= 1000`
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package g3801_3900.s3900_longest_balanced_substring_after_one_swap;

// #Medium #String #Hash_Table #Prefix_Sum #Staff #Weekly_Contest_497
// #2026_09_17_Time_15_ms_(100.00%)_Space_48.30_MB_(97.14%)

import java.util.Arrays;

public class Solution {
public int longestBalanced(String s) {
char[] arr = s.toCharArray();
int n = arr.length;
int bal = n + 1;
int ans = 0;
int[] nextIndex = new int[n];
int[] balIndex = new int[2 * n + 3];
Arrays.fill(balIndex, n + 1);
for (int i = n - 1; i >= 0; i--) {
bal += (('0' ^ arr[i]) << 1) - 1;
nextIndex[i] = balIndex[bal];
balIndex[bal] = i;
}
if (bal == n + 1) {
return n;
}
int zeros = (2 * n + 1 - bal) / 2;
int maxLength = 2 * Math.min(zeros, n - zeros);
for (int i = 1; i <= n && ans < maxLength; i++) {
bal += (('1' ^ arr[i - 1]) << 1) - 1;
if (i - balIndex[bal] > ans) {
ans = i - balIndex[bal];
}
if (balIndex[bal - 2] < i - maxLength) {
balIndex[bal - 2] = nextIndex[balIndex[bal - 2]];
}
if (i - balIndex[bal - 2] > ans) {
ans = i - balIndex[bal - 2];
}
if (balIndex[bal + 2] < i - maxLength) {
balIndex[bal + 2] = nextIndex[balIndex[bal + 2]];
}
if (i - balIndex[bal + 2] > ans) {
ans = i - balIndex[bal + 2];
}
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
3900\. Longest Balanced Substring After One Swap

Medium

You are given a binary string `s` consisting only of characters `'0'` and `'1'`.

A string is **balanced** if it contains an **equal** number of `'0'`s and `'1'`s.

You can perform **at most one** swap between any two characters in `s`. Then, you select a **balanced** substring from `s`.

Return an integer representing the **maximum** length of the **balanced** substring you can select.

**Example 1:**

**Input:** s = "100001"

**Output:** 4

**Explanation:**

* Swap <code>"10<ins>**0**</ins>00<ins>**1**</ins>"</code>. The string becomes `"101000"`.
* Select the substring <code>"<ins>**1010**</ins>00"</code>, which is balanced because it has two `'0'`s and two `'1'`s.

**Example 2:**

**Input:** s = "111"

**Output:** 0

**Explanation:**

* Choose not to perform any swaps.
* Select the empty substring, which is balanced because it has zero `'0'`s and zero `'1'`s.

**Constraints:**

* <code>1 <= s.length <= 10<sup>5</sup></code>
* `s` consists only of the characters `'0'` and `'1'`.
103 changes: 103 additions & 0 deletions src/main/java/g3901_4000/s3901_good_subsequence_queries/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package g3901_4000.s3901_good_subsequence_queries;

// #Hard #Array #Math #Segment_Tree #Number_Theory #Weekly_Contest_497 #Principal
// #2026_09_17_Time_20_ms_(100.00%)_Space_133.27_MB_(34.15%)

public class Solution {
private int[] tree;
private int validCount = 0;

public int countGoodSubseq(int[] nums, int p, int[][] queries) {
int n = nums.length;
// 1. Iterative Segment Tree only needs 2n space
tree = new int[2 * n];
// Build the leaves of the tree and get initial count
for (int i = 0; i < n; i++) {
if (nums[i] % p == 0) {
tree[n + i] = nums[i];
validCount++;
}
}
// Build the internal nodes bottom-up
for (int i = n - 1; i > 0; --i) {
// tree[i << 1] is the left child, tree[i << 1 | 1] is the right child
tree[i] = gcd(tree[i << 1], tree[i << 1 | 1]);
}
int ans = 0;
for (int[] q : queries) {
int idx = q[0];
int value = q[1];
// 2. O(1) tracking for validCount (No segment tree needed for this!)
boolean wasValid = (nums[idx] % p == 0);
boolean isValid = (value % p == 0);
if (wasValid && !isValid) {
validCount--;
}
if (!wasValid && isValid) {
validCount++;
}

// Update the original array to keep track of the old values
nums[idx] = value;
// Point update for the Iterative Tree
tree[idx + n] = isValid ? value : 0;
// Climb up the tree using bitwise shifts (i >>= 1 means i = i / 2)
for (int i = idx + n; i > 1; i >>= 1) {
tree[i >> 1] = gcd(tree[i], tree[i ^ 1]);
}
// 3. The logic check (tree[1] is ALWAYS the root in an iterative tree)
if (tree[1] == p) {
if (validCount < n) {
ans++;
} else {
// validCount == n (Every element is a multiple of p)
if (n > 20) {
// THE O(1) MATH BYPASS!
ans++;
} else {
// Only run this heavy check if n is 20 or smaller
boolean flag = false;
for (int i = 0; i < n; i++) {
int leftGcd = query(0, i - 1, n);
int rightGcd = query(i + 1, n - 1, n);
if (gcd(leftGcd, rightGcd) == p) {
flag = true;
break;
}
}
if (flag) {
ans++;
}
}
}
}
}
return ans;
}

// Iterative Range Query [l, r] inclusive
private int query(int l, int r, int n) {
if (l > r) {
return 0;
}
int res = 0;
for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
if ((l & 1) == 1) {
res = gcd(res, tree[l++]);
}
if ((r & 1) == 1) {
res = gcd(res, tree[--r]);
}
}
return res;
}

private int gcd(int a, int b) {
while (b > 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
}
74 changes: 74 additions & 0 deletions src/main/java/g3901_4000/s3901_good_subsequence_queries/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
3901\. Good Subsequence Queries

Hard

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

A **non-empty subsequence** of `nums` is called **good** if:

* Its length is **strictly less** than `n`.
* The **greatest common divisor (GCD)** of its elements is **exactly** `p`.

You are also given a 2D integer array `queries` of length `q`, where each <code>queries[i] = [ind<sub>i</sub>, val<sub>i</sub>]</code> indicates that you should update <code>nums[ind<sub>i</sub>]</code> to <code>val<sub>i</sub></code>.

After each query, determine whether there exists **any good subsequence** in the current array.

Return the **number** of queries for which a **good subsequence** exists.

The term `gcd(a, b)` denotes the **greatest common divisor** of `a` and `b`.

**Example 1:**

**Input:** nums = [4,8,12,16], p = 2, queries = [[0,3],[2,6]]

**Output:** 1

**Explanation:**

| i | `[ind_i, val_i]` | Operation | Updated `nums` | Any good Subsequence |
|---:|---|---|---|---|
| 0 | `[0, 3]` | Update `nums[0]` to `3` | `[3, 8, 12, 16]` | No, as no subsequence has GCD exactly `p = 2` |
| 1 | `[2, 6]` | Update `nums[2]` to `6` | `[3, 8, 6, 16]` | Yes, subsequence `[8, 6]` has GCD exactly `p = 2` |


Thus, the answer is 1.

**Example 2:**

**Input:** nums = [4,5,7,8], p = 3, queries = [[0,6],[1,9],[2,3]]

**Output:** 2

**Explanation:**

| i | `[ind_i, val_i]` | Operation | Updated `nums` | Any good Subsequence |
|---:|---|---|---|---|
| 0 | `[0, 6]` | Update `nums[0]` to `6` | `[6, 5, 7, 8]` | No, as no subsequence has GCD exactly `p = 3` |
| 1 | `[1, 9]` | Update `nums[1]` to `9` | `[6, 9, 7, 8]` | Yes, subsequence `[6, 9]` has GCD exactly `p = 3` |
| 2 | `[2, 3]` | Update `nums[2]` to `3` | `[6, 9, 3, 8]` | Yes, subsequence `[6, 9, 3]` has GCD exactly `p = 3` |

Thus, the answer is 2.

**Example 3:**

**Input:** nums = [5,7,9], p = 2, queries = [[1,4],[2,8]]

**Output:** 0

**Explanation:**

| i | `[ind_i, val_i]` | Operation | Updated `nums` | Any good Subsequence |
|---:|---|---|---|---|
| 0 | `[1, 4]` | Update `nums[1]` to `4` | `[5, 4, 9]` | No, as no subsequence has GCD exactly `p = 2` |
| 1 | `[2, 8]` | Update `nums[2]` to `8` | `[5, 4, 8]` | No, as no subsequence has GCD exactly `p = 2` |

Thus, the answer is 0.

**Constraints:**

* <code>2 <= n == nums.length <= 5 * 10<sup>4</sup></code>
* <code>1 <= nums[i] <= 5 * 10<sup>4</sup></code>
* <code>1 <= queries.length <= 5 * 10<sup>4</sup></code>
* <code>queries[i] = [ind<sub>i</sub>, val<sub>i</sub>]</code>
* <code>1 <= val<sub>i</sub>, p <= 5 * 10<sup>4</sup></code>
* <code>0 <= ind<sub>i</sub> <= n - 1</code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package g3901_4000.s3903_smallest_stable_index_i;

// #Easy #Array #Prefix_Sum #Mid_Level #Weekly_Contest_498
// #2026_09_17_Time_1_ms_(99.37%)_Space_46.08_MB_(92.83%)

public class Solution {
public int firstStableIndex(int[] nums, int k) {
int n = nums.length;
int[] mini = new int[n];
int mint = Integer.MAX_VALUE;
for (int i = n - 1; i >= 0; i--) {
if (nums[i] < mint) {
mint = nums[i];
}
mini[i] = mint;
}
int maxt = 0;
for (int i = 0; i < n; i++) {
if (nums[i] > maxt) {
maxt = nums[i];
}
if (maxt - mini[i] <= k) {
return i;
}
}
return -1;
}
}
Loading