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

// #Easy #Array #Hash_Table #Sorting #Weekly_Contest_474
// #2025_11_05_Time_2_ms_(94.89%)_Space_46.54_MB_(95.16%)

import java.util.ArrayList;
import java.util.List;

public class Solution {
public List<Integer> findMissingElements(int[] nums) {
int maxi = 0;
int mini = 101;
List<Integer> list = new ArrayList<>();
boolean[] array = new boolean[101];
for (int num : nums) {
array[num] = true;
if (maxi < num) {
maxi = num;
}
if (mini > num) {
mini = num;
}
}
for (int index = mini + 1; index < maxi; index++) {
if (array[index]) {
continue;
}
list.add(index);
}
return list;
}
}
46 changes: 46 additions & 0 deletions src/main/java/g3701_3800/s3731_find_missing_elements/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
3731\. Find Missing Elements

Easy

You are given an integer array `nums` consisting of **unique** integers.

Originally, `nums` contained **every integer** within a certain range. However, some integers might have gone **missing** from the array.

The **smallest** and **largest** integers of the original range are still present in `nums`.

Return a **sorted** list of all the missing integers in this range. If no integers are missing, return an **empty** list.

**Example 1:**

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

**Output:** [3]

**Explanation:**

The smallest integer is 1 and the largest is 5, so the full range should be `[1,2,3,4,5]`. Among these, only 3 is missing.

**Example 2:**

**Input:** nums = [7,8,6,9]

**Output:** []

**Explanation:**

The smallest integer is 6 and the largest is 9, so the full range is `[6,7,8,9]`. All integers are already present, so no integer is missing.

**Example 3:**

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

**Output:** [2,3,4]

**Explanation:**

The smallest integer is 1 and the largest is 5, so the full range should be `[1,2,3,4,5]`. The missing integers are 2, 3, and 4.

**Constraints:**

* `2 <= nums.length <= 100`
* `1 <= nums[i] <= 100`
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package g3701_3800.s3732_maximum_product_of_three_elements_after_one_replacement;

// #Medium #Array #Math #Sorting #Greedy #Weekly_Contest_474
// #2025_11_05_Time_4_ms_(95.32%)_Space_97.32_MB_(28.84%)

