In this problem your goal is to sort an array consisting of n integers in at most n swaps. For the given array find the sequence of swaps that makes the array sorted in the non-descending order. Swaps are performed consecutively, one after another.

Note that in this problem you do not have to minimize the number of swaps — your task is to find any sequence that is no longer than n.


-----Input-----

The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of array elements. The second line contains elements of array: a_0, a_1, ..., a_{n} - 1 ( - 10^9 ≤ a_{i} ≤ 10^9), where a_{i} is the i-th element of the array. The elements are numerated from 0 to n - 1 from left to right. Some integers may appear in the array more than once.


-----Output-----

In the first line print k (0 ≤ k ≤ n) — the number of swaps. Next k lines must contain the descriptions of the k swaps, one per line. Each swap should be printed as a pair of integers i, j (0 ≤ i, j ≤ n - 1), representing the swap of elements a_{i} and a_{j}. You can print indices in the pairs in any order. The swaps are performed in the order they appear in the output, from the first to the last. It is allowed to print i = j and swap the same pair of elements multiple times.

If there are multiple answers, print any of them. It is guaranteed that at least one answer exists.


-----Examples-----
Input
5
5 2 5 1 4

Output
2
0 3
4 2

Input
6
10 20 20 40 60 60

Output
0

Input
2
101 100

Output
1
0 1

Difficulty: interview

Starter code:

Solutions:
import operator


n = int(input())
xs = list(map(int, str.split(input())))
if xs == sorted(xs):

    print(0)

else:

    swaps = []
    counter = 0
    while xs:

        i = xs.index(min(xs))
        if i:

            swaps.append(str.format("{} {}", counter, i + counter))

        xs[0], xs[i] = xs[i], xs[0]
        xs.pop(0)
        counter += 1

    print(len(swaps))
    print(str.join("\n", swaps))




n = int(input())
a = [int(s) for s in input().split()]

b = sorted(a)

count = 0
swaps = []
for i in range(n):
    if a[i]!=b[i]:
        count += 1
        for j in range(i,n):
            if a[j]==b[i]:
                a[i],a[j]=a[j],a[i]
                swaps.append((i,j))
                break

print(count)
for i,j in swaps:
    print(i,j)
        
        
##while 1>0:
##    line = input()
##    if len(line.split())==0:
##        break
##    print(line)


'''
Created on 17-Nov-2014

@author: akash
'''
def findmin(lst, i):
    minn = i
    minval = lst[i]
    for j in range(i, len(lst)):
        if minval > lst[j]:
            minval = lst[j]
            minn = j
    return minn
n = int(input())
lst = list(map(int, input().split()))
ans = []
for i in range(n):
    j = findmin(lst, i)
    if i != j:
        lst[i], lst[j] = lst[j], lst[i]
        ans.append([i, j])
print(len(ans))
for i in range(len(ans)):
    print(ans[i][0], " ", ans[i][1])


n = int(input(''))

arvud = input('').split( )
arvud = list(map(int, arvud))
swaps = []
begin = 0

for i in range(n):
    mini = min(arvud)
    index = arvud.index(mini)
    if index != 0:
        arvud[0], arvud[index] = arvud[index], arvud[0]
        swaps.append((begin, index+begin))
        
    arvud.pop(0)
    begin+=1
        

print(len(swaps))
for i in range(len(swaps)):
    print(swaps[i][0], " ", swaps[i][1])


n = input()

src = list(int(x) for x in input().split())
cp = src.copy()

cp.sort()

def search(arr, val, border):
	for x in range(border, len(arr)):
		if arr[x] == val:
			return x
	print("ERROR!")

pairs = list()
def main():
	for index, val in enumerate(cp):
		tmp = src[index]
		if val != tmp:
			swap = search(src, val, index)
			pairs.append((index, swap))
			src[index] = src[swap]
			src[swap] = tmp
	
	print(len(pairs))
	for x, y in pairs:
		print(x,y)
		
	
main()

import operator


n = int(input())
xs = list(map(int, str.split(input())))
if xs == sorted(xs):

    print(0)

else:

    swaps = []
    counter = 0
    while xs:

        i = xs.index(min(xs))
        if i:

            swaps.append(str.format("{} {}", counter, i + counter))

        xs[0], xs[i] = xs[i], xs[0]
        xs.pop(0)
        counter += 1

    print(len(swaps))
    print(str.join("\n", swaps))

