GUPTA MECHANICAL

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

Wednesday 13 July 2022

[Solution] End Sorted CodeChef Solution



Chef considers a permutation P of {1,2,3,,N} End Sorted if and only if P1=1 and PN=N.

Chef is given a permutation P.

In one operation Chef can choose any index i (1iN1) and swap Pi and Pi+1. Determine the minimum number of operations required by Chef to make the permutation P End Sorted.

Note: An array P is said to be a permutation of {1,2,3,,N} if P contains each element of {1,2,3,,N} exactly once.

Input Format

  • The first line of input will contain a single integer T, denoting the number of test cases.
Solution Click Below:-  👉CLICK HERE👈
👇👇👇👇👇


  • Each test case consists of two lines of input.
    • The first line of each test case contains a single integer N, denoting the length of the permutation P.


    • The second line contains N space-separated integers P1,P2,P3,,PN, denoting the permutation P.

Output Format

For each test case, output minimum number of operations required by Chef to make the permutation P End Sorted.

Constraints

  • 1T1000
  • 2N105

No comments:

Post a Comment