To move from p to q, |p-q| must divisible by k. So if we sort the array and finds the q for all element and check whether |p-q| is divisible by k , if all the element satisfies this condition output yes else no.
Is my logic is wrong?
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t;
cin >> t;
while (t--)
{
int k, n;
cin >> n >> k;
int flag = 1;
vector<pair<int, int>> seq(n);
for (int i = 0; i < n; ++i)
{
cin >> seq[i].first;
seq[i].second = i;// storing the initial position :- q
}
sort(seq.begin(), seq.end());
for (int i = 0; i < n; ++i)
{
if ( (abs(i - seq[i].second)) % k != 0) // checking the condition
{
flag = 0;
break;
}
}
if (flag) cout << "yes"<< "\n";
else cout << "no"<< "\n";
}
return 0;
}
Its is observable that we can directly swap 2 elements Ai and Aj (i<j) if j is at some integral multiple of k position from i. So, starting at index i then traversing through all the possible positions of j keeping a record of the minimum value obtained from j’s ( and if this min value is less than a[i] ) swapping a[i] and the minimum value. After doing this for possible values of i.
If we get a sorted array then yes else no .
I thought i ll get a TLE over this but didn’t . Can somebody point out why it didnt get a TLE. [CodeChef: Practical coding for everyone]
I used a form of modified bubble sort for this question. But it barely passed the test cases in pypy CodeChef: Practical coding for everyone
When i ran the same code in Python3 it gave TLE as was expected. CodeChef: Practical coding for everyone
I suppose it would have done slightly better in C++ in terms of time. But this anomaly should not have happened between different languages. I think the tester should have looked into it.
Yes, that’s why i wrote in comment wrong solution. Actually my approach is whole wrong. It shouldn’t have been accepted. One of the participant has done the same during contest, so I was just checking it by submitting it myself.
can some one explain me why this solution is wrong…i have taken a 2d ‘f’ vector of k rows then sorted each row and merged them back to another vector ‘g’…and then checked if g is sorted or not…
and my solution is getting wrong answer for both all cases.
Yeah, even I feel the test cases are weak. I have seen some of the solutions with time complexity O(n^2). This time complexity indicates that we cannot solve the question with given constraints ( t<=100 and n<=10^5) with in 1 second. Problem setters and testers should have looked into it.
I agree with you @rajankur.
Hope this solution helps you all. I used binary search to compare the positions of each element in sorted array and unsorted array and checked whether the difference is divisible by k.
I had a similar approach but somewhat easy to understand. Hopefully, it helps
so, if an element’s original index in the array is i and its index in the sorted array is j
then because of the reason mentioned in the editorial, abs(j-i)%k==0
or I can simply write i%k == j%k
Therefore I stored original indexes(mod k) and those in a sorted array in a map and compared.
To handle duplicacy, all we need to do is to check for a particular element, the number of indexes(mod k) in the original and sorted array is equal.