Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5761 |

@blackshadows | 5701 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4944 |

@saiujwal13083 | 4506 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3516 |

@sushant_a | 3459 |

@verma_ji | 3341 |

@KshamaGupta | 3318 |

Complete Leaderboard | |

Handle | Score |

@aroranayan999 | 938 |

@bt8816103042 | 739 |

@SHOAIBVIJAPURE | 428 |

@codeantik | 412 |

@SherlockHolmes3 | 407 |

@neverevergiveup | 348 |

@mahlawatep | 347 |

@shalinibhataniya1097 | 343 |

@murarry3625 | 333 |

@saiujwal13083 | 326 |

@rohitanand | 314 |

Complete Leaderboard |

Given a sorted deck of cards numbered 1 to N.

1) We pick up 1 card and put it on the back of the deck.

2) Now, we pick up another card, it turns out to be card number 1, we put it outside the deck.

3) Now we pick up 2 cards and put it on the back of the deck.

4) Now, we pick up another card and it turns out to be card numbered 2, we put it outside the deck. ...

We perform this step until the last card.

If such an arrangement of decks is possible, output the arrangement, if it is not possible for a particular value of N then output -1.

**Example 1:**

**Input**:
N = 4
**Output:** 2 1 4 3
**Explanation**:
We initially have [2 1 4 3]
In Step1, we move the first card to the end.
Deck now is: [1 4 3 2]
In Step2, we get 1. Hence we remove it. Deck
now is: [4 3 2]
In Step3, we move the 2 front cards ony by one
to the end ([4 3 2] -> [3 2 4] -> [2 4 3]).
Deck now is: [2 4 3]
In Step4, we get 2. Hence we remove it. Deck
now is: [4 3]
In Step5, the following sequence follows:
[4 3] -> [3 4] -> [4 3] -> [3 4]. Deck now
is: [3 4]
In Step6, we get 3. Hence we remove it. Deck
now is: [4] Finally, we're left with a single
card and thus, we stop.

**Example 2:**

**Input:**
N = 5
**Output: **3 1 4 5 2

**Your Task: **

You don't need to read input or print anything. Your task is to complete the function **rotation()** which takes the integer N as input parameters and returns If such arrangement of decks is possible, return the arrangement, if it is not possible for a particular value of n then return -1.

**Expected Time Complexity:** O(N^2)

**Expected Auxiliary Space:** O(1)

**Constraints:**

1 ≤ N ≤ 1000

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Card Rotation

...