SHUFFLE - Code CHEF Easy Question
Hello Everyone !
Today , we will be discussing an easy question on code Chef
The question is Another Birthday Present code named SHUFFLE
Here is a link to the PROBLEM
So the question is pretty straightforward
You are given n numbers and an integer k , you can swap Ai and Ai+k
You need to find out if you can sort the numbers or not .
So coming to the prerequisites of this question , they are :-
1. Vectors/Arrays
2. Sorting Methods
3. Simple Algebra
So , let me give you the solution to the problem .
So by the given swaps you can only sort the numbers at the i+kth positions in the given unsorted numbers initially.
That means that in the given unsorted list of numbers , the original position of every number should be i + n*k
where i is its position in unsorted list and k is given and n can take any positive or negative value .
Hence , we just implement this algorithm to check , the moment we find a number which does not obey the law given above we quit the loop and say it cant be sorted .
Please take a look at the C++ code given below to the understand the solution more correctly and do leave your doubts in the comments section
Code:-
#include <iostream>
#include<vector>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
long int n,k;
cin>>n;
cin>>k;
long int i=0;
vector<pair<long long int,int>> v;
long long int arr[n];
while(i!=n)
{
cin>>arr[i];
v.push_back(make_pair(arr[i],i));
i++;
}
sort(v.begin(),v.end());
i=0;
while(i!=n)
{
arr[v[i].second]=i+1;
i++;
}
int ans=1;
int flag=1;
int count;
int j=0;
int x=-5;
i=0;
for(i=0;i<n;i++)
{
if(v[i].first!=x)
{
x=v[i].first;
v[i].first=i;
count=1;
}
else
{
v[i].first=i-count;
count++;
}
}
count=0;
i=0;
while(i<n)
{
if(i<n-1 && v[i+1].first!=v[i].first)
{
count=0;
if(v[i].second%k==(v[i].first%k))
{
// cout<<"ok";
}
else
{
//cout<<"hello"<<i;
flag=0;
ans=0;
}
i++;
}
else if(i==n-1)
{
count=0;
if(v[i].second%k==(v[i].first%k))
{
//cout<<"ok";
}
else
{
flag=0;
ans=0;
}
i++;
}
else
{ count=0;
int z=1;
while(v[i].first==v[i+1].first)
{
count++;
i++;
}
i=i-count;
count++;
int arr3[count]={0};
for(j=i;j<i+count;j++)
{z=0;
for(int f=0;f<count;f++)
{
if(v[j].second%k==(i+f)%k && arr3[f]==0)
{
//cout<<i+f<<" ";
arr3[f]=1;
z=1;
break;
}
}
if(z==0)
{
flag=0;
ans=0;
break;
}
}
i=i+count;
}
if(flag==0)
{
break;
}
}
if(ans==1)
{
cout<<"yes"<<endl;
}
else
{
cout<<"no"<<endl;
}
}
// your code goes here
return 0;
}
Today , we will be discussing an easy question on code Chef
The question is Another Birthday Present code named SHUFFLE
Here is a link to the PROBLEM
So the question is pretty straightforward
You are given n numbers and an integer k , you can swap Ai and Ai+k
You need to find out if you can sort the numbers or not .
So coming to the prerequisites of this question , they are :-
1. Vectors/Arrays
2. Sorting Methods
3. Simple Algebra
So , let me give you the solution to the problem .
So by the given swaps you can only sort the numbers at the i+kth positions in the given unsorted numbers initially.
That means that in the given unsorted list of numbers , the original position of every number should be i + n*k
where i is its position in unsorted list and k is given and n can take any positive or negative value .
Hence , we just implement this algorithm to check , the moment we find a number which does not obey the law given above we quit the loop and say it cant be sorted .
Please take a look at the C++ code given below to the understand the solution more correctly and do leave your doubts in the comments section
Code:-
#include <iostream>
#include<vector>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
long int n,k;
cin>>n;
cin>>k;
long int i=0;
vector<pair<long long int,int>> v;
long long int arr[n];
while(i!=n)
{
cin>>arr[i];
v.push_back(make_pair(arr[i],i));
i++;
}
sort(v.begin(),v.end());
i=0;
while(i!=n)
{
arr[v[i].second]=i+1;
i++;
}
int ans=1;
int flag=1;
int count;
int j=0;
int x=-5;
i=0;
for(i=0;i<n;i++)
{
if(v[i].first!=x)
{
x=v[i].first;
v[i].first=i;
count=1;
}
else
{
v[i].first=i-count;
count++;
}
}
count=0;
i=0;
while(i<n)
{
if(i<n-1 && v[i+1].first!=v[i].first)
{
count=0;
if(v[i].second%k==(v[i].first%k))
{
// cout<<"ok";
}
else
{
//cout<<"hello"<<i;
flag=0;
ans=0;
}
i++;
}
else if(i==n-1)
{
count=0;
if(v[i].second%k==(v[i].first%k))
{
//cout<<"ok";
}
else
{
flag=0;
ans=0;
}
i++;
}
else
{ count=0;
int z=1;
while(v[i].first==v[i+1].first)
{
count++;
i++;
}
i=i-count;
count++;
int arr3[count]={0};
for(j=i;j<i+count;j++)
{z=0;
for(int f=0;f<count;f++)
{
if(v[j].second%k==(i+f)%k && arr3[f]==0)
{
//cout<<i+f<<" ";
arr3[f]=1;
z=1;
break;
}
}
if(z==0)
{
flag=0;
ans=0;
break;
}
}
i=i+count;
}
if(flag==0)
{
break;
}
}
if(ans==1)
{
cout<<"yes"<<endl;
}
else
{
cout<<"no"<<endl;
}
}
// your code goes here
return 0;
}
Comments
Post a Comment