Hello, I am studying for an interview coding exam and came across this question. I attempted it in C#; here is my embarrassing response; I'm not sure if it's correct, but I'm guessing not; could someone maybe kindly share the answer so that when I rework on the solution, I can at least have the answer to validate the output? Thanks.
Sample data:
Code:
int[] arr = {5, 1, -7, 3, 7};
My code:
Code:
int[] LargestsubarrayMaxSum(int[] arr){
int temp = 0;
int[] resultArr = new int[arr.Length];
for (int i = 0; i < arr.Length - 1; i++)
{
if (i != 0)
{
foreach (int item in resultArr)
{
temp += item;
}
if (temp + arr[i + 1] > 0)
{
resultArr[i + 1] = temp + arr[i + 1];
}
}
else
{
if ((arr[i] + arr[i + 1]) >= 0)
{
resultArr[i] = arr[i];
resultArr[i + 1] = arr[i] + arr[i + 1];
}
else
{
resultArr[i] = arr[i];
resultArr[i + 1] = 0;
}
}
}
return resultArr; }
According to this <<rubbish url>>, I could either apply Enigmativity's solution or add the additional order by using subseq.Count() descending.