Thursday, 27 October 2016

Left Rotation Hacker Rank Problem Solution.


Solution:

#include <bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {
    int n,d,ar[100002];
    cin >> n >> d;
    for(int i = 0; i < n; i++)cin >> ar[(n-d+i)%n];
    for(int i = 0; i < n; i++)cout << ar[i] << " ";
    cout << endl;
    return 0;
}

1 comment:

  1. My solution-


    #include
    #include

    void leftRotation(int n, int* a, int d) {
    int temp,i;
    while(d>0){
    temp=a[0];
    for (i = 0; i < n-1; i++) {
    a[i]= a[i+1];
    }
    a[n-1]=temp;
    d--;
    }
    }

    int main() {
    int n,d,i;
    scanf("%d %d", &n, &d);
    int *a = (int*)malloc(sizeof(int) * n);
    for (i = 0; i < n; i++) {
    scanf("%d",&a[i]);
    }
    leftRotation(n,a, d);
    for ( i = 0; i < n; i++) {
    printf("%d ",a[i]);
    }
    return 0;
    }


    I implemented this logic. I don't understand why test case 8 fails for this answer. Can anyone help me?

    ReplyDelete