Program to find wealth of richest customer in Python

Suppose we have a matrix of order m x n called accounts where accounts[i][j] is the amount of money of ith customer present in jth bank. We have to find the wealth that the richest customer has. A customer is richest when he/she has maximum amount considering all banks.

Example Input

So, if the input is like ?

10 20 15
30 5 20
10 5 12
15 12 3

Then the output will be 55 as the money of second person is 30+5+20 = 55, which is maximum.

Algorithm

To solve this, we will follow these steps ?

  • max_value := 0

  • For each customer in accounts, do

    • Calculate total wealth by summing all bank amounts

    • If current wealth > max_value, then

      • max_value := current wealth

  • Return max_value

Implementation

Let us see the following implementation to get better understanding ?

def solve(accounts):
    max_value = 0
    
    for i in range(len(accounts)):
        current_wealth = sum(accounts[i])
        if current_wealth > max_value:
            max_value = current_wealth
    
    return max_value

accounts = [[10, 20, 15],
            [30, 5, 20],
            [10, 5, 12],
            [15, 12, 3]]

print("Richest customer wealth:", solve(accounts))
Richest customer wealth: 55

Alternative Solution Using max() Function

We can also solve this problem using Python's built-in max() function with a generator expression ?

def find_richest_wealth(accounts):
    return max(sum(customer) for customer in accounts)

accounts = [[10, 20, 15],
            [30, 5, 20],
            [10, 5, 12],
            [15, 12, 3]]

wealth = find_richest_wealth(accounts)
print("Maximum wealth:", wealth)

# Let's see individual customer wealth
for i, customer in enumerate(accounts):
    print(f"Customer {i+1}: {sum(customer)}")
Maximum wealth: 55
Customer 1: 45
Customer 2: 55
Customer 3: 27
Customer 4: 30

Conclusion

Both approaches find the richest customer's wealth by summing each customer's bank balances. The first method uses a manual loop, while the second uses Python's max() function for a more concise solution.

Updated on: 2026-03-25T20:26:36+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements