Https://hack.codingblocks.com/practice/p/402/316 <-this is question

https://ide.codingblocks.com/s/103238 pls tell answer is correct but giving runtime error

this is linked list k append pls tell?

Hi @ARYAN27, t is given in the question that k can be greater than n. Your code works well for k<n. But for k>= n, it is giving runtime error. So pls change your code according to that.

for that case i have made a node insert at end ??pls tell how to proceed

ok lets say we have a list of size n=7 and k = 3

original list is: 1 2 3 4 5 6 7

after processing it the list will be :

5 6 7 1 2 3 4

what happens if k = 7
in this case we have to put all the last k=7 elements in front. Notice that this will result into the original list itself.
i.e. for all k=n and also k=0 output is same as input.

now what happens if k = 9.
we simply put as many no. of elements in the front of list which are possible
which in this case is 7 because list has maximum 7 elements.

Let me name this shifting of all n elements to obtain the list to be same as original as task 1.

now after we have shifted seven elements, i.e. done with task 1; we are left with 9 - 7 = 2 elements to be shifted. i.e. we now need to shift 2 more elements.

so the list becomes:

6 7 1 2 3 4 5

also lets take an example where k = 17.

now lets do task1 here i.e. shift maximum possible elements i.e. all 7 elements to front
we are now left with 17 - 7 = 10
again we shift maximum no. of elements, i.e. we perform task 1 and now we are left with 10-7 = 3
elements.
now we shift he last 3 elements to front so the final output will be:

5 6 7 1 2 3 4

this is what happens if k is larger than n.

Now what you need to figure out is how to skip task 1 for all cases where k >= n. This is because shifting elements to reach a configuration which you already are at affects time complexity of the program. you just need to find the no. of element that will be left after maximum possible no. of task1s performed.

Hint: you have to use an operator.

Hope this helps :slight_smile:

1 Like

it is very helpful ,
using k=k%n;
is i have to use this after inputing k;
and is i have to make a case for k>n in the above program ?
pls tell this??

Hi @ARYAN27, when you use k=k%n, that handles the case for k>= n itself hence you do not need to do anything extra. In total, you are all correct now.

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.