Arrays: Left Rotation

  • + 0 comments

    You're not wrong but this solution is inefficient. You're solving it in O(((n-1) * k) + 2n). The solution below is in O(2n).

    private static void solution(int size, int shift, int[] arr) {
    
    		int count = 0;
    
    		for (int i = shift; i < size; i++) {
    			System.out.print(arr[i]);
    			System.out.print(" ");
    			count++;
    		}
    
    		count = 0;
    
    		for (int i = size - shift; i < size; i++) {
    			System.out.print(arr[count]);
    			if (i != size - 1)
    				System.out.print(" ");
    			count++;
    		}
    	}