Festival Coin Path Count
Given an array of coin values placed along a path, count how many coins have a value greater than the average of all coins. Return the count of above-average coins.
For example, coins [2, 4, 6, 8, 10] have average 6. Coins greater than 6 are 8 and 10 — count is 2. Coins [1, 2, 3] have average 2. Coins greater than 2 are 3 — count is 1.
This problem teaches two-pass processing: first compute the average, then count elements exceeding it. It combines summation, division, and conditional counting.
The solution first sums all elements and computes the average. Then iterates again, counting elements strictly greater than the average.
Edge cases include an empty array (return 0), all coins equal to the average (return 0), and all coins above average (return the array length).
Example Input & Output
Only the lower-right path remains open.
The starting stall is blocked, so no path exists.
Two routes skirt the blocked middle stall.
Algorithm Flow
Solution Approach
Compute the average in one pass, then count above-average elements in a second pass.
Handle empty array. Compute sum and average. Iterate again to count elements strictly greater than the average. Return the count.
Time complexity is O(n), space complexity is O(1).
Best Answers
import java.util.*;
class Solution {
public int festival_coin_path_count(Object grid) {
int[][] g = (int[][]) grid;
if (g.length == 0) return 0;
int m = g.length, n = g[0].length;
if (g[0][0] == 1) return 0;
long[] dp = new long[n];
dp[0] = 1;
long MOD = 1000000007;
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
if (g[r][c] == 1) dp[c] = 0;
else if (c > 0) dp[c] = (dp[c] + dp[c-1]) % MOD;
}
}
return (int) dp[n-1];
}
}
Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
