We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

your time complexity : O(n + n)
your space complexity : n

you can optimize it a little bit with :

time complexity : n + d
space complexity : d

public static void main(String[] args) {
Scanner c = new Scanner(System.in);
int n = c.nextInt();
int d = c.nextInt();
int skipPart[] = new int[d];
int j =0;
for(int i = 0; i < n; i++) {
if(i >= d) {
System.out.print(c.nextInt() + " ");
} else {
skipPart[j] = c.nextInt();
j++;
}
}
for(int i = 0; i < skipPart.length; i++) {
System.out.print(skipPart[i] + " ");
}
}

Got it. I'm guessing being judged in a challenge your way of thinking is better -- do the challenge int he fewest steps possible. I really do not think of optimzing the stdin/stdout as they are just mechanism for giving you the data needed and demonstrating the algorithm is correct. I'm guessing if company is viewing my code, they will think with your mindset.

There are a lot of people solving the problem this way. Reread the problem statement and you'll see that this doesn't do what was asked. There only prints out the input in the order expected after a left rotation d times. The real problem is left rotating an existing array and then print the final content of the array. So what you need to do is read in your input and then store it into an unsorted, unrotated, etc array. Left shift that array, and then print it out from array[0] to array [n].

That's what I was thinking, too. The easiest solution to the expected output isn't necessarily solving the stated problem. But, that's also the nature of the problems. Perhaps a better problem would be one that simply passed in an array and you had to modify said array in place. That would at least ensure the right problem is being solved.

## Left Rotation

You are viewing a single comment's thread. Return to all comments →

your time complexity : O(n + n) your space complexity : n

you can optimize it a little bit with :

time complexity : n + d space complexity : d

public static void main(String[] args) { Scanner c = new Scanner(System.in); int n = c.nextInt(); int d = c.nextInt(); int skipPart[] = new int[d]; int j =0; for(int i = 0; i < n; i++) { if(i >= d) { System.out.print(c.nextInt() + " "); } else { skipPart[j] = c.nextInt(); j++; } } for(int i = 0; i < skipPart.length; i++) { System.out.print(skipPart[i] + " "); } }

Got it. I'm guessing being judged in a challenge your way of thinking is better -- do the challenge int he fewest steps possible. I really do not think of optimzing the stdin/stdout as they are just mechanism for giving you the data needed and demonstrating the algorithm is correct. I'm guessing if company is viewing my code, they will think with your mindset.

There are a lot of people solving the problem this way. Reread the problem statement and you'll see that this doesn't do what was asked. There only prints out the input in the order expected after a left rotation d times. The real problem is left rotating an existing array and then print the final content of the array. So what you need to do is read in your input and then store it into an unsorted, unrotated, etc array. Left shift that array, and then print it out from array[0] to array [n].

thanks. Got it.

That's what I was thinking, too. The easiest solution to the

expected outputisn't necessarily solving the stated problem. But, that's also the nature of the problems. Perhaps a better problem would be one that simply passed in an array and you had to modify said array in place. That would at least ensure the right problem is being solved.