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.
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
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 firstname.lastname@example.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.
- Probability of distributing M items among X bags such that first bag contains N items
- Check if it is possible to return to the starting position after moving in the given directions
- Position of a person diametrically opposite on a circle
- Number of ways in which an item returns back to its initial position in N swaps in array of size K
- Program to find the profit or loss when CP of N items is equal to SP of M items
- Check if a circle lies inside another circle or not
- Area of the circle that has a square and a circle inscribed in it
- Number of ways of distributing N identical objects in R distinct groups
- Equation of circle when three points on the circle are given
- Number of ways of distributing N identical objects in R distinct groups with no groups empty
- Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
- Angle subtended by the chord to center of the circle when the angle subtended by the another equal chord of a congruent circle is given
- Check if two people starting from different points ever meet
- Minimum number of moves to reach N starting from (1, 1)
- Number of ways to reach (X, Y) in a matrix starting from the origin
- Final direction after visiting every cell of Matrix starting from (0, 0)
- Smallest and Largest N-digit number starting and ending with N
- Number of ways to reach (M, N) in a matrix starting from the origin without visiting (X, Y)
- Minimum steps to come back to starting point in a circular tour
- Maximum items that can be bought with the given type of coins