in Algorithms edited by
555 views
8 votes
8 votes

An inversion in an array $a$ is a pair of array indices $(i, j)$ such that $i<j$ but $a[i]>a[j]$.

What is the maximum number of inversions that can be eliminated by the following program fragment?

tmp = a[5] ; 
a[5] = a[10] ; 
a[10]= tmp ;
  1. $5$
  2. $6$
  3. $9$
  4. $20$
in Algorithms edited by
555 views

1 comment

$ \large{\colorbox{yellow}{Detailed video solution of this question with direct time stamp}}$
All India Mock Test 2 - Solutions Part 2

0
0

1 Answer

3 votes
3 votes
In the best case, $a[5]$ is larger than $a[6]$ through $a[10],$ and $a[10]$ is smaller than $a[5]$ through $a[9]$. In this case, the following nine inversions are eliminated: $(5,6),(5,7),(5,8),(5,9),(5,10)$, $(6,10),(7,10),(8,10)$, and $(9,10)$.
edited by

1 comment

sample eg, as mentioned in ans
21|15|13|12|11|10
so, worst case(max inversions scenario):-
EVERY inner ele (ie a[6] – a[9]) have inversions with both border elements(a[5],a[10]) and also within themselves too
Now, if border ele are swapped
then, no inner ele has inversions with border elements, then per inner element, 2 inversions decreases, and 1 more inversion decrease(between the border elements
so 2*4 +1=9

2
2
Answer:

Related questions