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 an integer **K **and a queue of integers, we need to reverse the order of the first** K** elements of the queue, leaving the other elements in the same relative order.

Only following standard operations are allowed on queue.

- enqueue(x) : Add an item x to rear of queue
- dequeue() : Remove an item from front of queue
- size() : Returns number of elements in queue.
- front() : Finds front item.

**Example 1:**

Input:5 3 1 2 3 4 5Output:3 2 1 4 5Explanation:After reversing the given input from the 3rd position the resultant output will be 3 2 1 4 5.

**Example 2:**

Input:4 4 4 3 2 1Output:1 2 3 4Explanation:After reversing the given input from the 4th position the resultant output will be 1 2 3 4.

**Your Task:**

**Complete **the **provided function** **modifyQueue **that takes **queue and k** as **parameters **and **returns **a **modified **queue. The **printing **is done **automatically **by the **driver code**.

**Expected TIme Complexity** : O(n)

**Expected Auxilliary Space **: O(n)

**Constraints:**

1 <= N <= 1000

1 <= K <= N

**Note:**The **Input/Ouput** format and **Example** given are used for system's internal purpose, and should be used by a user for **Expected Output** only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.

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
Reverse First K elements of Queue

...