My Report (&Account)

Array Data Structure Test – 2


Correct Answer: 2 points | Wrong: -1 point
Grades: A* (100% score) | A (80%-99%) | B (60%-80%) | C (40%-60%) | D (0%-40%)
advertisement

1. 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;
}

2. What is the time complexity of the juggling algorithm to rotate an array?

3. 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; 
} 

4. 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; 
} 

5. Predefined function rotate() in C++ is available under which header file?

6. 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;
}

7. What will be the resulting array after rotating arr[]={1, 2, 3, 4, 5} by 2?

8. How many arguments are required by the predefined function rotate() in C++?

9. Which of the following is the predefined function for array reversal in C++?

10. Reversal algorithm and juggling algorithm for array rotation have the same time complexity.

11. Which of the following algorithm to rotate an array has the maximum time complexity?

12. What will be the auxiliary space complexity of the code to rotate an array by using the reversal algorithm (d = number of rotations)?

13. 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; 
} 

14. 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; 
} 

15. To rotate an array by using the algorithm of rotating its elements one by one is an in place algorithm.

16. 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;
}

 

Start practicing “1000 MCQs on Data Structure”, and once you are ready, you can take tests on all topics by attempting our “Data Structure Test Series”.

advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.