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.
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.
To solve this, we will follow these steps −
max_balue := 0
ind_value := 0
for i in range 0 to row count of accounts - 1, do
ind_value := sum of all values in accounts[i]
if ind_value > max_balue, then
max_balue := ind_value
return max_balue
Example (Python)
Let us see the following implementation to get better understanding −
def solve(accounts): max_balue = 0 ind_value = 0 for i in range(len(accounts)): ind_value = sum(accounts[i]) if ind_value > max_balue: max_balue = ind_value return max_balue accounts = [[10,20,15], [30,5,20], [10,5,12], [15,12,3]] print(solve(accounts ))
Input
[[10,20,15], [30,5,20], [10,5,12], [15,12,3]]
Output
55