Array · Data Structures
17 questions · asked in 12 of 27 years (2000–2026)
MCQ 15 · NAT 21-mark 13 · 2-mark 4
2000Questions per year2026
Questions 6–10 of 17
Consider the following two C code segments. Y and X are one and two dimensional arrays of size n and nxn respectively, where . Assume that in both code segments, elements of Y are initialized to 0 and each element X[i][j] of array X is initialized to i+j. Further assume that when stored in main memory all elements of X are in same main memory page frame.
Code segment 1:
S1: Final contents of array Y will be same in both code segments
S2: Elements of array X accessed inside the for loop shown in code segment 1 are contiguous in main memory
S3: Elements of array X accessed inside the for loop shown in code segment 2 are contiguous in main memory
Code segment 1:
//initialize elements of Y to 0
//initialize elements X[i][j] of X to i+j
for(i = 0; i < n; i++)
Y[i] += X[0][i]; Code Segment 2: //initialize elements of Y to 0
//initialize elements X[i][j] of X to i+j
for(i = 0; i < n; i++)
Y[i] += X[i][0]; Which of the following statements is/are correct? S1: Final contents of array Y will be same in both code segments
S2: Elements of array X accessed inside the for loop shown in code segment 1 are contiguous in main memory
S3: Elements of array X accessed inside the for loop shown in code segment 2 are contiguous in main memory
Answer: (C)
Check each statement:
- S1: segment 1 adds X[0][i] = i. Segment 2 adds X[i][0] = i. So Y is the same True
- S2: X[0][0], X[0][1], ... lie in one row, so they are contiguous in row-major order True
- S3: X[0][0], X[1][0], ... are n elements apart False
Answer: Only S1 and S2 are correct
- S1: segment 1 adds X[0][i] = i. Segment 2 adds X[i][0] = i. So Y is the same True
- S2: X[0][0], X[0][1], ... lie in one row, so they are contiguous in row-major order True
- S3: X[0][0], X[1][0], ... are n elements apart False
Answer: Only S1 and S2 are correct
The minimum number of comparisons required to determine if an integer appears more than n/2 times in a sorted array of n integers is
Answer: (B)
In a sorted array equal values sit side by side. A value with more than copies fills a block longer than half the array, so the block must cover the middle position. The only candidate is = the middle element.
Upper bound. Binary search finds the first position of in comparisons. Then has more than copies exactly when position is inside the array and holds . That is one more comparison; if the position is past the end, the answer is no. Total: .
Lower bound. Take and compare array elements. Let () be a sorted array with the same value at positions ( copies, not more than ) and all other values distinct. Let be the same array with also at position ( copies, so the answer is yes). They differ only at position . So to answer no on , an algorithm must compare position with a position of the block, and that comparison has as its left end, so it helps only this one . In these arrays a comparison gives only two outcomes (equal or less). With comparisons there are at most outcome patterns. Each pattern can answer no for at most of the arrays , so , which gives .
So is both enough and needed: (A) is more than needed, while (C) and (D) constant are less than the lower bound.
Answer: (B)
Upper bound. Binary search finds the first position of in comparisons. Then has more than copies exactly when position is inside the array and holds . That is one more comparison; if the position is past the end, the answer is no. Total: .
Lower bound. Take and compare array elements. Let () be a sorted array with the same value at positions ( copies, not more than ) and all other values distinct. Let be the same array with also at position ( copies, so the answer is yes). They differ only at position . So to answer no on , an algorithm must compare position with a position of the block, and that comparison has as its left end, so it helps only this one . In these arrays a comparison gives only two outcomes (equal or less). With comparisons there are at most outcome patterns. Each pattern can answer no for at most of the arrays , so , which gives .
So is both enough and needed: (A) is more than needed, while (C) and (D) constant are less than the lower bound.
Answer: (B)
An array of n numbers is given, where n is an even number. The maximum as well as the minimum of these n numbers needs to be determined. Which of the following is TRUE about the number of comparisons needed?
Answer: (B)
Pair up the numbers and compare each pair: comparisons. Call the larger of each pair a winner and the smaller a loser.
The maximum is a winner, since it cannot be the smaller one of its pair. The minimum is a loser for the same reason.
Find the maximum among the winners: comparisons. Find the minimum among the losers: comparisons.
Total: comparisons, so at most are needed.
(A) is false: is already less than for large and any constant c. (C) is false: is far more than . (D) is false since (B) is true.
Answer: (B)
The maximum is a winner, since it cannot be the smaller one of its pair. The minimum is a loser for the same reason.
Find the maximum among the winners: comparisons. Find the minimum among the losers: comparisons.
Total: comparisons, so at most are needed.
(A) is false: is already less than for large and any constant c. (C) is false: is far more than . (D) is false since (B) is true.
Answer: (B)
Given two arrays of numbers and where each number is 0 or 1, the fastest algorithm to find the largest span(i,j) such that , or report that there is no such span,
Answer: (C)
Let with . The span (i, j) has equal sums exactly when . So the longest span comes from two equal values of d that are farthest apart.
Each is an integer from to , so there are only possible values. Keep a table first[v] of size that stores the first index k where . Scan k = 1 to n once: compute ; if first[] is empty store k, otherwise the span length is first[]. Keep the largest. If no value repeats, there is no span.
This takes steps of constant work, using a table of size (direct indexing, no real hashing needed): time and space. Any algorithm must read all numbers, since changing one can change the answer, so time is needed. So the time is .
(A) and (B) claim much slower running times, so they cannot describe the fastest algorithm. (D) claims , which is below the needed to read the input; the parity of the total sum has no effect.
Answer: (C)
Each is an integer from to , so there are only possible values. Keep a table first[v] of size that stores the first index k where . Scan k = 1 to n once: compute ; if first[] is empty store k, otherwise the span length is first[]. Keep the largest. If no value repeats, there is no span.
This takes steps of constant work, using a table of size (direct indexing, no real hashing needed): time and space. Any algorithm must read all numbers, since changing one can change the answer, so time is needed. So the time is .
(A) and (B) claim much slower running times, so they cannot describe the fastest algorithm. (D) claims , which is below the needed to read the input; the parity of the total sum has no effect.
Answer: (C)
An element in an array X is called a leader if it is greater than all elements to the right of it in X. The best algorithm to find all leaders in an array.
Answer: (B)
Option B. Go through the array from right to left. Keep track of the largest element seen so far (call it current_max).
- Begin at the rightmost element and store it in
current_max. The rightmost element is always a leader. - Now move left. If an element is bigger than
current_max, then is a leader too. Add it to the list of leaders (or just print it). Then setcurrent_maxto and keep going left.
The time complexity is .
Option A can also be done in linear time with a stack that pops smaller elements, but that needs extra space. The official key is B.
Answer: (B)
Keep practising