Q32
MCQ2 marks, −0.67AlgorithmsConsider an array that contains positive integers. A subarray of is defined to be a sequence of array locations with consecutive indices. The code snippet given below has been written to compute the length of the longest subarray of that contains at most two distinct integers. The code has two missing expressions labelled and .
int first=0, second=0, len1=0, len2=0, maxlen=0;
for (int i=0; i < n; i++) {
if (X[i] == first) {
len2++; len1++;
} else if (X[i] == second) {
len2++;
len1 = (P);
second = first;
} else {
len2 = (Q);
len1 = 1; second = first;
}
if (len2 > maxlen) {
maxlen = len2;
}
first = X[i];
}
Which one of the following options gives the CORRECT missing expressions? (Hint: At the end of the -th iteration, the value of is the length of the longest subarray ending with that contains all equal values, and is the length of the longest subarray ending with that contains at most two distinct values.)
Keys: A–D or 1–4 choose, Enter checks, ← → move between questions.
The solution is hidden while you attempt this question. It opens once you check or show the answer.