Rotation Array Operation Multiple Choice Questions and Answers (MCQs)

Uncategorized

This set of Data Structures & Algorithms Multiple Choice Questions & Answers (MCQs) focuses on “Rotation Array Operation”.

1. What will be the time complexity of the following code?

#include <bits/stdc++.h> 
using namespace std; 
void func1(int arr[], int n) 
{ 
	int k = arr[0], i; 
	for (i = 0; i < n - 1; i++) 
		arr[i] = arr[i + 1]; 
 
	arr[i] = k; 
} 
 
void func(int arr[], int d, int n) 
{ 
	for (int i = 0; i < d; i++) 
		func1(arr, n); 
} 
 
void printArray(int arr[], int n) 
{ 
	for (int i = 0; i < n; i++) 
		cout << arr[i] << " "; 
} 
 
int main() 
{ 
	int arr[] = { 1, 2, 3, 4, 5}; 
	int n = sizeof(arr) / sizeof(arr[0]); 
 
    int d = 3;
	func(arr, d, n); 
	printArray(arr, n); 
 
	return 0; 
}

A) O(n*d)
B) O(n)
C) O(d)
D) O(n2)

Explanation: The input array is rotated by d in the given code. It accomplishes this by rotating the elements one at a time until the desired rotation is reached. The rotation of each element takes O(n) time, and there are d such elements in the array. As a result, the time complexity is O(n*d).

2. What will be the auxiliary space complexity of the following code?

#include <bits/stdc++.h> 
using namespace std; 
void func1(int arr[], int n) 
{ 
	int k = arr[0], i; 
	for (i = 0; i < n - 1; i++) 
		arr[i] = arr[i + 1]; 
 
	arr[i] = k; 
} 
 
void func(int arr[], int d, int n) 
{ 
	for (int i = 0; i < d; i++) 
		func1(arr, n); 
} 
 
void printArray(int arr[], int n) 
{ 
	for (int i = 0; i < n; i++) 
		cout << arr[i] << " "; 
} 
 
int main() 
{ 
	int arr[] = { 1, 2, 3, 4, 5}; 
	int n = sizeof(arr) / sizeof(arr[0]); 
 
    int d = 3;
	func(arr, d, n); 
	printArray(arr, n); 
 
	return 0; 
}

A) O(1)
B) O(n)
C) O(d)
D) O(n*d)

Explanation: The input array is rotated by d in the given code. It accomplishes this by rotating the elements one at a time until the desired rotation is reached. The rotation of each element takes O(n) time, and there are d such elements in the array. As a result, the time complexity is O(n*d).

3. To rotate an array by using the algorithm of rotating its elements one by one is an in place algorithm.
A) true
B) false

Explanation: The stated algorithm’s auxiliary space requirement is O. (1). As a result, it counts as an in-place algorithm.

4. What will be the output of the following code?

#include <bits/stdc++.h> 
using namespace std; 
void func1(int arr[], int left, int right) 
{ 
	while (left < right) 
	{ 
		int temp = arr[left]; 
		arr[left] = arr[right]; 
		arr[right] = temp; 
		left++; 
		right--; 
	} 
} 
 
void func(int arr[], int d, int n) 
{ 
	func1(arr, 0, d-1); 
	func1(arr, d, n-1); 
	func1(arr, 0, n-1); 
} 
 
void printArray(int arr[], int size) 
{ 
	for (int i = 0; i < size; i++) 
	cout << arr[i] << " "; 
} 
 
int main() 
{ 
	int arr[] = {1, 2, 3, 4, 5}; 
	int n = sizeof(arr)/sizeof(arr[0]); 
	int d = 2; 
	func(arr, d, n); 
	printArray(arr, n); 
 
	return 0; 
}

A) 3 2 1 4 5
B) 3 4 5 1 2
C) 5 4 3 2 1
D) error

Explanation: The input array is rotated by 2 in the specified code. It accomplishes this by applying a reversal algorithm to various array segments. Individually, the first d elements and the rest of the sequence are reversed. The entire array is then inverted, yielding the desired rotated array. As a result, the result will be 3 4 5 1 2.

5. What will be the auxiliary space complexity of the code to rotate an array by using the reversal algorithm (d = number of rotations)?
A) O(1)
B) O(n)
C) O(d)
D) O(n*d)
Explanation: Auxiliary space is not needed by the reversal algorithm for rotating an array. As a result, the complexity of the auxiliary space would be O. (1).

6. How many arguments are required by the predefined function rotate() in C++?
A) 1
B) 2
C) 3
D) 4

Explanation: In C++, the predefined function for rotating an array is rotate(), which is part of the algorithm collection. There are three arguments needed.

7. Which of the following is the predefined function for array reversal in C++?
A) rotate()
B) arr_rotate()
C) array_rotate()
D) rot()

Explanation: In C++, the predefined function rotate() is used to rotate an array. It has three arguments and is specified by the library algorithm.

8. Predefined function rotate() in C++ is available under which header file?
A) math
B) stdio
C) stdlib
D) algorithm

Explanation: In C++, the predefined function for rotating an array is rotate(), which is found in the algorithm library. It takes three arguments, the first of which is a pointer to the array’s starting index and the second of which is a pointer to the array’s ending index. The pointer to the element that will become the first element in the rotated sequence is the middle argument.

9. Which of the following algorithm to rotate an array has the maximum time complexity?
A) rotate elements one by one
B) juggling algorithm
C) reversal algorithm
D) using a temporary array

