Arrays: Left Rotation

  • + 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;
      

      }