public class Solution {
public long maxProduct(int[] nums) {
long a = 0;
long b = 0;
for (int x : nums) {
long ax = Math.abs(x);
if (ax >= a) {
b = a;
a = ax;
} else if (ax > b) {
b = ax;
}
}
return 100000L * a * b;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
3732\. Maximum Product of Three Elements After One Replacement

Medium

You are given an integer array `nums`.

You **must** replace **exactly one** element in the array with **any** integer value in the range <code>[-10<sup>5</sup>, 10<sup>5</sup>]</code> (inclusive).

After performing this single replacement, determine the **maximum possible product** of **any three** elements at **distinct indices** from the modified array.

Return an integer denoting the **maximum product** achievable.

**Example 1:**

**Input:** nums = [-5,7,0]

**Output:** 3500000

**Explanation:**

Replacing 0 with -10<sup>5</sup> gives the array <code>[-5, 7, -10<sup>5</sup>]</code>, which has a product <code>(-5) * 7 * (-10<sup>5</sup>) = 3500000</code>. The maximum product is 3500000.

**Example 2:**

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

**Output:** 1200000

**Explanation:**

Two ways to achieve the maximum product include:

* `[-4, -2, -3]` → replace -2 with 10<sup>5</sup> → product = <code>(-4) * 10<sup>5</sup> * (-3) = 1200000</code>.
* `[-4, -1, -3]` → replace -1 with 10<sup>5</sup> → product = <code>(-4) * 10<sup>5</sup> * (-3) = 1200000</code>.

The maximum product is 1200000.

**Example 3:**

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

**Output:** 0

**Explanation:**

There is no way to replace an element with another integer and not have a 0 in the array. Hence, the product of all three elements will always be 0, and the maximum product is 0.

**Constraints:**

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

// #Medium #Math #Binary_Search #Weekly_Contest_474
// #2025_11_05_Time_1_ms_(100.00%)_Space_44.16_MB_(55.61%)

public class Solution {
private boolean pos(long k, long n1, long n2, int p1, int p2, long lVal) {
long kP1 = k / p1;
long kP2 = k / p2;
long kL = k / lVal;
long s1 = kP2 - kL;
long s2 = kP1 - kL;
long sB = k - kP1 - kP2 + kL;
long w1 = Math.max(0L, n1 - s1);
long w2 = Math.max(0L, n2 - s2);
return (w1 + w2) <= sB;
}

private long findGcd(long a, long b) {
if (b == 0) {
return a;
}
return findGcd(b, a % b);
}

public long minimumTime(int[] d, int[] r) {
long n1 = d[0];
long n2 = d[1];
int p1 = r[0];
int p2 = r[1];
long g = findGcd(p1, p2);
long l = (long) p1 * p2 / g;
long lo = n1 + n2;
long hi = (n1 + n2) * Math.max(p1, p2);
long res = hi;
while (lo <= hi) {
long mid = lo + (hi - lo) / 2;
if (pos(mid, n1, n2, p1, p2, l)) {
res = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
3733\. Minimum Time to Complete All Deliveries

Medium

You are given two integer arrays of size 2: <code>d = [d<sub>1</sub>, d<sub>2</sub>]</code> and <code>r = [r<sub>1</sub>, r<sub>2</sub>]</code>.

Two delivery drones are tasked with completing a specific number of deliveries. Drone `i` must complete <code>d<sub>i</sub></code> deliveries.

Each delivery takes **exactly** one hour and **only one** drone can make a delivery at any given hour.

Additionally, both drones require recharging at specific intervals during which they cannot make deliveries. Drone `i` must recharge every <code>r<sub>i</sub></code> hours (i.e. at hours that are multiples of <code>r<sub>i</sub></code>).

Return an integer denoting the **minimum** total time (in hours) required to complete all deliveries.

**Example 1:**

**Input:** d = [3,1], r = [2,3]

**Output:** 5

**Explanation:**

* The first drone delivers at hours 1, 3, 5 (recharges at hours 2, 4).
* The second drone delivers at hour 2 (recharges at hour 3).

**Example 2:**

**Input:** d = [1,3], r = [2,2]

**Output:** 7

**Explanation:**

* The first drone delivers at hour 3 (recharges at hours 2, 4, 6).
* The second drone delivers at hours 1, 5, 7 (recharges at hours 2, 4, 6).

**Example 3:**

**Input:** d = [2,1], r = [3,4]

**Output:** 3

**Explanation:**

* The first drone delivers at hours 1, 2 (recharges at hour 3).
* The second drone delivers at hour 3.

**Constraints:**

* <code>d = [d<sub>1</sub>, d<sub>2</sub>]</code>
* <code>1 <= d<sub>i</sub> <= 10<sup>9</sup></code>
* <code>r = [r<sub>1</sub>, r<sub>2</sub>]</code>
* <code>2 <= r<sub>i</sub> <= 3 * 10<sup>4</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package g3701_3800.s3734_lexicographically_smallest_palindromic_permutation_greater_than_target;

// #Hard #String #Two_Pointers #Enumeration #Weekly_Contest_474
// #2025_11_05_Time_2_ms_(100.00%)_Space_46.34_MB_(84.73%)

import java.util.Arrays;

public class Solution {
boolean func(int i, String target, char[] ans, int l, int r, int[] freq, boolean end) {
if (l > r) {
return new String(ans).compareTo(target) > 0;
}
if (l == r) {
char left = '#';
for (int k = 0; k < 26; k++) {
if (freq[k] > 0) {
left = (char) (k + 'a');
}
}
freq[left - 'a']--;
ans[l] = left;
if (func(i + 1, target, ans, l + 1, r - 1, freq, end)) {
return true;
}
freq[left - 'a']++;
ans[l] = '#';
return false;
}
if (end) {
for (int j = 0; j < 26; j++) {
if (freq[j] > 1) {
freq[j] -= 2;
ans[l] = ans[r] = (char) (j + 'a');
if (func(i + 1, target, ans, l + 1, r - 1, freq, end)) {
return true;
}
ans[l] = ans[r] = '#';
freq[j] += 2;
}
}
return false;
}
char curr = target.charAt(i);
char next = '1';
for (int k = target.charAt(i) - 'a' + 1; k < 26; k++) {
if (freq[k] > 1) {
next = (char) (k + 'a');
break;
}
}
if (freq[curr - 'a'] > 1) {
ans[l] = ans[r] = curr;
freq[curr - 'a'] -= 2;
if (func(i + 1, target, ans, l + 1, r - 1, freq, end)) {
return true;
}
freq[curr - 'a'] += 2;
}
if (next != '1') {
ans[l] = ans[r] = next;
freq[next - 'a'] -= 2;
if (func(i + 1, target, ans, l + 1, r - 1, freq, true)) {
return true;
}
}
ans[l] = ans[r] = '#';
return false;
}

public String lexPalindromicPermutation(String s, String target) {
int[] freq = new int[26];
for (int i = 0; i < s.length(); i++) {
freq[s.charAt(i) - 'a']++;
}
int oddc = 0;
for (int i = 0; i < 26; i++) {
if (freq[i] % 2 == 1) {
oddc++;
}
}
if (oddc > 1) {
return "";
}
char[] ans = new char[s.length()];
Arrays.fill(ans, '#');
if (func(0, target, ans, 0, s.length() - 1, freq, false)) {
return new String(ans);
}
return "";
}
}
Loading