191. Betting Money

You run a gambling business in which people place bets on the margin of victory in a football game. At the end of the day, the company would like to know what the day’s net gain has been.   Just as in any other betting system, people place certain amounts as their bets and if they guess correctly, they get their money back plus a pre-specified percentage of their bet; otherwise they lose the money they bet.

You are given a int[], amounts, the ith element of which is the number of dollars people have placed on a victory margin of i (i = 0 refers to the first element). You are also given a int[], centsPerDollar, the ith element of which is the number of cents the company has to pay for every dollar the people bet on a victory margin of i, if the final outcome is a victory margin of i. Finally, you are given an int, finalResult, which is the final margin of victory. You have to determine what the net gain for the day was and return the amount in cents.

For example, if amounts were {10,20,30}, it would mean that people placed $10 on a draw outcome, $20 on a victory margin of 1 and $30 on a victory margin of 2, and if centsPerDollar were {20,30,40}, it would mean the people would win 20 cents per dollar bet if the match were a draw, 30 cents per dollar if the victory margin were 1 and 40 cents per dollar if the victory margin were 2.

Suppose the final result is a victory margin of 1 (i.e., finalResult = 1). Then the people who guessed the outcome as a margin of 0 or 2 were wrong and the company receives the amounts they bet, $10+$30. However, the people who guessed that the outcome would be a margin of 1 were correct, and they receive money from the company according to the amount they bet (20 dollars) and the pre-set payoff percentage (30 cents per dollar) . This amounts to 20*30 = 600 cents. Hence, the day’s net gain is 40 dollars – 600 cents = 3400 cents. You should return 3400.

public class BettingMoney {

    private void moneyMade(int[] amounts, int[] centsPerDollar, int finalResult) {
        int gain =0;
        
        for (int i=0;i<amounts.length;i++) {
            if (i!=finalResult) {
                gain += amounts[i]*100;
            } else {
                gain -= amounts[i]*centsPerDollar[i];
            }
        }
        
        System.out.println(gain);
    }
    
    public static void main(String[] args) {
        BettingMoney bm = new BettingMoney();
        bm.moneyMade(new int[]{10,20,30}, new int[]{20,30,40}, 1);
        bm.moneyMade(new int[]{200,300,100}, new int[]{10,10,10}, 2);
        bm.moneyMade(new int[]{100,100,100,100}, new int[]{5,5,5,5}, 0);
        bm.moneyMade(new int[]{5000,5000}, new int[]{100,2}, 0);
        bm.moneyMade(new int[]{5000,5000}, new int[]{100,2}, 1);
        bm.moneyMade(new int[]{123, 342, 424, 524, 234, 634, 1000}, new int[]{12, 32, 42, 62, 12, 53, 5000}, 6);
        bm.moneyMade(new int[]{5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000}, new int[]{5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000}, 0);
        bm.moneyMade(new int[]{5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000}, new int[]{0, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000}, 0);
        bm.moneyMade(new int[]{100, 0, 0, 20}, new int[]{10, 100, 1000, 1}, 1);
        bm.moneyMade(new int[]{0, 0, 0, 0}, new int[]{0, 0, 0, 0}, 0);
        bm.moneyMade(new int[]{100, 20, 20, 20}, new int[]{10, 10, 5000, 10}, 2);
        bm.moneyMade(new int[]{100}, new int[]{10}, 0);
        bm.moneyMade(new int[]{100, 2000}, new int[]{10, 100}, 0);
        bm.moneyMade(new int[]{100}, new int[]{100}, 0);
        bm.moneyMade(new int[]{1249, 1495, 1060, 2165, 2147, 872, 1033, 3364, 613, 3030, 3306, 1267, 2973, 1997, 2183, 1820, 3308, 2167, 1471, 3005, 2265, 4612, 3592, 2485, 1189, 993, 4185, 2681, 1642, 1978, 2735, 1211, 1514, 572, 4450, 4231, 1306, 1161, 748, 4222, 2585, 2450, 2764, 3697, 3849, 571, 2732, 4828, 2400, 1856}, new int[]{2055, 654, 3243, 3537, 3040, 3274, 2424, 4773, 1207, 3325, 1110, 3448, 466, 3846, 3118, 1708, 16, 2260, 1607, 1452, 778, 834, 2925, 4370, 470, 4445, 2112, 4850, 4245, 3194, 1432, 1506, 2034, 4910, 3384, 1608, 288, 4811, 4014, 2088, 2403, 113, 1176, 1814, 3267, 2205, 2033, 1434, 859, 1706}, 49);         
    }
}
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: