We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

Hi, dk201966. Probably you are facing problems because the order you are doing the changes.
If you copy the value from the 1st position to the 2nd one and, after this, you copy the 2nd to the 3rd, at the end you will have all the positions with the 1st value.
And don't forget saving the last value before overwriting it!
PS: you'll probably face other issues with this challenge...

The main problem in the code is the order of the for loop used for rotating the elenets in the array.
You should store the last element value in some temporary variable..and then use a decreasing counter in the mentioned for loop ..then u can store the value in temporary variable to the first element
The error in ur code of overwriting values is eleminated by this code!👍

I did the same and realized the values of the elements of the array that you assign later has already replaced the other element in the array. For eg: a[1]=a[0] then a[2]=a[1]=a[0] which is equal to each other

#include<cmath>#include<cstdio>#include<vector>#include<iostream>#include<algorithm>usingnamespacestd;intmain(){/* Enter your code here. Read input from STDIN. Print output to STDOUT */intn,k,q,i,s=0,e=0,t=0,b;cin>>n>>k>>q;inta[n];for(i=0;i<n;i++){cin>>a[i];}k=k%n;s=0,e=n-k-1;while(s<e){t=a[s];a[s]=a[e];a[e]=t;s++;e--;}s=n-k,e=n-1;while(s<e){t=a[s];a[s]=a[e];a[e]=t;s++;e--;}s=0,e=n-1;while(s<e){t=a[s];a[s]=a[e];a[e]=t;s++;e--;}for(i=0;i<q;i++){cin>>b;cout<<a[b]<<endl;}return0;}

#include<iostream>#include<vector>#include<algorithm>#include<iterator>intmain(){intn;std::cin>>n;// array sizeintk;std::cin>>k;//no of rotationsintq;std::cin>>q;std::vector<int>vec;vec.reserve(n);std::copy_n(std::istream_iterator<int>(std::cin),n,back_inserter(vec));k%=n;k=n-k;std::rotate(vec.begin(),vec.begin()+k,vec.end());while(q--){intindex;std::cin>>index;std::cout<<vec[index]<<std::endl;}return0;}

i actually tried running the code in eclipse, and it is matching the expected output, expect in the last case where it doesnot return the result before enter is pressed, as when the return key is pressed the output becomes fully correct.

here is my code----------------------

import java.io.;
import java.util.;

public class Solution {

public static void main(String[] args) {
Scanner s1=new Scanner(System.in);
int n=s1.nextInt();
int k=s1.nextInt();
int q=s1.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=s1.nextInt();
}
for(int i=0;i<q;i++)
{
int m=s1.nextInt();
System.out.println(a[(n-k+m)%n]);
}
}

I am not concernerd with the working of the solution, the issue is why is it not working in that particular case, what is the issue? because i am facing the same issue in another problem which is Bigger is Greater in the implementation section.

I can't see any problem in your code. And your code is working fine, though i haven't run it on eclipse but i have tried it on an online compiler and it gave me the correct output.

that's because in test case 4 the number of rotations is much bigger than the number of elements of the array.
If you were really executing the rotations, it wouldn't cause any error, but would be very slow, causing errors in other cases (time out). If you use any algorythm that just subtacts the number of rotations from the size of the array, in fact you may try to access elements outside of the array. In case 4 we have an array with 515 elements which is rotated 100000 times. When I try to retrieve the 1st element, the algorythm will try to retriev the element with the index -9485(negative), giving an error of segmentation.

I came up with the exact same solution but "Test case #4" fails with a run time error. I downlowded the input and the output for this test case and ran it locally in Visual Studio (C#) and it does not fail.

In test case 4 ..it should be noted that the value of k is greater than the no. of elements in the array(n)
try using mod operator fr this problem
k=k%n;👍

I encountered the same problem. The test case 4 has k bigger than n, which causes segmentation error due to index out of bounds when you do n - k. Solved the problem by setting k = k % n when it is larger than n.

int n;
int k;
int q;
cin >> n >> k >> q;
int a[n];
for (int i=0;i<n;i++) cin >> a[i];
int dest=k%n;
int b[n];
for (int i=0;i<n;i++) {
b[dest++]=a[i];
if (dest==n) dest=0;
}
for (int i=0;i<q;i++) {
int m;
cin >> m;
cout << b[m] << endl;
}

## Circular Array Rotation

You are viewing a single comment's thread. Return to all comments →

Ah. I was doing some weird modular arithmetic, which led to funky outcomes for the unknown test cases.

In your solution, you could use another modular arithmetic, to ensure the index stays within the boundaries of the array.

In my solution the index are within the boundaries.

Thanks a ton Dinesh. Beautiful solution. I initially used brute force without understanding the rotation pattern.

Learnt something new. Your blog is really good

Cheers

can you plz tell me what is wrong with my code???

Hi, dk201966. Probably you are facing problems because the order you are doing the changes. If you copy the value from the 1st position to the 2nd one and, after this, you copy the 2nd to the 3rd, at the end you will have all the positions with the 1st value. And don't forget saving the last value before overwriting it! PS: you'll probably face other issues with this challenge...

Dude please fix your "include"s they seem ugly :)

