M items are to be delivered in a circle of size N. Find the position where the M-th item will be delivered if we start from a given position K. Note that items are distributed at adjacent positions starting from K.**Examples :**

Input : N = 5 // Size of circle M = 2 // Number of items K = 1 // Starting position Output : 2 The first item will be given to 1st position. Second (or last) item will be delivered to 2nd position Input : N = 5 M = 8 K = 2 Output : 4 The last item will be delivered to 4th position

We check if the number of items to be distributed is greater than our remaining positions in current cycle of circle or not. If yes, then we simply return m + k – 1 (We distribute items in same cycle starting from k). Else we compute number of remaining items after completing current cycle and return mod of remaining items.

Below is the implementation of the above idea

## C++

`// C++ program to find the position where` `// last item is delivered.` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// n ==> Size of circle` `// m ==> Number of items` `// k ==> Initial position` `int` `lastPosition(` `int` `n, ` `int` `m, ` `int` `k)` `{` ` ` `// n - k + 1 is number of positions` ` ` `// before we reach beginning of circle` ` ` `// If m is less than this value, then` ` ` `// we can simply return (m-1)th position` ` ` `if` `(m <= n - k + 1)` ` ` `return` `m + k - 1;` ` ` `// Let us compute remaining items before` ` ` `// we reach beginning.` ` ` `m = m - (n - k + 1);` ` ` `// We compute m % n to skip all complete` ` ` `// rounds. If we reach end, we return n` ` ` `// else we return m % n` ` ` `return` `(m % n == 0) ? n : (m % n);` `}` `// Driver code` `int` `main()` `{` ` ` `int` `n = 5;` ` ` `int` `m = 8;` ` ` `int` `k = 2;` ` ` `cout << lastPosition(n, m, k);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the position where` `// last item is delivered.` `class` `GFG {` ` ` `// n ==> Size of circle` ` ` `// m ==> Number of items` ` ` `// k ==> Initial position` ` ` `static` `int` `lastPosition(` `int` `n, ` `int` `m, ` `int` `k)` ` ` `{` ` ` `// n - k + 1 is number of positions` ` ` `// before we reach beginning of circle` ` ` `// If m is less than this value, then` ` ` `// we can simply return (m-1)th position` ` ` `if` `(m <= n - k + ` `1` `)` ` ` `return` `m + k - ` `1` `;` ` ` `// Let us compute remaining items before` ` ` `// we reach beginning.` ` ` `m = m - (n - k + ` `1` `);` ` ` `// We compute m % n to skip all complete` ` ` `// rounds. If we reach end, we return n` ` ` `// else we return m % n` ` ` `return` `(m % n == ` `0` `) ? n : (m % n);` ` ` `}` ` ` `// Driver Program to test above function` ` ` `public` `static` `void` `main(String arg[])` ` ` `{` ` ` `int` `n = ` `5` `;` ` ` `int` `m = ` `8` `;` ` ` `int` `k = ` `2` `;` ` ` `System.out.print(lastPosition(n, m, k));` ` ` `}` `}` `// This code is contributed by Anant Agarwal.` |

*chevron_right*

*filter_none*

## Python

