GATE / CS ARCHIVE

GATE CSE 1999 / QUESTION 1.13

Minimum swaps for sign partition

Algorithms · Searching, Sorting & Selection · Full GATE CSE 1999 paper

GATE 1999, Q1.13 1 mark · MCQ
Report issue

Searching, Sorting & Selection

Question 1.13, GATE 1999: Partition n array values so negatives precede positives. What is the minimum number of arbitrary exchanges in the worst case: n−1,n,n+1,or none?
Tap to enlarge
Your answer
Answer & solution

Answer: D: floor(n/2) (no official key was published)

Suppose there are k negative values. Their final region is the first k positions. Let t positives currently lie in that region; exactly t negatives then lie outside it. Pair and swap each misplaced positive with a misplaced negative, requiring t exchanges. Every exchange fixes at most one misplaced negative in the target region, proving optimality. Since t≤min(k,n−k), the worst case over k is floor(n/2), attained by placing balanced sign groups in the wrong halves. Thus none of the listed formulas A–C is the general answer. This uses arbitrary swaps, not adjacent swaps.

Original paper, page 2