Bird
0
0
DSA Cprogramming~10 mins

Array Rotation Techniques in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Array Rotation Techniques
Start with array
Choose rotation direction
Rotate Left
Shift elements accordingly
Handle wrap-around
Update array state
Done
Shows the flow of rotating an array either left or right by shifting elements and wrapping around.
Execution Sample
DSA C
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
int d = 2; // rotate left by 2
// Rotate left by d positions
for (int i = 0; i < d; i++) {
  int temp = arr[0];
  for (int j = 0; j < n - 1; j++) {
    arr[j] = arr[j + 1];
  }
  arr[n - 1] = temp;
}
Rotates the array left by 2 positions by shifting elements one by one.
Execution Table
StepOperationArray StatePointer ChangesVisual State
1Start[1, 2, 3, 4, 5]temp = arr[0] = 11 -> 2 -> 3 -> 4 -> 5
2Shift elements left by 1[2, 3, 4, 5, 5]j moves from 0 to 32 -> 3 -> 4 -> 5 -> 5
3Place temp at end[2, 3, 4, 5, 1]arr[n-1] = temp2 -> 3 -> 4 -> 5 -> 1
4Start second rotationCurrent array: [2, 3, 4, 5, 1]temp = arr[0] = 22 -> 3 -> 4 -> 5 -> 1
5Shift elements left by 1[3, 4, 5, 1, 1]j moves from 0 to 33 -> 4 -> 5 -> 1 -> 1
6Place temp at end[3, 4, 5, 1, 2]arr[n-1] = temp3 -> 4 -> 5 -> 1 -> 2
7Rotation complete[3, 4, 5, 1, 2]No pointer changes3 -> 4 -> 5 -> 1 -> 2
💡 Completed 2 left rotations as d=2
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6Final
arr[1, 2, 3, 4, 5][1, 2, 3, 4, 5][2, 3, 4, 5, 5][2, 3, 4, 5, 1][2, 3, 4, 5, 1][3, 4, 5, 1, 1][3, 4, 5, 1, 2][3, 4, 5, 1, 2]
tempundefined1112222
jundefined0 to 30 to 3undefined0 to 30 to 3undefinedundefined
i00001112
Key Moments - 3 Insights
Why do we save arr[0] in temp before shifting?
Because shifting elements left overwrites arr[0], we save it in temp to place it at the end later. See execution_table steps 1 and 3.
Why does the inner loop run till n-1 and not n?
Because we shift elements from arr[j+1] to arr[j], j must stop at n-2 to avoid out-of-bound access. See execution_table steps 2 and 5.
What happens if d is equal to n?
Rotating by n positions results in the same array, so after d rotations, the array returns to original. This is why d is usually taken modulo n.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the array state?
A[1, 2, 3, 4, 5]
B[2, 3, 4, 5, 1]
C[2, 3, 4, 5, 5]
D[3, 4, 5, 1, 2]
💡 Hint
Check the 'Array State' column at step 3 in execution_table.
At which step does the second rotation start?
AStep 5
BStep 2
CStep 4
DStep 6
💡 Hint
Look for 'Start second rotation' in the 'Operation' column.
If we change d to 3, how many times will the outer loop run?
A3 times
B2 times
C5 times
D1 time
💡 Hint
d controls the number of rotations, see variable 'i' in variable_tracker.
Concept Snapshot
Array Rotation Techniques:
- Rotate array left or right by shifting elements.
- Save first/last element before shifting to handle wrap-around.
- Repeat shifting d times for d rotations.
- Use modulo n for d to avoid full cycle rotations.
- Time complexity: O(n*d) for naive method.
Full Transcript
This visualization shows how to rotate an array left by d positions using a simple shifting method. We start with the original array and save the first element in a temporary variable. Then we shift all elements one position to the left, overwriting the first element. After shifting, we place the saved element at the end of the array. This process repeats d times. The execution table tracks each step, showing the array state and pointer changes. Key moments clarify why we save the first element and why the inner loop runs till n-1. The visual quiz tests understanding of array states at different steps and the effect of changing d. The concept snapshot summarizes the rotation technique and its complexity.