An array A contains n ≥ 1 positive integers in the locations A[1], A[2], …,…

PYQ GATE 1997

An array A contains n ≥ 1 positive integers in the locations A[1], A[2], …, A[n]. The following program fragment prints the length of a shortest sequence of consecutive elements of A, A[i], A[i+1], …, A[j], such that the sum of their values is ≥ M, a given positive number. It prints n + 1 if no such sequence exists. Complete the program by filling in the boxes. In each case use the simplest possible expression. Write only the line number and the contents of the box.

1.  begin
2.      i := 1; j := 1;
3.      sum := □;
4.      min := n; finish := false;
5.      while not finish do
6.          if □ then
7.              if j = n then finish := true
8.              else
9.              begin
10.                 j := j + 1;
11.                 sum := □;
12.             end
13.         else
14.         begin
15.             if (j - i) < min then min := j - i;
16.             sum := sum - A[i];
17.             i := i + 1;
18.         end;
19.     writeln(min + 1);
20. end.

Attempted by 2 students.

Sign up free to check your answer

Sign up free

Explore the full course: Zero To Hero

Loading lesson…