Skip to content

Commit 8aeb79b

Browse files
authored
Added task 3824
1 parent 82eb7b1 commit 8aeb79b

3 files changed

Lines changed: 190 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g3801_3900.s3824_minimum_k_to_reduce_array_within_limit;
2+
3+
// #Medium #Array #Binary_Search #Staff #Biweekly_Contest_175
4+
// #2026_06_09_Time_16_ms_(100.00%)_Space_130.89_MB_(86.74%)
5+
6+
public class Solution {
7+
public int minimumK(int[] nums) {
8+
int n = nums.length;
9+
int mx = 0;
10+
for (int x : nums) {
11+
mx = Math.max(mx, x);
12+
}
13+
long total = 0;
14+
for (int x : nums) {
15+
total += x;
16+
}
17+
int left = Math.max((int) Math.ceil(Math.sqrt(n)), (int) Math.ceil(Math.cbrt(total))) - 1;
18+
int right = (int) Math.ceil(Math.sqrt(nonPositive(nums, left + 1)));
19+
while (left + 1 < right) {
20+
int k = (left + right) / 2;
21+
if (nonPositive(nums, k) <= (long) k * k) {
22+
right = k;
23+
} else {
24+
left = k;
25+
}
26+
}
27+
return left + 1;
28+
}
29+
30+
private long nonPositive(int[] nums, int k) {
31+
long sum = nums.length;
32+
for (int x : nums) {
33+
sum += (x - 1) / k;
34+
}
35+
return sum;
36+
}
37+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
3824\. Minimum K to Reduce Array Within Limit
2+
3+
Medium
4+
5+
You are given a **positive** integer array `nums`.
6+
7+
For a positive integer `k`, define `nonPositive(nums, k)` as the **minimum** number of **operations** needed to make every element of `nums` **non-positive**. In one operation, you can choose an index `i` and reduce `nums[i]` by `k`.
8+
9+
Return an integer denoting the **minimum** value of `k` such that <code>nonPositive(nums, k) <= k<sup>2</sup></code>.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [3,7,5]
14+
15+
**Output:** 3
16+
17+
**Explanation:**
18+
19+
When `k = 3`, <code>nonPositive(nums, k) = 6 <= k<sup>2</sup></code>.
20+
21+
* Reduce `nums[0] = 3` one time. `nums[0]` becomes `3 - 3 = 0`.
22+
* Reduce `nums[1] = 7` three times. `nums[1]` becomes `7 - 3 - 3 - 3 = -2`.
23+
* Reduce `nums[2] = 5` two times. `nums[2]` becomes `5 - 3 - 3 = -1`.
24+
25+
**Example 2:**
26+
27+
**Input:** nums = [1]
28+
29+
**Output:** 1
30+
31+
**Explanation:**
32+
33+
When `k = 1`, <code>nonPositive(nums, k) = 1 <= k<sup>2</sup></code>.
34+
35+
* Reduce `nums[0] = 1` one time. `nums[0]` becomes `1 - 1 = 0`.
36+
37+
**Constraints:**
38+
39+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
40+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package g3801_3900.s3824_minimum_k_to_reduce_array_within_limit;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void minimumK() {
11+
assertThat(new Solution().minimumK(new int[] {3, 7, 5}), equalTo(3));
12+
}
13+
14+
@Test
15+
void minimumK2() {
16+
assertThat(new Solution().minimumK(new int[] {1}), equalTo(1));
17+
}
18+
19+
@Test
20+
void minimumK3() {
21+
// Single element of value 10: ceil(10/k) <= k => k >= 4 (since 10/4=3 ceil 3, +1 length=4
22+
// <= 16)
23+
// Actually: nonPositive = 1 + ceil((10-1)/k+? ); test with simple expected
24+
assertThat(new Solution().minimumK(new int[] {10}), equalTo(3));
25+
}
26+
27+
@Test
28+
void minimumK4() {
29+
// Array of all ones: sum = n, each (1-1)/k = 0, so nonPositive = n. Need n <= k*k
30+
// n=4 -> k>=2
31+
assertThat(new Solution().minimumK(new int[] {1, 1, 1, 1}), equalTo(2));
32+
}
33+
34+
@Test
35+
void minimumK5() {
36+
// n=9, need k*k >= 9, k>=3
37+
assertThat(new Solution().minimumK(new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1}), equalTo(3));
38+
}
39+
40+
@Test
41+
void minimumK6() {
42+
assertThat(new Solution().minimumK(new int[] {1, 1}), equalTo(2));
43+
}
44+
45+
@Test
46+
void minimumK7() {
47+
// Two elements of 1000000
48+
// n=2, need: 2 + 2*ceil(999999/k) <= k*k
49+
// k=126: 2 + 2*ceil(999999/126)=2+2*7937=15876, k*k=15876 -> exactly equal
50+
assertThat(new Solution().minimumK(new int[] {1000000, 1000000}), equalTo(126));
51+
}
52+
53+
@Test
54+
void minimumK8() {
55+
assertThat(new Solution().minimumK(new int[] {2, 3, 4, 5}), equalTo(3));
56+
}
57+
58+
@Test
59+
void minimumK9() {
60+
assertThat(new Solution().minimumK(new int[] {5, 5, 5}), equalTo(3));
61+
}
62+
63+
@Test
64+
void minimumK10() {
65+
int[] nums = new int[100];
66+
for (int i = 0; i < 100; i++) {
67+
nums[i] = 1;
68+
}
69+
// n=100, need k*k >= 100, k>=10
70+
assertThat(new Solution().minimumK(nums), equalTo(10));
71+
}
72+
73+
@Test
74+
void minimumK11() {
75+
int[] nums = new int[50];
76+
for (int i = 0; i < 50; i++) {
77+
nums[i] = i + 1;
78+
}
79+
assertThat(new Solution().minimumK(nums), equalTo(12));
80+
}
81+
82+
@Test
83+
void minimumK12() {
84+
// n=1, val=2: nonPositive=1+ceil(1/k). Need <= k*k. k=2: 1+1=2 <=4 yes. k=1: 1+1=2 <=1? No
85+
assertThat(new Solution().minimumK(new int[] {2}), equalTo(2));
86+
}
87+
88+
@Test
89+
void minimumK13() {
90+
assertThat(new Solution().minimumK(new int[] {100, 200, 300, 400, 500}), equalTo(12));
91+
}
92+
93+
@Test
94+
void minimumK14() {
95+
// single element with max int-like value
96+
int[] nums = {1000000};
97+
// need 1 + ceil(999999/k) <= k*k
98+
// k=100: 1 + 10000 = 10001, k*k=10000 -> no
99+
// k=101: 1 + ceil(999999/101)=1+9901=9902, k*k=10201 -> yes
100+
assertThat(new Solution().minimumK(nums), equalTo(100));
101+
}
102+
103+
@Test
104+
void minimumK15() {
105+
// n=3, need k*k >= 3, k>=2
106+
assertThat(new Solution().minimumK(new int[] {1, 1, 1}), equalTo(2));
107+
}
108+
109+
@Test
110+
void minimumK16() {
111+
assertThat(new Solution().minimumK(new int[] {1, 2}), equalTo(2));
112+
}
113+
}

0 commit comments

Comments
 (0)