Skip to content

Commit

Permalink
Added tasks 2996-3000
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored Jan 17, 2024
1 parent fc3f7e3 commit aa7c845
Show file tree
Hide file tree
Showing 15 changed files with 460 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package g2901_3000.s2996_smallest_missing_integer_greater_than_sequential_prefix_sum;

// #Easy #Array #Hash_Table #Sorting #2024_01_17_Time_1_ms_(93.13%)_Space_42.2_MB_(58.31%)

import java.util.Arrays;

public class Solution {
public int missingInteger(int[] nums) {
int n = nums.length;
int sum = nums[0];
for (int i = 1; i < n; i++) {
if (nums[i] == nums[i - 1] + 1) {
sum = sum + nums[i];
} else {
break;
}
}
Arrays.sort(nums);
for (int no : nums) {
if (no == sum) {
sum++;
}
}
return sum;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
2996\. Smallest Missing Integer Greater Than Sequential Prefix Sum

Easy

You are given a **0-indexed** array of integers `nums`.

A prefix `nums[0..i]` is **sequential** if, for all `1 <= j <= i`, `nums[j] = nums[j - 1] + 1`. In particular, the prefix consisting only of `nums[0]` is **sequential**.

Return _the **smallest** integer_ `x` _missing from_ `nums` _such that_ `x` _is greater than or equal to the sum of the **longest** sequential prefix._

**Example 1:**

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

**Output:** 6

**Explanation:** The longest sequential prefix of nums is [1,2,3] with a sum of 6. 6 is not in the array, therefore 6 is the smallest missing integer greater than or equal to the sum of the longest sequential prefix.

**Example 2:**

**Input:** nums = [3,4,5,1,12,14,13]

**Output:** 15

**Explanation:** The longest sequential prefix of nums is [3,4,5] with a sum of 12. 12, 13, and 14 belong to the array while 15 does not. Therefore 15 is the smallest missing integer greater than or equal to the sum of the longest sequential prefix.

**Constraints:**

* `1 <= nums.length <= 50`
* `1 <= nums[i] <= 50`
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package g2901_3000.s2997_minimum_number_of_operations_to_make_array_xor_equal_to_k;

// #Medium #Array #Bit_Manipulation #2024_01_17_Time_1_ms_(100.00%)_Space_58.7_MB_(15.31%)

public class Solution {
public int minOperations(int[] nums, int k) {
int count = 0;
int xor = 0;
for (int num : nums) {
xor = xor ^ num;
}
while (xor > 0 || k > 0) {
if (xor % 2 != k % 2) {
count++;
}
xor /= 2;
k /= 2;
}
return count;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
2997\. Minimum Number of Operations to Make Array XOR Equal to K

Medium

You are given a **0-indexed** integer array `nums` and a positive integer `k`.

You can apply the following operation on the array **any** number of times:

* Choose **any** element of the array and **flip** a bit in its **binary** representation. Flipping a bit means changing a `0` to `1` or vice versa.

Return _the **minimum** number of operations required to make the bitwise_ `XOR` _of **all** elements of the final array equal to_ `k`.

**Note** that you can flip leading zero bits in the binary representation of elements. For example, for the number <code>(101)<sub>2</sub></code> you can flip the fourth bit and obtain <code>(1101)<sub>2</sub></code>.

**Example 1:**

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

**Output:** 2

**Explanation:** We can do the following operations:

- Choose element 2 which is 3 == (011)<sub>2</sub>, we flip the first bit and we obtain (010)<sub>2</sub> == 2. nums becomes [2,1,2,4].

- Choose element 0 which is 2 == (010)<sub>2</sub>, we flip the third bit and we obtain (110)<sub>2</sub> = 6. nums becomes [6,1,2,4].

The XOR of elements of the final array is (6 XOR 1 XOR 2 XOR 4) == 1 == k.

It can be shown that we cannot make the XOR equal to k in less than 2 operations.

**Example 2:**

**Input:** nums = [2,0,2,0], k = 0

**Output:** 0

**Explanation:** The XOR of elements of the array is (2 XOR 0 XOR 2 XOR 0) == 0 == k. So no operation is needed.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>0 <= nums[i] <= 10<sup>6</sup></code>
* <code>0 <= k <= 10<sup>6</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g2901_3000.s2998_minimum_number_of_operations_to_make_x_and_y_equal;

// #Medium #Dynamic_Programming #Memoization #Breadth_First_Search
// #2024_01_17_Time_0_ms_(100.00%)_Space_41.2_MB_(92.39%)

public class Solution {
public int minimumOperationsToMakeEqual(int x, int y) {
if (x <= y) {
return y - x;
}
int res = x - y;
res = Math.min(res, 1 + minimumOperationsToMakeEqual(x / 5, y) + x % 5);
res = Math.min(res, 1 + minimumOperationsToMakeEqual(x / 5 + 1, y) + 5 - x % 5);
res = Math.min(res, 1 + minimumOperationsToMakeEqual(x / 11, y) + x % 11);
res = Math.min(res, 1 + minimumOperationsToMakeEqual(x / 11 + 1, y) + 11 - x % 11);
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
2998\. Minimum Number of Operations to Make X and Y Equal

Medium

You are given two positive integers `x` and `y`.

In one operation, you can do one of the four following operations:

1. Divide `x` by `11` if `x` is a multiple of `11`.
2. Divide `x` by `5` if `x` is a multiple of `5`.
3. Decrement `x` by `1`.
4. Increment `x` by `1`.

Return _the **minimum** number of operations required to make_ `x` _and_ `y` equal.

**Example 1:**

**Input:** x = 26, y = 1

**Output:** 3

**Explanation:** We can make 26 equal to 1 by applying the following operations:

1. Decrement x by 1

2. Divide x by 5

3. Divide x by 5

It can be shown that 3 is the minimum number of operations required to make 26 equal to 1.

**Example 2:**

**Input:** x = 54, y = 2

**Output:** 4

**Explanation:** We can make 54 equal to 2 by applying the following operations:

1. Increment x by 1

2. Divide x by 11

3. Divide x by 5

4. Increment x by 1

It can be shown that 4 is the minimum number of operations required to make 54 equal to 2.

**Example 3:**

**Input:** x = 25, y = 30

**Output:** 5

**Explanation:** We can make 25 equal to 30 by applying the following operations:

1. Increment x by 1

2. Increment x by 1

3. Increment x by 1

4. Increment x by 1

5. Increment x by 1

It can be shown that 5 is the minimum number of operations required to make 25 equal to 30.

**Constraints:**

* <code>1 <= x, y <= 10<sup>4</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package g2901_3000.s2999_count_the_number_of_powerful_integers;

// #Hard #String #Dynamic_Programming #Math #2024_01_17_Time_1_ms_(100.00%)_Space_42.6_MB_(83.99%)

public class Solution {
public long numberOfPowerfulInt(long start, long finish, int limit, String s) {
long sn = Long.parseLong(s);
if (finish < sn) {
return 0;
}
start = Math.max(start, sn);
long originalL = s.length();
long factor = 1;
for (long i = 1; i <= originalL; i++) {
factor *= 10;
}
long sx = (start - sn) % factor == 0 ? (start - sn) / factor : (start - sn) / factor + 1;
long lx = (finish - sn) / factor;

return sx == 0
? indexOfLimitIntSmallerThanOrEqual(lx, limit) + 1
: indexOfLimitIntSmallerThanOrEqual(lx, limit)
- indexOfLimitIntSmallerThanOrEqual(sx - 1, limit);
}

private long indexOfLimitIntSmallerThanOrEqual(long target, int limit) {
String s = Long.toString(target);
long index = 0;
boolean limitViolated = false;
for (int i = 0; i < s.length(); i++) {
index *= limit + 1;
if (!limitViolated) {
if (s.charAt(i) - '0' > limit) {
limitViolated = true;
index += limit;
} else {
index += (s.charAt(i) - '0');
}
} else {
index += limit;
}
}
return index;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
2999\. Count the Number of Powerful Integers

Hard

You are given three integers `start`, `finish`, and `limit`. You are also given a **0-indexed** string `s` representing a **positive** integer.

A **positive** integer `x` is called **powerful** if it ends with `s` (in other words, `s` is a **suffix** of `x`) and each digit in `x` is at most `limit`.

Return _the **total** number of powerful integers in the range_ `[start..finish]`.

A string `x` is a suffix of a string `y` if and only if `x` is a substring of `y` that starts from some index (**including** `0`) in `y` and extends to the index `y.length - 1`. For example, `25` is a suffix of `5125` whereas `512` is not.

**Example 1:**

**Input:** start = 1, finish = 6000, limit = 4, s = "124"

**Output:** 5

**Explanation:** The powerful integers in the range [1..6000] are 124, 1124, 2124, 3124, and, 4124. All these integers have each digit <= 4, and "124" as a suffix. Note that 5124 is not a powerful integer because the first digit is 5 which is greater than 4.

It can be shown that there are only 5 powerful integers in this range.

**Example 2:**

**Input:** start = 15, finish = 215, limit = 6, s = "10"

**Output:** 2

**Explanation:** The powerful integers in the range [15..215] are 110 and 210. All these integers have each digit <= 6, and "10" as a suffix.

It can be shown that there are only 2 powerful integers in this range.

**Example 3:**

**Input:** start = 1000, finish = 2000, limit = 4, s = "3000"

**Output:** 0

**Explanation:** All integers in the range [1000..2000] are smaller than 3000, hence "3000" cannot be a suffix of any integer in this range.

**Constraints:**

* <code>1 <= start <= finish <= 10<sup>15</sup></code>
* `1 <= limit <= 9`
* <code>1 <= s.length <= floor(log<sub>10</sub>(finish)) + 1</code>
* `s` only consists of numeric digits which are at most `limit`.
* `s` does not have leading zeros.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package g2901_3000.s3000_maximum_area_of_longest_diagonal_rectangle;

// #Easy #Array #2024_01_17_Time_1_ms_(99.67%)_Space_44.1_MB_(93.21%)

public class Solution {
public int areaOfMaxDiagonal(int[][] dimensions) {
int mx = 0;
for (int[] t : dimensions) {
if (t[0] * t[0] + t[1] * t[1] > mx) {
mx = t[0] * t[0] + t[1] * t[1];
}
}
int area = 0;
for (int[] t : dimensions) {
if (t[0] * t[0] + t[1] * t[1] == mx && t[0] * t[1] > area) {
area = t[0] * t[1];
}
}
return area;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
3000\. Maximum Area of Longest Diagonal Rectangle

Easy

You are given a 2D **0-indexed** integer array `dimensions`.

For all indices `i`, `0 <= i < dimensions.length`, `dimensions[i][0]` represents the length and `dimensions[i][1]` represents the width of the rectangle `i`.

Return _the **area** of the rectangle having the **longest** diagonal. If there are multiple rectangles with the longest diagonal, return the area of the rectangle having the **maximum** area._

**Example 1:**

**Input:** dimensions = [[9,3],[8,6]]

**Output:** 48

**Explanation:**

For index = 0, length = 9 and width = 3. Diagonal length = sqrt(9 \* 9 + 3 \* 3) = sqrt(90) ≈ 9.487.

For index = 1, length = 8 and width = 6. Diagonal length = sqrt(8 \* 8 + 6 \* 6) = sqrt(100) = 10.

So, the rectangle at index 1 has a greater diagonal length therefore we return area = 8 \* 6 = 48.

**Example 2:**

**Input:** dimensions = [[3,4],[4,3]]

**Output:** 12

**Explanation:** Length of diagonal is the same for both which is 5, so maximum area = 12.

**Constraints:**

* `1 <= dimensions.length <= 100`
* `dimensions[i].length == 2`
* `1 <= dimensions[i][0], dimensions[i][1] <= 100`
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g2901_3000.s2996_smallest_missing_integer_greater_than_sequential_prefix_sum;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void missingInteger() {
assertThat(new Solution().missingInteger(new int[] {1, 2, 3, 2, 5}), equalTo(6));
}

@Test
void missingInteger2() {
assertThat(new Solution().missingInteger(new int[] {3, 4, 5, 1, 12, 14, 13}), equalTo(15));
}
}
Loading

0 comments on commit aa7c845

Please sign in to comment.