Share Money

DNS decided to share moeny with his friends instead. He knows how much money each of his friends has. While he still has money left, he will repeat the following steps: Choose the poorest friend. If there are multiple poorest friends, choose one of them randomly. Give 1 dollar to the chosen friend. The return value must contain the number of dollars owned by each friend after DNS has performed the above distribution, sorted in non-decreasing order.
input => int[] friendmoney, int moneyshare
output => int[] friendmoney (distributed sorted in non-decreasing order)
Examples
1)i/p: {1, 2, 3, 4} 2 o/p: {2, 3, 3, 4 }
2)i/p: {4, 7} 1 o/p: {5, 7}
3)i/p: {1} 100 o/p: 101
4)i/p: {21, 85, 6, 54, 70, 100, 91, 60, 71} 15 100 o/p: {21, 21, 54, 60, 70, 71, 85, 91, 100 }
5)i/p: {68, 30, 5, 66, 69, 25, 58}, 55 o/p: {38, 38, 39, 58, 66, 68, 69}
6)i/p: {45, 44, 24, 92, 29, 94}, 45 o/p: {46, 47, 47, 47, 92, 94}

import java.util.*;
public class TheJackpotDivTwo {
    public int[] find(int[] money, int jackpot) {
        while(jackpot>0) {
            Arrays.sort(money);
            money[0]++;
            jackpot--;
        }
        Arrays.sort(money);
        return money;
    }

    public static void main(String[] args) {
        TheJackpotDivTwo jd = new TheJackpotDivTwo();
        System.out.println(Arrays.toString(jd.find(new int[]{1, 2, 3, 4}, 2)));
        System.out.println(Arrays.toString(jd.find(new int[]{4,7}, 1)));
        System.out.println(Arrays.toString(jd.find(new int[]{1}, 100)));
        System.out.println(Arrays.toString(jd.find(new int[]{21, 85, 6, 54, 70, 100, 91, 60, 71}, 15)));
        System.out.println(Arrays.toString(jd.find(new int[]{21, 85, 6, 54, 70, 100, 91, 60, 71}, 55)));
        System.out.println(Arrays.toString(jd.find(new int[]{68, 30, 5, 66, 69, 25, 58}, 55)));
    }
}
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: