Get item after the array is used up 'n' times

Get item after array used up n times or by n things.
Sometimes we may encounter a question like, if there are 5 colors and every color is picked up by a student in order and if 72 students pick colors in order one after the other, then what is the color picked by 72th student. For such questions there are answers. In this post, i have written an easy logic that could easily be understood which will outsmart this problem.

import java.util.*;
class GetItem
    public static void main(String args[])
    Scanner s=new Scanner(;
    System.out.println("Enter the no.of colors");
    int n=s.nextInt();

    System.out.println("Enter the array");
    String[] st=new String[n];
        for(int i=0;i<st.length;i++)

    System.out.println("Enter the student number");
    int x=s.nextInt();

    System.out.println("Student number "+x+" will get


Sample Output

Enter the no.of colors
Enter the array
Enter the student number
Student number 72 will get green


There is in fact only single line of logic to explain. You might however be familiar with java.util.Scanner class and reading values using it. Now, when it comes to the logic, it is very simple. Just divide the student number with the length of the array, the resultant remainder is the position (not index) of the color. As we know that index=position-1 where position of an element in an array represents where it appears. It is just index+1. For instance, red has a position of 1 and index of 0. So first student will pick up red, second student green and so on. Now nth student will pick up n%x positioned element or n%x-1 indexed element.
Carry out the division of 72/5. You will get 2 as remainder and the position of the element is 2 i.e. it's index is 2-1=1.

In this way we can get item after the array after it is reused n times. Feel free to drop a comment.

Posted by at on

Tags: Playing with Numbers,