1525B - Permutation Sort - CodeForces Solution


constructive algorithms greedy *900

Please click on ads to support us..

Python Code:

for run in range(int(input())):
    n=int(input())
    lst=list(map(int,input().split()))
    cpy=lst.copy()
    cpy.sort()
    if lst==cpy:
        print(0)
    elif lst[0]==1 or lst[-1]==n:
        print(1)
    elif lst[0] == n and lst[-1] ==1:
        print(3)
    else:
        print(2)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int a[101];
int b[100];
int main()
{
	int t;
	cin >> t;
	for (int l=0; l<t; ++l)
    {
        int n, v=2;
        cin >> n;
        for (int i=1; i<=n; ++i)
        {
            cin >> a[i];
            b[i]=a[i];
        }
        sort(b+1, b+n+1);
        bool f=true;
        for (int i=1; i<=n; ++i)
        {
            if (a[i]!=b[i]) {f=false; break;}
        }
        if (f) {cout << "0 \n"; continue;}
        else if (a[1]==1 || a[n]==n) {cout << "1 \n"; continue;}
        else if (a[1]==n && a[n]==1) {cout << "3 \n"; continue;}
        cout << "2 \n";
	}
}


Comments

Submit
0 Comments
More Questions

1668A - Direction Change
1667B - Optimal Partition
1668B - Social Distance
88B - Keyboard
580B - Kefa and Company
960A - Check the string
1220A - Cards
897A - Scarborough Fair
1433B - Yet Another Bookshelf
1283B - Candies Division
1451B - Non-Substring Subsequence
1408B - Arrays Sum
1430A - Number of Apartments
1475A - Odd Divisor
1454B - Unique Bid Auction
978C - Letters
501B - Misha and Changing Handles
1496A - Split it
1666L - Labyrinth
1294B - Collecting Packages
1642B - Power Walking
1424M - Ancient Language
600C - Make Palindrome
1669D - Colorful Stamp
1669B - Triple
1669A - Division
1669H - Maximal AND
1669E - 2-Letter Strings
483A - Counterexample
3C - Tic-tac-toe