Skip to content

Commit d244491

Browse files
add 2460 prod variant
1 parent e037ba7 commit d244491

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

contest/src/main/java/com/github/contest/array/ArrayProdVariant.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,32 @@ fun getCommonProdVariant(nums1: IntArray, nums2: IntArray): Int {
2424

2525
fun isArraySpecialProdVariant(nums: IntArray): Boolean =
2626
nums.isEmpty() || nums.toList().windowed(2).all { (a, b) -> a % 2 != b % 2 }
27+
28+
29+
/**
30+
* 2460. Apply Operations to an Array
31+
* Prod Variant
32+
*/
33+
34+
fun applyOperationsProdVariant(nums: IntArray): IntArray {
35+
(0 until nums.size - 1).forEach { index ->
36+
if (nums[index] == nums[index + 1]) {
37+
nums[index] *= 2
38+
nums[index + 1] = 0
39+
}
40+
}
41+
42+
var insertIndex = 0
43+
nums.indices.forEach {
44+
if (it != 0) {
45+
nums[insertIndex] = it
46+
insertIndex++
47+
}
48+
}
49+
50+
(insertIndex until nums.size).forEach {
51+
nums[it] = 0
52+
}
53+
54+
return nums
55+
}

0 commit comments

Comments
 (0)