1823B - Sort with Step - CodeForces Solution


brute force math sortings *900

Please click on ads to support us..

Python Code:

import math

for _ in range(int(input())):
    n,k=map(int,input().split())
    p=list(map(int,input().split()))
    c=0
    for i in range(n):
        if (p[i]-i-1)%k!=0:
            c+=1
    c=math.ceil(c/2)
    
    if c==0:
        print(0)
    elif c==1:
        print(1)
    else:
        print(-1)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    
    while(t--){
 
int n,k,c=0,x;
cin>>n>>k;
for(int i=1;i<n+1;i++){
cin>>x;
c+=i%k!=x%k;
 
      }
cout<<((c>2)?-1:c/2)<<endl;
}
}


Comments

Submit
0 Comments
More Questions

1277C - As Simple as One and Two
1301A - Three Strings
460A - Vasya and Socks
1624C - Division by Two and Permutation
1288A - Deadline
1617A - Forbidden Subsequence
914A - Perfect Squares
873D - Merge Sort
1251A - Broken Keyboard
463B - Caisa and Pylons
584A - Olesya and Rodion
799A - Carrot Cakes
1569B - Chess Tournament
1047B - Cover Points
1381B - Unmerge
1256A - Payment Without Change
908B - New Year and Buggy Bot
979A - Pizza Pizza Pizza
731A - Night at the Museum
742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers
1360E - Polygon
1517D - Explorer Space
1230B - Ania and Minimizing
1201A - Important Exam
676A - Nicholas and Permutation
431A - Black Square
474B - Worms
987B - High School Become Human
1223A - CME