Explanation: The algorithm that rotates elements one by one necessitates the most time complexity. It takes O(n*d) time to complete.

10. What is the time complexity of the juggling algorithm to rotate an array?
A) O(1)
B) O(n)
C) O(d)
D) O(n*d)

Explanation: The juggling algorithm has an O time complexity (n). It has an O auxiliary space complexity (1).

11. Reversal algorithm and juggling algorithm for array rotation have the same time complexity.
A) True
B) False

Explanation: The juggling algorithm, like the reversal algorithm, has an O(n) time complexity. They have the same degree of spatial complexity.

12. What will be the resulting array after rotating arr[]={1, 2, 3, 4, 5} by 2?
A) 2, 1, 3, 4, 5
B) 3, 4, 5, 1, 2
C) 4, 5, 1, 2, 3
D) 1, 2, 3, 5, 4

Explanation: When the given array is rotated by 2 then the resulting array will be
Rotation 1: {2,3,4,5,1}
Rotation 2: {3,4,5,1,2}.
Thus, the final array is {3,4,5,1,2}.

13. What will be the output of the following code?

#include <iostream>
using namespace std;
int main()
{   
    int arr[] = {1,2,3,4,5,6};
    int n = sizeof(arr)/sizeof(arr[0]);
    int d=4;
    int temp[10];
 
    for(int i=0;i<d;i++)
    temp[i]=arr[i];
 
    int j=0;
    for(int i=d;i<n;i++,j++)
    arr[j]=arr[i];
 
    int k=0;
    for(int i=n-d;i<n;i++,k++)
    arr[i]=temp[k];
 
    for(int i=0;i<n;i++)
    cout<<arr[i]<<" ";
    return 0;
}

A) 5 6 1 2 3 4
B) 6 5 4 3 1 2
C) 3 4 5 6 1 2
D) error

Explanation: The provided code rotates the array by 4 degrees. It accomplishes this by storing the first d elements in an array temp[] and then shifting them to the end of the array. As a result, the final result will be 5 6 1 2 3 4.

14. What will be the time complexity of the following code?

#include <iostream>
using namespace std;
int main()
{   
    int arr[] = {1,2,3,4,5,6};
    int n = sizeof(arr)/sizeof(arr[0]);
    int d=4;
    int temp[10];
 
    for(int i=0;i<d;i++)
    temp[i]=arr[i];
 
    int j=0;
    for(int i=d;i<n;i++,j++)
    arr[j]=arr[i];
 
    int k=0;
    for(int i=n-d;i<n;i++,k++)
    arr[i]=temp[k];
 
    for(int i=0;i<n;i++)
    cout<<arr[i]<<" ";
    return 0;
}

A) O(d)
B) O(n)
C) O(n2)
D) O(n*d)

Explanation: An input array is rotated by d in the given code. The code’s longest loop has n iterations, so the time complexity is O. (n).

15. What will be the auxiliary space complexity of the following code?

#include <iostream>
using namespace std;
int main()
{   
    int arr[] = {1,2,3,4,5,6};
    int n = sizeof(arr)/sizeof(arr[0]);
    int d=4;
    int temp[10];
 
    for(int i=0;i<d;i++)
    temp[i]=arr[i];
 
    int j=0;
    for(int i=d;i<n;i++,j++)
    arr[j]=arr[i];
 
    int k=0;
    for(int i=n-d;i<n;i++,k++)
    arr[i]=temp[k];
 
    for(int i=0;i<n;i++)
    cout<<arr[i]<<" ";
    return 0;
}

A) O(1)
B) O(n)
C) O(d)
D) O(n*d)

Explanation: An input array is rotated by d in the given code. It accomplishes this by storing the first d elements of the original array in an auxiliary array called temp[]. As a result, the complexity of the auxiliary space would be O. (d).

16. What will be the output of the following code?

#include <bits/stdc++.h> 
using namespace std; 
 
void func1(int arr[], int n) 
{ 
	int k = arr[0], i; 
	for (i = 0; i < n - 1; i++) 
		arr[i] = arr[i + 1]; 
 
	arr[i] = k; 
} 
 
void func(int arr[], int d, int n) 
{ 
	for (int i = 0; i < d; i++) 
		func1(arr, n); 
} 
 
void printArray(int arr[], int n) 
{ 
	for (int i = 0; i < n; i++) 
		cout << arr[i] << " "; 
} 
 
int main() 
{ 
	int arr[] = { 1, 2, 3, 4, 5}; 
	int n = sizeof(arr) / sizeof(arr[0]); 
 
 
	func(arr, 3, n); 
	printArray(arr, n); 
 
	return 0; 
}

A) 4 5 1 2 3
B) 3 4 5 1 2
C) 5 4 3 1 2
D) error

Explanation: The following code rotates the input sequence by three times. It accomplishes this by rotating the elements one at a time until the desired rotation is reached. As a result, the final result will be 4 5 1 2 3.

By looping through the array and performing the procedure arr[j] = arr[j+1], the array can be left rotated by moving its elements to a position prior to them. The array’s first element would be added to the rotated array’s last element. Rotation of an array definition. Consider the case where we want to ‘rotate’ the elements of an array, or move them left by one index. The element that used to be at index 0 will now be moved to the array’s last slot. 3, 8, 9, 7, 5 becomes 8, 9, 7, 5, 3 in this case.

Leave a Reply

Your email address will not be published. Required fields are marked *