The main problem in the code is the order of the for loop used for rotating the elenets in the array. You should store the last element value in some temporary variable..and then use a decreasing counter in the mentioned for loop ..then u can store the value in temporary variable to the first element The error in ur code of overwriting values is eleminated by this code!👍

why my code gives wrong answers ...?

you need to return q not a.

you are not supposed to print the whole array a but only the elements at positions in queries

how come!, It's not working???

if you try to interchange the value,t then it does not work,

if(j+1!=n-1)

in your code there is assignment of values....shifting of values is absent..

hi Change your condition as a[j-1]=a[j]; and j=n-1;j>=0;j-- in for loop

your code is wrong due to elae statement

I did the same and realized the values of the elements of the array that you assign later has already replaced the other element in the array. For eg: a[1]=a[0] then a[2]=a[1]=a[0] which is equal to each other

Thank you very much..passed all test cases.

it will work

simpler one:

why are you taking modulus of k???

refer to my code

refer this

same to you. but if you dry run the code ,then can you understand what is wrong with the code !!

it is failing for one case when modulo is used, any help?

I don't know why you guys are having problem. I have submitted my solution without any problem.

Anyway if you are having issues than you should upload you code than only i will know what the problem is.

i actually tried running the code in eclipse, and it is matching the expected output, expect in the last case where it doesnot return the result before enter is pressed, as when the return key is pressed the output becomes fully correct.

here is my code----------------------

import java.io.

; import java.util.;public class Solution {

}

Same problem in another solution.

Try this solution it will work

I am not concernerd with the working of the solution, the issue is why is it not working in that particular case, what is the issue? because i am facing the same issue in another problem which is Bigger is Greater in the implementation section.

I can't see any problem in your code. And your code is working fine, though i haven't run it on eclipse but i have tried it on an online compiler and it gave me the correct output.

yes, thats what the problem is , i m not understanding why is it not accepting the solution.

Anyways, Thanks for the help

Do this. Had same issue and it fixed it.

k=k%n; Looking at the test case found that k is pretty large.

thank you

This one is very good but, for the case when k is much more bigger than n it is not working, because an arrayoutofboundException will occure.

maybe this will help

Add one line k=k%n; before second loop start then you'll pass all the test.

Thank you

i got runtime error by using this

same problem

that's because in test case 4 the number of rotations is much bigger than the number of elements of the array. If you were really executing the rotations, it wouldn't cause any error, but would be very slow, causing errors in other cases (time out). If you use any algorythm that just subtacts the number of rotations from the size of the array, in fact you may try to access elements outside of the array. In case 4 we have an array with 515 elements which is rotated 100000 times. When I try to retrieve the 1st element, the algorythm will try to retriev the element with the index -9485(negative), giving an error of segmentation.

What will happen if the rotations(k) arer greater than n. For example

There are 3 number of elements i.e n=3 it performs 96 rotations i.e k=96 and m=0 ?

I posted my code, if u could look at it in the comment section above that would be great!

## include

using namespace std; int main() { int n,k,q,temp,i,p; cin>>n>>k>>q; int a[n]; for(i=0;i>a[i]; } while(k>0) { temp=a[n-1]; for(i=n-1;i>=0;i--) { a[i]=a[i-1]; } a[0]=temp; k--; for(i=0;i>p; cout<

}

problem : Terminated due to time out??

I came up with the exact same solution but "Test case #4" fails with a run time error. I downlowded the input and the output for this test case and ran it locally in Visual Studio (C#) and it does not fail.

In test case 4 ..it should be noted that the value of k is greater than the no. of elements in the array(n) try using mod operator fr this problem k=k%n;👍

Helpful!!

you figured out the time problem bro!!!! great

Thanks!!

i tried the same thing but the test case #4 is showing runtime error

The strange thing is that I uploaded the same solution in C#, C++ and Java 8 and for all of them "Test case #4" fails with a run time error.

I encountered the same problem. The test case 4 has k bigger than n, which causes segmentation error due to index out of bounds when you do n - k. Solved the problem by setting k = k % n when it is larger than n.

Thanks :)

Thanks!

Thanks!!Thank you a lot. I was stuck in this test case. I thought it was because of a very huge data set that could exceed maximum memory size.

The "Runtime Error" for test case #4 occurs because k can be much bigger than n and in such cases the code tries to access a negative index.

For test case 4: k = 100000, n = 515.

The solution is to apply an extra modulo n on k before substracting:

what if array size is 3 and it is rotated 4 times basically n=3 and k=4 and I want value at index = 0

That is the same as if the array was rotated once. So, at index 0 we'll find the element formerly located at index 1. peace of cake..

Modular arithmetic is the best way to do this problem. No need to actually rotate the array. Take a look here. Circular Array Rotation

Python solution using mod:

Here is another Python Solution:-

n,k,q = input().strip().split(' ') n,k,q = [int(n),int(k),int(q)] a = [int(a_temp) for a_temp in input().strip().split(' ')]

for i in range(q): m = int(input().strip()) print(a[(m-k)%len(a)])

@hasan2020mainul can you explain the operation a(m-k)%len(a) you did?

Another simple python solution

what is the faster?

or we can simply do like this by using python collections

hi @vasilij_kolomie1 your code doesn't work for test case 4

Yes when the number of rotations is too big you need to use use rotations mod number of prisoners