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.
I was facing the same problem.I gave several attempts but the issue couldn't be solved. Can you please tell me how to define a loop for a set of array with so many elements as such... :)
an inner loop will not cause his program to time out. I don't believe the variable n was ever initialized, so the loop is approaching a value of n that isn't defined.
I was facing the same issue in PHP. My solution worked for 9 out of 10 test cases but timed out on one of them every time. You have to re-write the solution to be less memory intensive. In my case I was using array_shift() which re-indexes the arrays, so for large arrays it uses too much memory. My solution was to use array_reverse() and then array_pop() instead, because those methods don't re-index.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Arrays: Left Rotation
You are viewing a single comment's thread. Return to all comments →
Seems like this algorith only works for small number because when the array is big enough due to long looping period u will have system "timeout"
I was facing the same problem.I gave several attempts but the issue couldn't be solved. Can you please tell me how to define a loop for a set of array with so many elements as such... :)
In java8 the problem was in String; You have to use more efficient StringBuilder instead; And of couse use only one loop to iterate over array;
here is my code snippet:
Thnx
okay
okay
okay
okay
okay
okay
Better to use linked list, so no need to LOOP fully:
val z = LinkedList(a.toList()) for(i in 0 until n) z.addLast(z.pollFirst())
why it is not working if we are using same array to store modified array i.e. a[i]=a[i+k)%n]
include
void reverse(int *str,int length) { int start,end; for(start=0,end=length-1;start
} int main(){
}
its because your solution is O(n^2) with the inner loop. Try and find an O(xn) solution and iterate over the whole array only once.
O(n^2) means you have 2 for loops causing a greater time complexity
an inner loop will not cause his program to time out. I don't believe the variable n was ever initialized, so the loop is approaching a value of n that isn't defined.
static int[] rotLeft(int[] a, int d) { int j,i,p; for(j=0;j
Check with this you will get what is the mistake ypu did.
My implementation of this in java didn't have this error.
use only int
I was facing the same issue in PHP. My solution worked for 9 out of 10 test cases but timed out on one of them every time. You have to re-write the solution to be less memory intensive. In my case I was using array_shift() which re-indexes the arrays, so for large arrays it uses too much memory. My solution was to use array_reverse() and then array_pop() instead, because those methods don't re-index.