📖 Scenario: You are working on a simple program that helps a store manager rotate the order of products on a shelf. The products are arranged in a list, and sometimes the manager wants to move the first few products to the end to change the display order.
🎯 Goal: Build a program that rotates an array of product names to the left by a given number of positions. This means the first few products move to the end, and the rest shift forward.
📋 What You'll Learn
Create an array called products with these exact values: "Milk", "Bread", "Eggs", "Butter", "Cheese"
Create an integer variable called rotateBy and set it to 2
Write code to rotate the products array to the left by rotateBy positions
Print the rotated array elements separated by spaces
💡 Why This Matters
🌍 Real World
Rotating arrays is useful in real life when you want to change the order of items cyclically, like rotating shifts, playlists, or product displays.
💼 Career
Understanding array rotation helps in many programming tasks such as data manipulation, scheduling algorithms, and game development.
Progress0 / 4 steps
1
Create the products array
Create a string array called products with these exact values: "Milk", "Bread", "Eggs", "Butter", "Cheese"
DSA C#
Hint
Use string[] products = { ... }; to create the array with the exact product names.
2
Set the rotation count
Create an integer variable called rotateBy and set it to 2
DSA C#
Hint
Use int rotateBy = 2; to set the number of positions to rotate.
3
Rotate the array to the left
Write code to rotate the products array to the left by rotateBy positions. Create a new array called rotated to hold the rotated products.
DSA C#
Hint
Use a for loop to move each product to its new position. Use modulo (%) to wrap around the array.
4
Print the rotated array
Print the elements of the rotated array separated by spaces using Console.WriteLine.
DSA C#
Hint
Use Console.WriteLine(string.Join(" ", rotated)); to print all elements separated by spaces.
Practice
(1/5)
1. What does a left rotation by 1 position do to the array [1, 2, 3, 4, 5]?
easy
A. Leaves the array unchanged
B. Moves the last element to the front, resulting in [5, 1, 2, 3, 4]
C. Reverses the array, resulting in [5, 4, 3, 2, 1]
D. Moves the first element to the end, resulting in [2, 3, 4, 5, 1]
Solution
Step 1: Understand left rotation by 1
Left rotation by 1 means the first element moves to the end, and all others shift left by one.
Step 2: Apply to given array
Starting with [1, 2, 3, 4, 5], after rotation it becomes [2, 3, 4, 5, 1].
Final Answer:
Moves the first element to the end, resulting in [2, 3, 4, 5, 1] -> Option D
Quick Check:
Left rotation by 1 shifts left, first element to end [OK]
Hint: Left rotation moves front element to end [OK]
Common Mistakes:
Confusing left rotation with right rotation
Thinking rotation reverses the array
Assuming array stays the same
2. Which of the following C# code snippets correctly performs a right rotation by 1 on an integer array arr?
easy
A. int last = arr[arr.Length - 1]; for (int i = arr.Length - 1; i > 0; i--) arr[i] = arr[i - 1]; arr[0] = last;
B. int first = arr[0]; for (int i = 0; i < arr.Length - 1; i++) arr[i] = arr[i + 1]; arr[arr.Length - 1] = first;
C. Array.Reverse(arr);
D. Array.Sort(arr);
Solution
Step 1: Understand right rotation by 1
Right rotation by 1 moves the last element to the front, shifting others right.
Step 2: Check code snippets
int last = arr[arr.Length - 1]; for (int i = arr.Length - 1; i > 0; i--) arr[i] = arr[i - 1]; arr[0] = last; saves last element, shifts elements right, then places last at front. This matches right rotation.
Final Answer:
int last = arr[arr.Length - 1]; for (int i = arr.Length - 1; i > 0; i--) arr[i] = arr[i - 1]; arr[0] = last; -> Option A
Quick Check:
Right rotation moves last element to front [OK]
Hint: Right rotation moves last element to front [OK]
Common Mistakes:
Using left rotation code for right rotation
Reversing array instead of rotating
Sorting array instead of rotating
3. What is the output of the following C# code?
int[] arr = {10, 20, 30, 40, 50};
int d = 2;
int n = arr.Length;
int[] rotated = new int[n];
for (int i = 0; i < n; i++) {
rotated[i] = arr[(i + d) % n];
}
Console.WriteLine(string.Join(", ", rotated));
medium
A. 40, 50, 10, 20, 30
B. 30, 40, 50, 10, 20
C. 50, 10, 20, 30, 40
D. 10, 20, 30, 40, 50
Solution
Step 1: Understand rotation logic in code
Each element at index i in rotated is assigned arr[(i + d) % n], which means left rotation by d=2.