236. Business Tasks

N tasks are written down in the form of a circular list, so the first task is adjacent to the last one. A number n is also given. Starting with the first task, move clockwise (from element 1 in the list to element 2 in the list and so on), counting from 1 to n. When your count reaches n, remove that task from the list and start counting from the next available task. Repeat this procedure until one task remains. Return it.

public class BusinessTasks {
    private String getTask(String[] list, int n) {
        String out = "";
        boolean bl[] = new boolean[list.length];
        if (n==1) return list[list.length-1];
        if (list.length==2) {
            if(n%2==0)
                return list[0];
            else
                return list[1];
        }
        int i=0,c=0, l=list.length;
        while(l>1){
            if(!bl[c]) i++;
            if(i==n) {
                bl[c]=true;
                i=0;
                l--;
            }
            c++;
            if(c==list.length) c=0;
        }
        for (i =0; i<list.length;i++)
            if(!bl[i]) return list[i];
        return out;
    }
    public static void main(String args[]) {
        BusinessTasks bt = new BusinessTasks();
        System.out.println(bt.getTask(new String[]    {"a", "b", "c", "d"}, 2     ));
        System.out.println(bt.getTask(new String[]    {"a", "b", "c", "d", "e"}, 3     ));
        System.out.println(bt.getTask(new String[]    {"alpha", "beta", "gamma", "delta", "epsilon"}, 1     ));
        System.out.println(bt.getTask(new String[]    {"a", "b"}, 1000     ));
        System.out.println(bt.getTask(new String[]    {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}, 17     ));
        System.out.println(bt.getTask(new String[]    {"zlqamum", "yjsrpybmq", "tjllfea", "fxjqzznvg", "nvhekxr", "am", "skmazcey", "piklp", "olcqvhg", "dnpo", "bhcfc", "y", "h", "fj", "bjeoaxglt", "oafduixsz", "kmtbaxu", "qgcxjbfx", "my", "mlhy", "bt", "bo", "q"}, 9000000     ));
        System.out.println(bt.getTask(new String[]    {"l", "celzw", "vp", "n", "nkmyi", "uaquiikr", "tgu"}, 7836541     ));
        System.out.println(bt.getTask(new String[]    {"yhvadcqtq", "myr", "mjoyirny", "nofaqvinol", "trxphmasx", "yblchjotr"}, 5561784     ));
        System.out.println(bt.getTask(new String[]    {"dfnm", "k"}, 4147202     ));
        System.out.println(bt.getTask(new String[]    {"jegzlqohby", "gvscbzrxvq", "yoznfwvxfr", "wtprk", "zmvqlj", "ughtyb", "cgdincc", "ztsuv", "i", "ba", "jotel", "jwn", "erfhr", "zdhxgfnf", "hkclj", "pfqy", "fkfrxvnp", "dzmucuzru", "oqhaob", "v", "mgbiukmk", "rqykcqogwl", "ot", "f", "mam", "bag", "ildun", "qtlof", "xvlcbdl", "fedopz", "nl"}, 3827158     ));
    }
}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: