Arrays & Linked Lists
![Question 14(b), GATE 1996: Delete the smallest element of an n×n row- and column-increasing array. Insert x, smaller than every item of the last row and last column, in O(n) time. Complete: i:=1;j:=1;A[i,j]:=x; while (x>blank or x>blank) do if A[i+1,j]<blank then begin A[i,j]:=A[i+1,j];i:=i+1 end else begin blank end; A[i,j]:=blank. Assume the intended full monotonicity and distinct inserted value.](../../assets/questions/CS-1996-Q72-1.webp)
![Question 14(b), GATE 1996: Delete the smallest element of an n×n row- and column-increasing array. Insert x, smaller than every item of the last row and last column, in O(n) time. Complete: i:=1;j:=1;A[i,j]:=x; while (x>blank or x>blank) do if A[i+1,j]<blank then begin A[i,j]:=A[i+1,j];i:=i+1 end else begin blank end; A[i,j]:=blank. Assume the intended full monotonicity and distinct inserted value.](../../assets/questions/CS-1996-Q72-2.webp)
![Question 14(b), GATE 1996: Delete the smallest element of an n×n row- and column-increasing array. Insert x, smaller than every item of the last row and last column, in O(n) time. Complete: i:=1;j:=1;A[i,j]:=x; while (x>blank or x>blank) do if A[i+1,j]<blank then begin A[i,j]:=A[i+1,j];i:=i+1 end else begin blank end; A[i,j]:=blank. Assume the intended full monotonicity and distinct inserted value.](../../assets/questions/CS-1996-Q72-3.webp)
Answer & solution
Answer: Compare x to A[i+1,j],A[i,j+1]; choose their minimum; shift it up/left; advance i or j; finally write x. (no official key was published)
A complete hole-moving version is: i:=1; j:=1; A[i,j]:=x; while (x>A[i+1,j]) or (x>A[i,j+1]) do begin if A[i+1,j]<A[i,j+1] then begin A[i,j]:=A[i+1,j]; i:=i+1 end else begin A[i,j]:=A[i,j+1]; j:=j+1 end end; A[i,j]:=x.
Treat the current cell as a hole until the final assignment. If both children are greater than x, x belongs there. Otherwise the smaller of the downward and rightward neighbors is the smallest candidate that can fill the hole without violating either row or column order. Move it into the hole, advance to its old position, and repeat. Along the moved path, predecessors remain smaller than the element shifted into place; untouched cells preserve their old comparisons. The terminal hole receives x.
Each iteration increases i+j by one, hence there are O(n) iterations and O(1) extra storage. The given strict upper bound on x prevents the hole from entering the last row or last column: a boundary neighbor is greater than x and cannot be the smaller neighbor selected while the loop is true. Thus the two neighbor accesses are safe for n≥2. Without that promise use explicit bounds or +∞ sentinels.
Distinct x is a sufficient extra condition for preserving strict order. Without it the guarantee can fail: consider [[0,10,40],[20,30,50],[60,70,80]] with x=10. Here x is below every last-row/column entry, yet the procedure stops with adjacent equal values in the first row. Equal entries can sometimes occupy incomparable cells, but strict preservation is not guaranteed by the printed promise. If duplicates are allowed, interpret sorted as nondecreasing instead. Finally, the literal source quantifier omits boundary comparisons, so the algorithm relies on the intended full row/column definition discussed in part (a).