# Array and Array Operations – 2

Subject:  Data Structure

Topic: Array and Array Operations

Difficulty Level: Difficult

No. of Questions: 10

#### 11. What is the time complexity of finding the maximum element in an unsorted array?

a) O(1)

b) O(log n)

c) O(n)

d) O(n^2)

Answer Explanation: Finding the maximum element in an unsorted array requires checking each element, resulting in a time complexity of O(n).

#### 12. How does the time complexity change when inserting an element at the middle of a dynamic array that requires resizing?

a) O(1)

b) O(n)

c) O(n log n)

d) O(n^2)

Answer Explanation: Inserting an element in the middle requires shifting elements to the right. If resizing is needed, the overall complexity remains O(n).

#### 13. What is the output of the following code snippet?``````int arr[] = {4, 7, 1, 3, 9, 2}; int n = sizeof(arr) / sizeof(arr[0]); int sum = 0; for(int i = 0; i < n; i += 2) { sum += arr[i]; } printf("%d", sum);``````

a) 10

b) 14

c) 16

d) 20

Answer Explanation: The sum of elements at even indices (0, 2, 4) is 4 + 1 + 9 = 14.

#### 14. Which sorting algorithm is most efficient for sorting a small array and why?

a) Quick sort

b) Merge sort

c) Insertion sort

d) Bubble sort

Answer Explanation: Insertion sort is efficient for small arrays because it has a low overhead and performs well on nearly sorted data.

#### 15. Given the array int arr[] = {5, 3, 6, 2, 10}, which of the following represents the array after performing one iteration of the selection sort algorithm?

a) {2, 3, 6, 5, 10}

b) {3, 5, 6, 2, 10}

c) {5, 6, 3, 2, 10}

d) {2, 5, 6, 3, 10}

Answer: d) {2, 5, 6, 3, 10}

Answer Explanation: The smallest element (2) is swapped with the first element (5).

#### 16. How do you reverse an array in-place in C?

a) Using a temporary array

b) Using the standard library function

c) Swapping elements from ends towards the middle

d) By sorting the array in descending order

Answer: c) Swapping elements from ends towards the middle

Answer Explanation: To reverse an array in-place, swap the elements from the two ends towards the middle.

#### 17. What is the best method to find a missing number in an array containing integers from 1 to n with one missing number?

a) Sorting the array

b) Using a hash table

c) Summing the first n natural numbers and subtracting the array sum

d) Using binary search

Answer: c) Summing the first n natural numbers and subtracting the array sum

Answer Explanation: Sum the first n natural numbers using the formula n*(n+1)/2 and subtract the sum of the array elements to find the missing number.

#### 18. Given an array with the following elements: {15, 22, 13, 27, 12}, what will be the index of the pivot after the first partitioning step in the quicksort algorithm?

a) 0

b) 2

c) 3

d) 4

Answer Explanation: After the first partitioning step, the pivot (12) will be placed at index 2, with all smaller elements to the left and larger to the right.

#### 19. Which of the following algorithms can be used to find the kth smallest element in an unsorted array?

a) Merge sort

b) Quick select

c) Bubble sort

d) Binary search

Answer Explanation: Quick select is a selection algorithm to find the kth smallest element in an unordered list, and it is related to the quicksort algorithm.

a) O(n log n)

b) O(n^2)

c) O(log n)

d) O(n)