import bisect

n = int(input())
a = list(map(int, input().split()))
ans = []

for i in range(n):
    j = min(list(range(i, n)), key = lambda k: a[k])
    if j != i:
        a[i], a[j] = a[j], a[i]
        ans.append((i, j))

print(len(ans))
for i, j in ans: print(i, j)



n = int(input())
a = list(map(int, input().split()))
ans = []

for i in range(n):
    j = min(range(i, n), key = lambda k: a[k])
    if j != i:
        a[i], a[j] = a[j], a[i]
        ans.append((i, j))

print(len(ans))
for i, j in ans: print(i, j)

n = int(input())
a = list(map(int, input().split()))
perms=[]
for i in range(n):
    min_i = min(list(range(i,n)), key=lambda x:a[x])
 
    if min_i !=i:
        perms.append([i,min_i])
        a[i], a[min_i] = a[min_i], a[i]
       
print(len(perms))
for p1,p2 in perms:
    print(p1,p2)


def solve(xs):
    oxs = xs[:]
    oxs.sort()
    res = []
    for i in range(len(xs)):
        if xs[i] != oxs[i]:
            j = i+1
            while not(oxs[i] == xs[j] and xs[j] != oxs[j]):
                j+=1
            res.append([i, j])
            xs[i], xs[j] = xs[j], xs[i]
    
    return res
    
def __starting_point():
    input()
    xs = list(map(int, input().split()))
    ret = solve(xs)
    print(len(ret))
    if ret : print(
        "\n".join(
            "{0[0]} {0[1]}".format(swp) for swp in ret
        )
    )
__starting_point()

n = int(input())
a = [int(m) for m in input().split()]
counter = 0
exit_list = []
i = 0
while i < n:
    sublist = a[i:]
    minimum = sublist.index(min(sublist)) + i
    if i != minimum:
        exit_list.append((i, minimum))
        c = a[minimum]
        a[minimum] = a[i]
        a[i] = c
        counter += 1
    i += 1
print(counter)
for x in exit_list:
    print(x[0], x[1])

def find_min(i):
    min = 1000000001
    index = 0
    for j in range(i, n):
        if a[j] < min:
            min = a[j]
            index = j
    return index

n = int(input())
a = input().split()
for i in range(n):
    a[i] = int(a[i])

k = 0
ans = ()

for i in range(n):
    m = find_min(i)
    if i != m:
        a[i] = a[i] + a[m]
        a[m] = a[i] - a[m]
        a[i] = a[i] - a[m]
        ans += (i, m)
        k += 1
    
print(k)
for i in range(0, len(ans), 2):
    print(ans[i], ans[i+1])

def find_min(i):
    min = 1000000001
    index = 0
    for j in range(i, n):
        if a[j] < min:
            min = a[j]
            index = j
    return index

n = int(input())
a = input().split()
for i in range(n):
    a[i] = int(a[i])

k = 0
ans = ()

for i in range(n):
    m = find_min(i)  # ����� ����������� �������� �� �������
    if i != m:
        a[i] = a[i] + a[m]
        a[m] = a[i] - a[m]
        a[i] = a[i] - a[m]
        # �������� ��������
        ans += (i, m)
        k += 1
    
print(k)
for i in range(0, len(ans), 2):
    print(ans[i], ans[i+1])

def main():
    n = int(input())
    l = list(map(int, input().split()))
    l = sorted((x, i) for i, x in enumerate(l))
    l = sorted((i, j) for j, (x, i) in enumerate(l))
    l = [j for x, j in l]
    index = [0] * n
    for i, x in enumerate(l):
        index[x] = i
    res = []
    for i, x in enumerate(index):
        y = l[i]
        if i != y:
            res.append((i, x))
            l[i], l[x] = i, y
            index[y] = x
    print(len(res))
    for a in res:
        print(*a)


def __starting_point():
    main()

__starting_point()

def input_split(f): return list(map(f, input().split()))


def main():
    n = int(input())
    a = list(input_split(int))

    k = 0
    res = []
    for i in range(0, n-1):
        m = a[i]
        index = 0
        for j in range(i+1, n):
            if m > a[j]:
                m = a[j]
                index = j
        if m != a[i]:
            k += 1
            a[i], a[index] = m, a[i]
            res.append((i, index))

    print(str(k))
    for v in res:
        print(" ".join(map(str, v)))


def __starting_point():
    main()

__starting_point()