GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

Wednesday, 1 June 2022

[Solution] Dearrange sorting Solution CodeChef Solution | CodeChef Problem Solution 2022

You are given a permutation P of length N. A permutation of length N is an array where every element from 1 to N occurs exactly once.

You must perform some operations on the array to make it sorted in increasing order. In one operation, you must:

  • Select two indices L and R (1≤L<R≤N)
  • Completely dearrange the subarray PL,PL+1,…PR

A dearrangement of an array A is any permutation B of A for 

Solution Click Below:-  CLICK HERE

which Bi≠Ai for all i.

For example, consider the array A=[2,1,3,4]. Some examples of dearrangements of A are [1,2,4,3], [3,2,4,1] and [4,3,2,1]. [3,5,2,1] is not a valid dearrangement since it is not a permutation of A. [1,2,3,4] is not a valid dearrangement either since B3=A3 and B4=A4.

Find the minimum number of operations required to sort the array in increasing order. It is guaranteed that the given permutation can be sorted in atmost 1000 operations.

Input Format

  • The first line contains a single integer T — the number of test cases. Then the test cases follow.
  • The first line of each test case contains an integer N — the size of the permutation P.

  • The second line of each test case contains N space-separated integers P1,P2,…,PN denoting the permutation P.

Output Format

  • On the first line of each test case output the minimum number of operations M. The description of the M operations must follow.
  • Each operation must be described in two lines
    • On the first line of each operation output two space separated integers L and R (1≤L<R≤N) — the indices of the subarray chosen.
    • On the second line output N space separated integers — the permutation P after dearranging the subarray PL,PL+1,…PR. Note that you have to output the whole permutation {P1,P2,…PN}

Constraints

  • 1≤T≤200
  • 3≤N≤1000
  • P is a permutation of length N
  • The sum of N over all test cases does not exceed 1000
  • It is guaranteed that the given permutations can be sorted in atmost 1000 operations.

No comments:

Post a Comment