`# Python program to find the position where` `# last item is delivered.` `# n ==> Size of circle` `# m ==> Number of items` `# k ==> Initial position` `def` `lastPosition(n, m, k):` ` ` ` ` `# n - k + 1 is number of positions` ` ` `# before we reach beginning of circle` ` ` `# If m is less than this value, then` ` ` `# we can simply return (m-1)th position` ` ` `if` `(m <` `=` `n ` `-` `k ` `+` `1` `):` ` ` `return` `m ` `+` `k ` `-` `1` ` ` ` ` `# Let us compute remaining items before` ` ` `# we reach beginning.` ` ` `m ` `=` `m ` `-` `(n ` `-` `k ` `+` `1` `)` ` ` ` ` `# We compute m % n to skip all complete` ` ` `# rounds. If we reach end, we return n` ` ` `# else we return m % n` ` ` `if` `(m ` `%` `n ` `=` `=` `0` `):` ` ` `return` `n` ` ` `else` `:` ` ` `return` `m ` `%` `n` ` ` `# Driver code` `n ` `=` `5` `m ` `=` `8` `k ` `=` `2` `print` `lastPosition(n, m, k)` `# This code is contributed by Sachin Bisht` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the position where` `// last item is delivered.` `using` `System;` `class` `GFG {` ` ` `// n ==> Size of circle` ` ` `// m ==> Number of items` ` ` `// k ==> Initial position` ` ` `static` `int` `lastPosition(` `int` `n, ` `int` `m, ` `int` `k)` ` ` `{` ` ` `// n - k + 1 is number of positions` ` ` `// before we reach beginning of circle` ` ` `// If m is less than this value, then` ` ` `// we can simply return (m-1)th position` ` ` `if` `(m <= n - k + 1)` ` ` `return` `m + k - 1;` ` ` `// Let us compute remaining items before` ` ` `// we reach beginning.` ` ` `m = m - (n - k + 1);` ` ` `// We compute m % n to skip all complete` ` ` `// rounds. If we reach end, we return n` ` ` `// else we return m % n` ` ` `return` `(m % n == 0) ? n : (m % n);` ` ` `}` ` ` `// Driver Program to test above function` ` ` `public` `static` `void` `Main()` ` ` `{` ` ` `int` `n = 5;` ` ` `int` `m = 8;` ` ` `int` `k = 2;` ` ` `Console.WriteLine(lastPosition(n, m, k));` ` ` `}` `}` `// This code is contributed by vt_m.` |

*chevron_right*

*filter_none*

## PHP

`<?php` `// PHP program to find the ` `// position where last item` `// is delivered.` `// n ==> Size of circle` `// m ==> Number of items` `// k ==> Initial position` `function` `lastPosition(` `$n` `, ` `$m` `, ` `$k` `)` `{` ` ` `// n - k + 1 is number of ` ` ` `// positions before we reach ` ` ` `// beginning of circle. ` ` ` `// If m is less than this value, ` ` ` `// then we can simply return` ` ` `// (m-1)th position` ` ` `if` `(` `$m` `<= ` `$n` `- ` `$k` `+ 1)` ` ` `return` `$m` `+ ` `$k` `- 1;` ` ` `// Let us compute remaining items ` ` ` `// before we reach beginning.` ` ` `$m` `= ` `$m` `- (` `$n` `- ` `$k` `+ 1);` ` ` `// We compute m % n to skip ` ` ` `// all complete rounds. If we ` ` ` `// reach end, we return n` ` ` `// else we return m % n` ` ` `return` `(` `$m` `% ` `$n` `== 0) ? ` `$n` `: (` `$m` `% ` `$n` `);` `}` `// Driver code` `$n` `= 5;` `$m` `= 8;` `$k` `= 2;` `echo` `lastPosition(` `$n` `, ` `$m` `, ` `$k` `);` `// This code is contributed by ajit` `?>` |

*chevron_right*

*filter_none*

**Output :**

4

**Time Complexity **: O(1)

This article is contributed by **Sarthak Kohli**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Subsequences of size three in an array whose sum is divisible by m
- Generate Array whose sum of all K-size subarrays divided by N leaves remainder X
- Check if quantities of 3 distinct colors can be converted to a single color by given merge-pair operations
- Check if a destination is reachable from source with two movements allowed | Set 2
- Find the largest possible value of K such that K modulo X is Y
- Maximize modulus by replacing adjacent pairs with their modulus for any permutation of given Array
- Possible number of Trees having N vertex
- Count of distinct numbers formed by shuffling the digits of a large number N
- Find the first and last M digits from K-th power of N
- Minimize increment/decrement of Array elements to make each modulo K equal
- Count of Distinct strings possible by inserting K characters in the original string
- Count of pairs of Array elements which are divisible by K when concatenated
- Check if N leaves only distinct remainders on division by all values up to K
- Find the value of P and modular inverse of Q modulo 998244353