Skip to main content
edited tags; added 1 character in body
Source Link
mdfst13
  • 22.4k
  • 6
  • 34
  • 70

I tried to solve this Left Rotation problem on HackerRank. My code works out for most test cases, but for some it doesn't, probably because they have large umbernumber of data. So there seems to be some optimization or performance issue in my code, and I can't really figure out how to fix it. Any help will be greatly appreciated :)

public class Solution {

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        String[] nd = scanner.nextLine().split(" ");
        int c;
        int n = Integer.parseInt(nd[0]);

        int d = Integer.parseInt(nd[1]);

        int[] a = new int[n];

        int arr[] = new int[n];

        String[] aItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int i = 0; i < n; i++) {
            int aItem = Integer.parseInt(aItems[i]);
            a[i] = aItem;
        }

        scanner.close();

        for (int k=1;k<=d;k++) {
            for (int j=0;j<n;j++) {
                if (j==n-1) 
                { c=0;}
                else 
                { c=j+1;}
                arr[j]=a[c];
            }
            System.arraycopy(arr, 0, a, 0, n);
        }

        for (int j=0;j<n;j++) {
            System.out.print(arr[j]+ " ");
        }
    }
}

I tried to solve this Left Rotation problem on HackerRank. My code works out for most test cases, but for some it doesn't, probably because they have large umber of data. So there seems to be some optimization or performance issue in my code, and I can't really figure out how to fix it. Any help will be greatly appreciated :)

public class Solution {

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        String[] nd = scanner.nextLine().split(" ");
        int c;
        int n = Integer.parseInt(nd[0]);

        int d = Integer.parseInt(nd[1]);

        int[] a = new int[n];

        int arr[] = new int[n];

        String[] aItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int i = 0; i < n; i++) {
            int aItem = Integer.parseInt(aItems[i]);
            a[i] = aItem;
        }

        scanner.close();

        for (int k=1;k<=d;k++) {
            for (int j=0;j<n;j++) {
                if (j==n-1) 
                { c=0;}
                else 
                { c=j+1;}
                arr[j]=a[c];
            }
            System.arraycopy(arr, 0, a, 0, n);
        }

        for (int j=0;j<n;j++) {
            System.out.print(arr[j]+ " ");
        }
    }
}

I tried to solve this Left Rotation problem on HackerRank. My code works out for most test cases, but for some it doesn't, probably because they have large number of data. So there seems to be some optimization or performance issue in my code, and I can't really figure out how to fix it. Any help will be greatly appreciated :)

public class Solution {

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        String[] nd = scanner.nextLine().split(" ");
        int c;
        int n = Integer.parseInt(nd[0]);

        int d = Integer.parseInt(nd[1]);

        int[] a = new int[n];

        int arr[] = new int[n];

        String[] aItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int i = 0; i < n; i++) {
            int aItem = Integer.parseInt(aItems[i]);
            a[i] = aItem;
        }

        scanner.close();

        for (int k=1;k<=d;k++) {
            for (int j=0;j<n;j++) {
                if (j==n-1) 
                { c=0;}
                else 
                { c=j+1;}
                arr[j]=a[c];
            }
            System.arraycopy(arr, 0, a, 0, n);
        }

        for (int j=0;j<n;j++) {
            System.out.print(arr[j]+ " ");
        }
    }
}
added 164 characters in body
Source Link
JaDogg
  • 4.6k
  • 3
  • 29
  • 66

I tried to solve this Left Rotation problem on HackerRank. My code works out for most test cases, but for some it doesn't, probably because they have large umber of data. So there seems to be some optimization or performance issue in my code, and I can't really figure out how to fix it. Any help will be greatly appreciated :)

public class Solution {

public class Solution {

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        String[] nd = scanner.nextLine().split(" ");
        int c;
        int n = Integer.parseInt(nd[0]);

        int d = Integer.parseInt(nd[1]);

        int[] a = new int[n];

