File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ My solutions to LeetCode problems in Kotlin.
3838
| [2914](https://leetcode.com/problems/minimum-number-of-changes-to-make-binary-string-beautiful/) | [Minimum Number of Changes to Make Binary String Beautiful](src/main/kotlin/com/schmoczer/leetcode/_2914/MinChanges.kt) | Medium |
3939
| [2924](https://leetcode.com/problems/find-champion-ii/) | [Find Champion II](src/main/kotlin/com/schmoczer/leetcode/_2924/FindChampionII.kt) | Medium |
4040
| [3011](https://leetcode.com/problems/find-if-array-can-be-sorted/) | [Find if Array Can Be Sorted](src/main/kotlin/com/schmoczer/leetcode/_3011/FindIfArrayCanBeSorted.kt) | Medium |
41+
| [3133](https://leetcode.com/problems/minimum-array-end/) | [Minimum Array End](src/main/kotlin/com/schmoczer/leetcode/_3133/MinimumArrayEnd.kt) | Medium |
4142
| [3163](https://leetcode.com/problems/string-compression-iii/) | [String Compression III](src/main/kotlin/com/schmoczer/leetcode/_3163/StringCompression3.kt) | Medium |
4243
| [3254](https://leetcode.com/problems/find-the-power-of-k-size-subarrays-i/) | [Find the Power of K-Size Subarrays I](src/main/kotlin/com/schmoczer/leetcode/_3254/FindThePowerOfKSizeSubarrays.kt) | Medium |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.schmoczer.leetcode._3133
2+
3+
class MinimumArrayEnd {
4+
fun minEnd(n: Int, x: Int): Long {
5+
var count = 0
6+
var result = x.toLong()
7+
while (count < n - 1) {
8+
result = (result + 1) or x.toLong()
9+
count++
10+
}
11+
return result
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Minimum Array End
2+
3+
You are given two integers `n` and `x`. You have to construct an array of positive integers `nums` of size `n` where for
4+
every `0 <= i < n - 1`, `nums[i + 1]` is greater than `nums[i]`, and the result of the bitwise `AND` operation between
5+
all elements of `nums` is `x`.
6+
7+
Return the minimum possible value of `nums[n - 1]`.
8+
9+
Example 1:
10+
11+
> Input: n = 3, x = 4
12+
>
13+
> Output: 6
14+
15+
Example 2:
16+
17+
> Input: n = 2, x = 7
18+
>
19+
> Output: 15
20+
21+
Constraints:
22+
23+
- `1 <= n, x <= 10^8`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.schmoczer.leetcode._3133
2+
3+
import org.junit.jupiter.api.BeforeEach
4+
import kotlin.test.Test
5+
import kotlin.test.assertEquals
6+
7+
class MinimumArrayEndTest {
8+
private lateinit var sut: MinimumArrayEnd
9+
10+
@BeforeEach
11+
fun setUp() {
12+
sut = MinimumArrayEnd()
13+
}
14+
15+
@Test
16+
fun `when n = 3 and x = 4 then array end is 6`() {
17+
val n = 3
18+
val x = 4
19+
val expected: Long = 6
20+
21+
val result = sut.minEnd(n, x)
22+
23+
assertEquals(expected, result)
24+
}
25+
26+
@Test
27+
fun `when n = 2 and x = 7 then array end is 15`() {
28+
val n = 2
29+
val x = 7
30+
val expected: Long = 15
31+
32+
val result = sut.minEnd(n, x)
33+
34+
assertEquals(expected, result)
35+
}
36+
}

0 commit comments

Comments
 (0)