-3

Im looking to receive a jagged array of type int[][] and then run through the arrays to find the specific array with the highest sum and then return that sum. Ive done some digging online and haven't found much info about how to sum up each array individually.

For example if this was the received input:

int[][] accounts = new int[3][];
            accounts[0] = new int[] { 1, 2, 3};
            accounts[1] = new int[] { 2, 3, 4 };
            accounts[2] = new int[] { 3, 4, 5 };

The closest I've come to this was getting the total sum of all elements

for (var i = 0; i < accounts.Length; i++)
{
    for (var j = 0; j < accounts[i].Length; j++)
        {
            sum += accounts[i][j];
        }
}

What step am I missing to separate the arrays and sum them up individually?

elementmg
  • 234
  • 2
  • 14

2 Answers2

1

Your issue is you are not keeping a max value around.

Given

var accounts = new int[][]
{
   new[] {1, 2, 3},
   new[] {2, 3, 4},
   new[] {3, 4, 5}
};

Example

var max = 0;
for (var i = 0; i < accounts.Length; i++)
{
   var sum = 0;
   for (var j = 0; j < accounts[i].Length; j++)
      sum += accounts[i][j];
   if (sum > max) 
      max = sum;
}

However, let's see how we can make this more succinct.

You could use foreach

foreach (var array in accounts)
{
   var sum = 0;
   foreach (var item in array)
      sum += item;
   if (sum > max) 
      max = sum;
}

You could use Linq Sum

foreach (var array in accounts)
{
   var sum = array.Sum();
   if (sum > max) 
      max = sum;
}

Or you can use Linq Max and Sum

var max = accounts.Max(x => x.Sum());

Additional Resources

Enumerable.Max Method

Returns the maximum value in a sequence of values.

Enumerable.Sum Method

Computes the sum of a sequence of numeric values.

halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
1

One easy to implement fix to your current attempt would be to just keep track of the maxAccountSum seen so far:

using System;

class MainClass
{
    public static void Main (string[] args)
    {
        int[][] accounts = new int[3][];
        accounts[0] = new int[] { 1, 2, 3 };
        accounts[1] = new int[] { 2, 3, 4 };
        accounts[2] = new int[] { 3, 4, 5 };     
        
        var maxAccountSum = Int32.MinValue; // To account for if the accounts have negative balances? Like they were all overdrafted or something.
        for (var i = 0; i < accounts.Length; i++)
        {
            var accountSum = 0;
            for (var j = 0; j < accounts[i].Length; j++)
            {
                accountSum += accounts[i][j];
            }
            maxAccountSum = Math.Max (maxAccountSum, accountSum);
        }

        Console.WriteLine ("The highest sum in an account is {0}.", maxAccountSum);
    }
}

Time-Space Analysis:

  • O(mn) time, where m is the number of accounts and n is the max number of values in an individual account.
  • O(1) space, since we are only keeping track of a couple extra variables.

Output:

The highest sum in an account is 12.
Sash Sinha
  • 18,743
  • 3
  • 23
  • 40