GATE / CS ARCHIVE

GATE CSE 1997 / QUESTION 17

Complete a shortest-window algorithm

Algorithms · Correctness, Invariants & Design · Full GATE CSE 1997 paper

GATE 1997, Q17 5 marks · Descriptive
Report issue

Correctness, Invariants & Design

Question 17, GATE 1997: An array A[1..n], n≥1, contains positive integers, and M>0. Complete the boxes in this program so it prints the shortest nonempty contiguous-subarray length having sum at least M, or n+1 if none exists. The program uses one-based array indexing:
1 begin
2   i:=1; j:=1;
3   sum:=[B3];
4   min:=n; finish:=false;
5   while not finish do
6     if [B6] then
7       if j=n then finish:=true
8       else
9         begin
10          j:=j+1;
11          sum:=[B11]
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
Question 17, GATE 1997: An array A[1..n], n≥1, contains positive integers, and M>0. Complete the boxes in this program so it prints the shortest nonempty contiguous-subarray length having sum at least M, or n+1 if none exists. The program uses one-based array indexing:
1 begin
2   i:=1; j:=1;
3   sum:=[B3];
4   min:=n; finish:=false;
5   while not finish do
6     if [B6] then
7       if j=n then finish:=true
8       else
9         begin
10          j:=j+1;
11          sum:=[B11]
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
Tap to enlarge
Answer & solution

Answer: B3=A[1]; B6=sum<M; B11=sum+A[j]. (no official key was published)

Fill line 3 with A[1], line 6 with sum<M, and line 11 with sum+A[j]. The invariant is that sum equals A[i]+…+A[j], with value zero if the current window is empty (i=j+1).

Initially i=j=1, so sum must be A[1]. If sum<M, the current window is too small. Removing positive elements cannot help it reach M, so extend the right endpoint. Line 10 has already incremented j, which is why line 11 adds A[j], not A[j+1]. If j=n, there is no further element to add and the loop can terminate.

If sum≥M, the current window is feasible. Its actual length is j−i+1, but min deliberately stores length minus one, so line 15 compares j−i. Remove A[i] and advance i to look for a shorter feasible window with the same right endpoint. Repeated shrinking examines the best ending-at-j choice before extending again. Positivity ensures that once a left endpoint is discarded, retaining it for later right endpoints cannot improve on a feasible length already considered.

For A=[2,3,1,2] and M=5, the first feasible window is [2,3], length 2. It stores min=1. After shrinking and extending, [3,1,2] is feasible but longer. The final print is 2. If no window reaches M, min stays n and the sentinel print is n+1. If one element alone reaches M, min becomes 0 and the answer is 1; removing that element creates a temporary empty window of sum zero, which safely forces extension or termination because M>0.

Both i and j only increase, giving O(n) time and O(1) auxiliary space. Negative array elements would invalidate the monotonic shrinking argument. The strict comparison at line 6 is essential: equality sum=M already satisfies the requested threshold. Full code and its statement numbering are reconstructed from the paper pp8–9, where the boxes and assignments remain visible.

Original paper, page 8