Menu Close

Program to find Richest Customer Wealth

Here, we will see Program to find Richest Customer Wealth with code and algorithm.

You are given an m x n integer grid accounts where accounts[i][j] is the amount of money the i​​​​​​​​​​​th​​​​ customer has in the j​​​​​​​​​​​th​​​​ bank. You have to return the wealth that the richest customer has.

A customer’s wealth is the amount of money they have in all their bank accounts. The richest customer is the customer that has the maximum wealth.

Example:

1)
Input: accounts = [[1,2,3],[3,2,1]]
Output: 6
Explanation: 1st customer has wealth = 1 + 2 + 3 = 6
2nd customer has wealth = 3 + 2 + 1 = 6
Both customers are considered the richest with a wealth of 6 each, so return 6.

2)
Input: accounts = [[1,5],[7,3],[3,5]]
Output: 10
Explanation: 1st customer has wealth = 6
2nd customer has wealth = 10 
3rd customer has wealth = 8
The 2nd customer is the richest with a wealth of 10.

3)
Input: accounts = [[2,8,7],[7,1,3],[1,9,5]]
Output: 17
Explanation: 1st customer has wealth = 17
2nd customer has wealth = 11
3rd customer has wealth = 15
The 1st customer is the richest with a wealth of 17.

Program to find Richest Customer Wealth code in C++

Code 1:

#include <iostream>
#include <vector>
#include <limits.h>

using namespace std;

int maximumWealth(vector<vector<int>>& accounts) {
    int max = INT_MIN;
        
    // accounts.size(): Get the number of total rows in accounts matrix
    for(int i = 0; i < accounts.size(); i++) {
        int sum = 0;

        // accounts[0].size(): Number of total columns in accounts matrix
        for(int j = 0; j < accounts[0].size(); j++) {
            sum += accounts[i][j];
        }
        if(sum > max) {
            max = sum;
        }
    }
    return max;
}
    
int main()
{
    vector<vector<int> > accounts = {
        {2,8,7},
        {7,1,3},
        {1,9,5}
    };
    
    cout<<maximumWealth(accounts);
    
    return 0;
}

Code 2:

#include <iostream>
#include <vector>
#include <limits.h>
#include <numeric>

using namespace std;

int maximumWealth(vector<vector<int>>& accounts) 
{
    int maxi = INT_MIN;

    for(auto i :accounts)
    {
        int sum = accumulate(i.begin(),i.end(),0) ;
        maxi = max(maxi,sum) ;
    }
    return maxi;
}
    
int main()
{
    vector<vector<int> > accounts = {
        {2,8,7},
        {7,1,3},
        {1,9,5}
    };
    
    cout<<maximumWealth(accounts);
    
    return 0;
}

Output:

17

To check more leetcode problem’s solution. Pls click given below link:

https://www.techieindoor.com/category/leetcode/

Posted in C++, Easy

Leave a Reply

Your email address will not be published.

Contact Us