Posts Distribute in Circle! (InterviewBit)
Post
Cancel

Distribute in Circle! (InterviewBit)

PROBLEM DESCRIPTION

A items are to be delivered in a circle of size B.

Find the position where the Ath item will be delivered if we start from a given position C.

NOTE: Items are distributed at adjacent positions starting from C.

InterviewBit

SOLUTION

1
2
3
4
5
6
7
8
9
10
public class Solution {
    public int solve(int A, int B, int C) {

        C = (C-1)%B; // conver to 0 index
        A = A%B; // after every B items, it will reach same position, so A%B will give extra items left

        return (C + A)%B; // from C, deliver more A items to reach the needed position (if it goes beyond B, it will cycle again)

    }
}

Alternative: return (A+C-1)%B;

This post is licensed under CC BY 4.0 by the author.