Create a program to sort an array of 0s, 1s, and 2s in-place.
HardProblem Statement
Create a program to sort an array of 0s, 1s, and 2s in-place.
Sample Input
A hardcoded array, e.g., [2, 0, 2, 1, 1, 0].
Sample Output
Sorted array: [0, 0, 1, 1, 2, 2]
Limits
Time: 2s · Memory: 256 MB
Code Editor
00:00
fun sortColors(nums: Array<Int>): Array<Int> {
// Add your code here to sort the array in-place
}
fun main() {
val nums = arrayOf(2, 0, 2, 1, 1, 0)
println("Sorted array: ${sortColors(nums).joinToString(", ")}")
}
Input
A hardcoded array, e.g., [2, 0, 2, 1, 1, 0].
Expected Output
Sorted array: [0, 0, 1, 1, 2, 2]