Solution Review: Shuffle Integers
This review discusses the solution of the Shuffle Integers challenge in detail.
We'll cover the following
Solution # 1
It is safe to assume for this problem that the given array can be divided into two equal halves, since the number of elements in the array is .
The brute force solution to solve this problem involves two nested loops to transfer the elements in the right half of the array to the left half.
-
(Line 7): The first loop runs
n/2
times (Line 15: size was halved) to cover all elements in the second half of the array. -
(Line 8,9): The second loop transfers the elements to the left.
Note that the start index in the second loop depends on which element we are rotating, and the end index depends on how many positions we need to move to the left.
Here is the implementation of this approach:
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.