• + 3 comments

    int main() {

     int n,d,f,j,i,*ar;
    cin>>n>>d;
    ar=new int[n];
    for(i=0;i<n;i++){
        cin>>ar[i];
    }
    for(i=1;i<=d;i++){
        f=ar[0];
        for(j=0;j<n-1;j++){
            ar[j]=ar[j+1];
        }
        ar[n-1]=f;
    }
    for(i=0;i<n;i++){
        cout<<ar[i];
        if(i<(n-1)){
            cout<<" ";
        }
    }
    return 0;
    

    } test case#8 is showing "terminated due to timeout"! Any suggestions? i am new to this!

    • + 2 comments

      I have the exactly same problem. Did anyone figure out why?

      • + 0 comments

        I don't particularly know what test case 8 is exactly, but I would assume that this test case has set the input parameters of n and d to be very large. If you look at the constraints on n, then you will see that n can be as large as 10,000 and d also can be as large as n. With an approach that manually shifts the array one by one a certain amount of times can lead to a lot of work. Possibly shifting all 10,000 elements to the left 10,000 times racks up to a lot of work. You would have to shift something up to 10,000,000,000 times in this scenario. That is a lot of work and would most likely be the reason the test case times out before finishing.

      • + 4 comments

        I did solve it! It was proably beacuse of the nested loops! they take more time to run!

        • + 0 comments

          which is known as complexity

        • + 1 comment

          how did you solve it ?

          • + 2 comments

            Its easy man i cant figure out the last test case though !!

            1.Input an array of size n 2.Input number of rotations as r 3. r=r%n -> it reduces the number of repeatative rotations thus decreasing the complexity of our code . suppose the size of the array is 3 and number of rotations is also 3 then our loop wont run it will simply print the same elements .

            1. take a temperory variable and store the leftmost element in it i.e a[o]
            2. Now run the loop from 0 to second last element of the array 6.a[i]=a[i+1] -> shifts the element towards left for eg. for i=0 a[0]=a[1]; it shifts all the elements towards left 7.now simply store the first element in temp variable to the last position.
            3. steps 4 to 7 for first rotation now while loop will run as per the given number of rotations these steps will be repeated until r becomes 0.
            4. At last display the elements in the array.

            :)

            • + 1 comment

              Man i have better version of solution. See my code.

              vector array_left_rotation(vector a, int n, int k) { rotate(a.begin(),a.begin()+k,a.end()); return a; }

              int main(){ int n; int k; cin >> n >> k; vector a(n); for(int a_i = 0;a_i < n;a_i++){ cin >> a[a_i]; } vector output = array_left_rotation(a, n, k); for(int i = 0; i < n;i++) cout << output[i] << " "; cout << endl; return 0; }

              • + 0 comments

                chup.. space dedeta kuch to

            • + 0 comments

              Doesn't work for 8th test case

        • + 1 comment

          how did u solve??

          • + 1 comment

            Keep in mind a left rotation of x is equal to (len-x) of righ rotations. Hence, it would be more efficient to the rotation with least amount of "moves".

            • + 1 comment

              Thats true. I got accepted for test case 8 using right rotation. We need to choose :

              If #rotations < (size /2) Use Left Rotation If #rotations > (size /2) Use Right Rotation

              • + 0 comments

                can you elaborate @crystalcode

    • + 1 comment

      just type,
      for i=0 to n-1
      print a [ ( i + d ) %n ].

      thats all..

      • + 1 comment
        [deleted]
        • + 1 comment

          god

    • + 2 comments

      :( I'm also stuck at test case 8. The message throws "Run time error" Don't know if there's any alternatives rather the traditional for / pop push method ? My current code (Pass all test cases except test case 8)

      function rotLeft(a, d) {
          let newArray = [...a];
          let iterationCount = 0;
          while(iterationCount < d){
              let valueToPop = newArray[0];
              newArray.shift();
              newArray.push(valueToPop);
              iterationCount++;
           }
          return newArray;
      }
      					   
      
      • + 0 comments

        same code works for me for all test cases.