Hello,
I am trying to stop a loop at a specific cycle.
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Solution {
// Complete the maxSubsetSum function below.
static int maxSubsetSum(int[] arr) {
int size = arr.Length;
int max_so_far = int.MinValue,
max_ending_here = 0;
for (int i = 0; i < size; i++)
{
if (i % 2 == 0)
{
max_ending_here = max_ending_here + arr[i];
if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
// cant tell why the loop not stopping at the largest sum
Console.WriteLine("\t3rd loop cycle!: " +max_ending_here );
}
}
return max_so_far;
}
static void Main(string[] args) {
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
int n = Convert.ToInt32(Console.ReadLine());
int[] arr = Array.ConvertAll(Console.ReadLine().Split(' '), arrTemp => Convert.ToInt32(arrTemp))
;
int res = maxSubsetSum(arr);
textWriter.WriteLine(res);
textWriter.Flush();
textWriter.Close();
}
}
The output return :
Input (stdin)
5
3 7 4 6 5
Your Output (stdout)
12
Expected Output
13
Debug output
3rd loop cycle!: 3
3rd loop cycle!: 7
3rd loop cycle!: 12
5
3 5 -7 8 10
Your Output (stdout)
6
Expected Output
15
Debug output
3rd loop cycle!: 3
3rd loop cycle!: -4
3rd loop cycle!: 6
5
2 1 5 8 4
Only this output return the right answer.
Your Output (stdout)
11
Expected Output
11
Debug output
3rd loop cycle!: 2
3rd loop cycle!: 7
3rd loop cycle!: 11
Thank you
