Arrays: Left Rotation

  • + 3 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"

    • + 2 comments

      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... :)

      • + 5 comments

        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:

        StringBuilder output = new StringBuilder();
        	
        	for(int i = 0; i<n; i++) {
        		
        		b[i] = a[(i+k) % n];
        		output = output.append(b[i]).append(" ");
        		
        	}
        
        • + 1 comment

          Thnx

          • + 1 comment

            okay

            • + 1 comment

              okay

              • + 1 comment

                okay

                • + 1 comment

                  okay

                  • + 1 comment

                    okay

                    • + 0 comments

                      okay

        • + 0 comments

          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())

        • + 0 comments

          why it is not working if we are using same array to store modified array i.e. a[i]=a[i+k)%n]

      • + 0 comments

        include

        void reverse(int *str,int length) { int start,end; for(start=0,end=length-1;start

                }
        

        } int main(){

          int size,nor;
           scanf("%d %d",&size,&nor);
         int *str=(int *)malloc(size*sizeof(int));
         for(int i=0;i<size;scanf("%d",&str[i++]));
         reverse(str,size);
         reverse(str,size-nor);
         reverse(str+size-nor,nor);
         for(int i=0;i<size;printf("%d ",str[i++]));
         return 0;
        

        }

    • + 4 comments
      [deleted]
      • + 2 comments

        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.

        • + 1 comment
          [deleted]
          • + 1 comment

            O(n^2) means you have 2 for loops causing a greater time complexity

            • + 0 comments

              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.

      • + 0 comments

        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.

      • + 0 comments

        My implementation of this in java didn't have this error.

      • + 0 comments

        use only int

    • + 0 comments

      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.