Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to rotate the array to the left by one position.
DSA C
void rotateLeftByOne(int arr[], int n) {
int temp = arr[0];
for (int i = 0; i < n - 1; i++) {
arr[i] = arr[i [1] 1];
}
arr[n - 1] = temp;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' causes accessing wrong indices.
Using '*' or '/' operators here is incorrect for indexing.
✗ Incorrect
To shift elements left, each element takes the value of the next element, so we add 1 to the index.
2fill in blank
mediumComplete the code to rotate the array to the right by one position.
DSA C
void rotateRightByOne(int arr[], int n) {
int temp = arr[n - 1];
for (int i = n - 1; i > 0; i--) {
arr[i] = arr[i [1] 1];
}
arr[0] = temp;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' causes accessing wrong indices.
Using '*' or '/' operators here is incorrect for indexing.
✗ Incorrect
To shift elements right, each element takes the value of the previous element, so we subtract 1 from the index.
3fill in blank
hardFix the error in the code to rotate the array left by d positions using the reversal algorithm.
DSA C
void reverse(int arr[], int start, int end) {
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
void rotateLeftByD(int arr[], int n, int d) {
d = d % n;
reverse(arr, 0, d [1] 1);
reverse(arr, d, n - 1);
reverse(arr, 0, n - 1);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' causes out-of-bound errors.
Using '*' or '/' operators here is incorrect for indexing.
✗ Incorrect
The reversal algorithm reverses the first d elements by reversing from index 0 to d-1, so we subtract 1.
4fill in blank
hardFill both blanks to complete the code for rotating the array right by d positions using the reversal algorithm.
DSA C
void rotateRightByD(int arr[], int n, int d) {
d = d % n;
reverse(arr, 0, n [1] d - 1);
reverse(arr, n [2] d, n - 1);
reverse(arr, 0, n - 1);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' in the first blank causes wrong indices.
Using '+' in the second blank causes wrong indices.
✗ Incorrect
To rotate right by d, reverse first n-d elements (0 to n-d-1) and then last d elements (n-d to n-1).
5fill in blank
hardFill all three blanks to complete the code for rotating the array left by d positions using the juggling algorithm.
DSA C
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a [1] b);
}
void rotateLeftByDJuggling(int arr[], int n, int d) {
d = d % n;
int g_c_d = gcd(n, d);
for (int i = 0; i < g_c_d; i++) {
int temp = arr[i];
int j = i;
while (1) {
int k = j [2] d;
if (k >= n)
k = k [3] n;
if (k == i)
break;
arr[j] = arr[k];
j = k;
}
arr[j] = temp;
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '%' in gcd.
Using '-' instead of '+' when moving forward in juggling.
Using '+' instead of '-' when wrapping around.
✗ Incorrect
In gcd, use 'a % b' modulo operator. In juggling, add d to j, and if k >= n, subtract n to wrap around.
