File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
contest/src/main/java/com/github/contest/array Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -62,4 +62,37 @@ fun sortArrayByParityII(nums: IntArray): IntArray {
62
62
return nums
63
63
}
64
64
65
+ /* *
66
+ * 2460. Apply Operations to an Array
67
+ */
68
+
69
+ fun applyOperations (nums : IntArray ): IntArray {
70
+ for (i in 0 until nums.size - 1 ) {
71
+ if (nums[i] == nums[i + 1 ]) {
72
+ nums[i] * = 2
73
+ nums[i + 1 ] = 0
74
+ }
75
+ }
76
+ var i = 0
77
+ var j = 1
78
+ while (i < nums.size && j < nums.size) {
79
+ while (i < nums.size && nums[i] != 0 ) i++
80
+ j = i
81
+ while (j < nums.size && nums[j] == 0 ) j++
82
+ if (i < nums.size && j < nums.size) {
83
+ nums.swap(i, j)
84
+ i++
85
+ j++
86
+ }
87
+ }
88
+
89
+ return nums
90
+ }
91
+
92
+ private fun IntArray.swap (from : Int , to : Int ) {
93
+ val temp = this [from]
94
+ this [from] = this [to]
95
+ this [to] = temp
96
+ }
97
+
65
98
You can’t perform that action at this time.
0 commit comments