        int arr[] = new int[n];

        String[] aItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int i = 0; i < n; i++) {
            int aItem = Integer.parseInt(aItems[i]);
            a[i] = aItem;
        }

        scanner.close();

        for (int k=1;k<=d;k++) {
            for (int j=0;j<n;j++) {
                if (j==n-1) 
                { c=0;}
                else 
                { c=j+1;}
                arr[j]=a[c];
            }
            System.arraycopy(arr, 0, a, 0, n);
        }

        for (int j=0;j<n;j++) {
            System.out.print(arr[j]+ " ");
        }
    }
}

}

I tried to solve this Left Rotation problem on HackerRank. My code works out for most test cases, but for some it doesn't, probably because they have large umber of data. So there seems to be some optimization or performance issue in my code, and I can't really figure out how to fix it. Any help will be greatly appreciated :)

public class Solution {

private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
    String[] nd = scanner.nextLine().split(" ");
    int c;
    int n = Integer.parseInt(nd[0]);

    int d = Integer.parseInt(nd[1]);

    int[] a = new int[n];

    int arr[] = new int[n];

    String[] aItems = scanner.nextLine().split(" ");
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

    for (int i = 0; i < n; i++) {
        int aItem = Integer.parseInt(aItems[i]);
        a[i] = aItem;
    }

    scanner.close();

    for (int k=1;k<=d;k++) {
        for (int j=0;j<n;j++) {
            if (j==n-1) 
            { c=0;}
            else 
            { c=j+1;}
            arr[j]=a[c];
        }
        System.arraycopy(arr, 0, a, 0, n);
    }

    for (int j=0;j<n;j++) {
        System.out.print(arr[j]+ " ");
    }
}

}

I tried to solve this Left Rotation problem on HackerRank. My code works out for most test cases, but for some it doesn't, probably because they have large umber of data. So there seems to be some optimization or performance issue in my code, and I can't really figure out how to fix it. Any help will be greatly appreciated :)

public class Solution {

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        String[] nd = scanner.nextLine().split(" ");
        int c;
        int n = Integer.parseInt(nd[0]);

        int d = Integer.parseInt(nd[1]);

        int[] a = new int[n];

        int arr[] = new int[n];

        String[] aItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int i = 0; i < n; i++) {
            int aItem = Integer.parseInt(aItems[i]);
            a[i] = aItem;
        }

        scanner.close();

        for (int k=1;k<=d;k++) {
            for (int j=0;j<n;j++) {
                if (j==n-1) 
                { c=0;}
                else 
                { c=j+1;}
                arr[j]=a[c];
            }
            System.arraycopy(arr, 0, a, 0, n);
        }

        for (int j=0;j<n;j++) {
            System.out.print(arr[j]+ " ");
        }
    }
}
Source Link

HackerRank Left rotation code using Java 8

I tried to solve this Left Rotation problem on HackerRank. My code works out for most test cases, but for some it doesn't, probably because they have large umber of data. So there seems to be some optimization or performance issue in my code, and I can't really figure out how to fix it. Any help will be greatly appreciated :)

public class Solution {

private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
    String[] nd = scanner.nextLine().split(" ");
    int c;
    int n = Integer.parseInt(nd[0]);

    int d = Integer.parseInt(nd[1]);

    int[] a = new int[n];

    int arr[] = new int[n];

    String[] aItems = scanner.nextLine().split(" ");
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

    for (int i = 0; i < n; i++) {
        int aItem = Integer.parseInt(aItems[i]);
        a[i] = aItem;
    }

    scanner.close();

    for (int k=1;k<=d;k++) {
        for (int j=0;j<n;j++) {
            if (j==n-1) 
            { c=0;}
            else 
            { c=j+1;}
            arr[j]=a[c];
        }
        System.arraycopy(arr, 0, a, 0, n);
    }

    for (int j=0;j<n;j++) {
        System.out.print(arr[j]+ " ");
    }
}

}