Geeksforgeeks

Error

×

Leaderboard

Showing:

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

@Ibrahim Nash | 5725 |

@blackshadows | 5685 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4908 |

@saiujwal13083 | 4156 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3511 |

@sushant_a | 3453 |

@KshamaGupta | 3318 |

@the_coder95 | 3302 |

Complete Leaderboard | |

Handle | Score |

@shashi1iitk | 398 |

@Suniti Jain | 114 |

@ankish2000nayak | 104 |

@gopalsethlnct | 94 |

@esmadhan | 90 |

@senthil1185 | 90 |

@gyanadipta07 | 89 |

@satwikdash2000 | 85 |

@varunmishra3040 | 83 |

@gamec1634 | 82 |

@pj_1721 | 80 |

Complete Leaderboard |

Given the total number of persons **n** and a number **k** which indicates that** k-1 **persons are skipped and **k ^{th}** person is killed in circle in a fixed direction.

The task is to choose the **safe place in the circle** so that when you perform these operations starting from **1**^{st}** ^{ }place** in the circle, you are the last one remaining and survive.

**Example 1:**

**Input:
**n = 3 k = 2
**Output: **3**
Explanation: **There are 3 persons so
skipping 1 person i.e 1st person 2nd
person will be killed. Thus the safe
position is 3.

**Example 2:**

**Input:
**n = 5 k = 3
**Output: **4**
Explanation: **There are 5 persons so
skipping 2 person i.e 3rd person will
be killed. Thus the safe position is 4.

**Your Task:**

You don't need to read input or print anything. You are required to complete the **function josephus ()** that takes** two parameters n and k** and **returns **an integer denoting** safe position**.

**Expected Time Complexity: **O(N).

**Expected Auxiliary Space: **O(N).

**Constraints:**

1 <= k, n <= 20

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
Josephus problem

...