Similar questions 1 more →
Checking increasing adjacent differences
Correctness, Invariants & Design
![Question 38, GATE 2026: For integer A[1…n], determine whether for every i>j in {1,…,n−1}, A[i+1]−A[i] > A[j+1]−A[j]. Choose optimal worst-case time: A Θ(n); B Θ(log n); C Θ(n log n); D Θ(n²).](../../../assets/questions/CS-2026-S2-Q38-1.webp)
Answer & solution
Official answer: A
Givens and goal
For integer A[1…n], determine whether for every i>j in {1,…,n−1}, A[i+1]−A[i] > A[j+1]−A[j]. Choose optimal worst-case time: A Θ(n); B Θ(log n); C Θ(n log n); D Θ(n²).
Approach
Reduce all pairwise comparisons to adjacent comparisons using transitivity.
Step-by-step solution
1. Define d_i=A[i+1]−A[i]. The condition says each later difference is larger than every earlier one. This is equivalent to d_1<d_2<…<d_(n−1).
2. The forward implication includes all adjacent pairs. Conversely, if adjacent inequalities hold, chaining d_j<d_(j+1)<…<d_i proves the required inequality for every i>j.
3. Algorithm: if n≤2 return true; set previous=A[2]−A[1]; for i=2,…,n−1 set current=A[i+1]−A[i]; if current≤previous return false; otherwise set previous=current. If the loop ends return true. Only O(1) extra storage is needed.
4. The loop uses O(n) time. For a lower bound, consider a valid array such as A[i]=i², whose differences are strictly increasing. If any position is unread, an adversary can change that entry sufficiently to violate a neighboring comparison while leaving all inspected values unchanged. Thus worst-case inspection requires Ω(n) entries for n≥3.
5. The optimal bound is Θ(n), A. Use sufficiently wide arithmetic so subtraction does not overflow; the proof is in the mathematical-integer model.
![Question 42, GATE 2024: For positive X[0..n−1], initialize first=second=len1=len2=maxlen=0. For each v=X[i]: if v==first increment both len1,len2; else if v==second increment len2, set len1=P, second=first; else set len2=Q, len1=1, second=first. Update maxlen=max(maxlen,len2), then first=v. len1 is the equal-value suffix length; len2 is the suffix length with at most two distinct values. Choose P,Q: A len1+1,len2+1; B 1,len1+1; C 1,len2+1; D len2+1,len1+1.](../../../assets/questions/CS-2024-S2-Q42-1.webp)

![Question 63, GATE 2015: DOSOMETHING(c,a,n) receives a bit array c[0..k−1] and positive integers a,n. It sets z=1; then for i=0,...,k−1 sets z=z² mod n, and, if c[i]=1, sets z=(z×a) mod n; finally it returns z. For k=4, c=(1,0,1,1), a=2, n=8, what is returned?](../../../assets/questions/CS-2015-S3-Q63-1.webp)


![Question 2.15, GATE 2000: For array s[1…n] and 1≤k≤n, execute reverse(s,1,k),reverse(s,k+1,n),reverse(s,1,n). Does this rotate left by k,leave unchanged,reverse everything,or none?](../../../assets/questions/CS-2000-Q38-1.webp)
![Question 2.15, GATE 2000: For array s[1…n] and 1≤k≤n, execute reverse(s,1,k),reverse(s,k+1,n),reverse(s,1,n). Does this rotate left by k,leave unchanged,reverse everything,or none?](../../../assets/questions/CS-2000-Q38-2.webp)
![Question 14(b), GATE 2000: Let m,n be positive integers and let the line be y=(n/m)x. Define A=mq−np for endpoint (p,q), and B=ms−nr for endpoint (r,s). Complete clash: initialize false; line4 tests A [box] 0; line5 tests B [box] 0; line6 tests A [box] 0 and B [box] 0; line7 tests A [box] 0 and B [box] 0. Each successful test sets clash=true.](../../../assets/questions/CS-2000-Q76-1.webp)
![Question 14(b), GATE 2000: Let m,n be positive integers and let the line be y=(n/m)x. Define A=mq−np for endpoint (p,q), and B=ms−nr for endpoint (r,s). Complete clash: initialize false; line4 tests A [box] 0; line5 tests B [box] 0; line6 tests A [box] 0 and B [box] 0; line7 tests A [box] 0 and B [box] 0. Each successful test sets clash=true.](../../../assets/questions/CS-2000-Q76-2.webp)
![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](../../../assets/questions/CS-1997-Q74-1.webp)
![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](../../../assets/questions/CS-1997-Q74-2.webp)

![Question 7, GATE 1994: A[0..n−1] contains n integers. Shift it cyclically left K places, 1≤K≤n−1, in linear time without another array. Fill the five blanks in this skeleton.
min:=n; i:=0;
while ___ do begin
temp:=A[i]; j:=i;
while ___ do begin
A[j]:=___; j:=(j+K) mod n;
if j<min then min:=j;
end;
A[(n+i-K) mod n]:=___;
i:=___;
end;](../../../assets/questions/CS-1994-Q53-1.webp)
![Question 12, GATE 1993: For integer A[0..n-1,0..n-1], complete four boxes in: max:=[A]; i:=0; j:=0; while [B] do begin if A[i,j]>max then max:=A[i,j]; if [C] then j:=j+1 else begin j:=0; i:=[D] end end. Use a single loop to find the largest entry. The source's comma/semicolon punctuation is defective; boxes A–D are one coordinated question.](../../../assets/questions/CS-1993-Q48-1.webp)




