📖 Scenario: You are working on a simple program that helps rotate elements in an array. Imagine you have a row of books on a shelf, and you want to move some books from the front to the back, keeping their order. This is called rotating the array.
🎯 Goal: Build a program that rotates an array of integers to the left by a given number of positions and then prints the rotated array.
📋 What You'll Learn
Create an array with exact elements: 10, 20, 30, 40, 50
Create an integer variable for the number of rotations with value 2
Rotate the array to the left by the number of rotations
Print the rotated array elements separated by spaces
💡 Why This Matters
🌍 Real World
Array rotation is useful in tasks like scheduling, buffering data streams, and cyclic shifts in games or simulations.
💼 Career
Understanding array rotation helps in coding interviews and solving problems related to data manipulation and optimization.
Progress0 / 4 steps
1
Create the initial array
Create an integer array called arr with these exact elements: 10, 20, 30, 40, 50.
DSA C++
Hint
Use curly braces to list the elements inside the array declaration.
2
Set the number of rotations
Create an integer variable called rotations and set it to 2.
DSA C++
Hint
Just declare an integer variable and assign the value 2.
3
Rotate the array to the left
Write a for loop that rotates the array arr to the left by rotations positions. Use a temporary array called rotated to store the rotated elements. The array size is 5.
DSA C++
Hint
Use modulo operator % to wrap around the array indices.
4
Print the rotated array
Use a for loop to print all elements of the rotated array separated by spaces on one line.
DSA C++
Hint
Print each element followed by a space, then print a newline at the end.
Practice
(1/5)
1. What does rotating an array to the left by 1 position do to the elements?
easy
A. Moves the last element to the front and shifts all others one position right
B. Moves the first element to the end and shifts all others one position left
C. Reverses the entire array
D. Sorts the array in ascending order
Solution
Step 1: Understand left rotation by 1
Left rotation by 1 means the element at index 0 moves to the end.
Step 2: Effect on other elements
All other elements shift one position to the left to fill the gap.
Final Answer:
Moves the first element to the end and shifts all others one position left -> Option B
Quick Check:
Left rotate by 1 = first element moves to end [OK]
Hint: Left rotate by 1 moves front element to back [OK]
Common Mistakes:
Confusing left rotation with right rotation
Thinking rotation reverses the array
Assuming rotation sorts the array
2. Which of the following is the correct syntax to rotate an array arr of size n to the right by 1 position in C++?
easy
A. std::rotate(arr, arr + n, arr + 1);
B. std::rotate(arr, arr + 1, arr + n);
C. std::rotate(arr, arr + n - 1, arr + n);
D. std::rotate(arr + 1, arr, arr + n);
Solution
Step 1: Understand std::rotate parameters
std::rotate takes three pointers: first, middle, last. It rotates the range [first, last) so that the element at middle becomes first.
Step 2: Rotate right by 1 means last element moves to front
To rotate right by 1, middle should point to the last element (arr + n - 1).
Final Answer:
std::rotate(arr, arr + n - 1, arr + n); -> Option C
Quick Check:
Right rotate by 1 uses middle = last-1 [OK]
Hint: Right rotate by 1 uses middle = last-1 in std::rotate [OK]
Common Mistakes:
Using middle = first + 1 for right rotation
Swapping parameters order incorrectly
Confusing rotate range boundaries
3. What is the output of the following C++ code?
int arr[] = {10, 20, 30, 40, 50};
int n = 5;
int d = 2;
std::rotate(arr, arr + d, arr + n);
for(int i = 0; i < n; i++) std::cout << arr[i] << " ";
medium
A. 30 40 50 10 20
B. 40 50 10 20 30
C. 50 10 20 30 40
D. 20 30 40 50 10
Solution
Step 1: Understand std::rotate with d=2
std::rotate(arr, arr + 2, arr + 5) moves elements starting from index 2 to front.
Step 2: Rearrange array after rotation
Elements from index 2 to 4: 30, 40, 50 come first, then elements from index 0 to 1: 10, 20.
Final Answer:
30 40 50 10 20 -> Option A
Quick Check:
Rotate left by 2 shifts first 2 elements to end [OK]
Hint: Rotate left by d moves first d elements to end [OK]
Common Mistakes:
Confusing left and right rotation
Off-by-one errors in index calculation
Printing array before rotation
4. The following code is intended to rotate an array arr of size n to the left by d positions. What is the error?
void rotateLeft(int arr[], int n, int d) {
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;
}
}
medium
A. temp should be assigned arr[n-1] instead of arr[0]
B. Inner loop should run from j=1 to n-1
C. The outer loop should run from i=1 to d
D. Accessing arr[n] causes out-of-bounds error
Solution
Step 1: Analyze inner loop index usage
The inner loop runs j from 0 to n-1, but accesses arr[j+1]. When j = n-1, arr[j+1] = arr[n] is accessed.
Step 2: Identify out-of-bounds access
Array indices go from 0 to n-1, so arr[n] is invalid and causes out-of-bounds error.
Final Answer:
Accessing arr[n] causes out-of-bounds error -> Option D
Quick Check:
Array index out of bounds at arr[n] [OK]
Hint: Array indices go 0 to n-1, arr[n] is invalid [OK]
Common Mistakes:
Ignoring out-of-bounds access
Misunderstanding loop ranges
Incorrectly assigning temp value
5. Given an array arr = {1, 2, 3, 4, 5, 6, 7} and d = 3, which sequence of operations correctly rotates the array to the left by d positions using the reversal algorithm?
hard
A. Reverse first d elements, reverse last n-d elements, then reverse whole array
B. Reverse whole array, reverse first d elements, then reverse last n-d elements
C. Reverse first d elements, reverse whole array, then reverse last n-d elements
D. Reverse whole array, reverse last n-d elements, then reverse first d elements
Solution
Step 1: Understand reversal algorithm steps
To rotate left by d, first reverse the first d elements.
Step 2: Reverse last n-d elements, then reverse whole array
Next reverse the last n-d elements, finally reverse the entire array to get the rotated result.
Final Answer:
Reverse first d elements, reverse last n-d elements, then reverse whole array -> Option A
Quick Check:
Reversal algorithm order = first d, last n-d, whole [OK]
Hint: Left rotate reversal: first d, last n-d, then whole [OK]
Common Mistakes:
Reversing parts before whole array
Mixing order of reversing segments
Confusing left and right rotation steps
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.