Why am i getting run error
Hello @Anchal,
You have missed a very important part of the question:
K can be greater than N.
The following part of your code is causing runtime error for k>=n
while(jump<x)
{
fast=fast->next;
jump++;
}
Example 1:
7
1 2 2 1 8 5 6
7
Expected Output:
1 2 2 1 8 5 6
Example 2:
7
1 2 2 1 8 5 6
8
Expected Output:
6 1 2 2 1 8 5
Hope, this would help.
Give a like, if you are satisfied.
The program you have send is working properly for values x<=n.
But, when i take a value greater than n, then it prints the entire sequence as it is.
Example:
7
1 2 2 1 8 5 6
8
Expected Output:
6 1 2 2 1 8 5
Your Output:
1 2 2 1 8 5 6
Solution:
If you observe the pattern:
for x=7, the output is same as x=0,
for x=8, the output is same as that of x=1,
…and so on.
Thus, divide the value x with n and then pass remainder in shift(head,x) as x.
I have modified your code.
Click here to check.
Hope, this would help.
Give a like, if you are satisfied.