Skip to content

Commit 73fc172

Browse files
committed
Add Kadanes max sub array sum
1 parent 9b4d31b commit 73fc172

File tree

6 files changed

+119
-0
lines changed

6 files changed

+119
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace MaxSubArray;
2+
3+
public class Array
4+
{
5+
public static int MaxSubArraySum(int[] values)
6+
{
7+
if (values is null || values.Length == 0)
8+
return 0;
9+
10+
int best = values[0];
11+
int sum = best;
12+
13+
foreach (var x in values[1..])
14+
{
15+
sum = Math.Max(x, sum + x);
16+
best = Math.Max(best, sum);
17+
}
18+
19+
return best;
20+
}
21+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
global using Xunit;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace MaxSubArrayTests;
2+
3+
public class MaxSubArraySumTests
4+
{
5+
[Fact]
6+
public void Empty() =>
7+
Assert.Equal(0, MaxSubArray.Array.MaxSubArraySum([]));
8+
9+
[Fact]
10+
public void Single() =>
11+
Assert.Equal(3, MaxSubArray.Array.MaxSubArraySum([3]));
12+
13+
[Theory]
14+
[InlineData(new int[] { -4, 10 }, 10)]
15+
[InlineData(new int[] { 10, -4 }, 10)]
16+
[InlineData(new int[] { -6, -4 }, -4)]
17+
[InlineData(new int[] { 4, 6 }, 10)]
18+
public void Double(int[] input, int expected) =>
19+
Assert.Equal(expected, MaxSubArray.Array.MaxSubArraySum(input));
20+
21+
[Theory]
22+
[InlineData(new int[] { -1, 2, -5, 7 }, 7)]
23+
[InlineData(new int[] { -1, 2, -5, 2, 7 }, 9)]
24+
[InlineData(new int[] { -2, 1, -3, 4, -1, 2, 1, -5, 4 }, 6)]
25+
[InlineData(new int[] { -2, 4, -1, 2, 1, -5, 4, -9, 7, 8 }, 15)]
26+
public void NAry(int[] input, int expected) =>
27+
Assert.Equal(expected, MaxSubArray.Array.MaxSubArraySum(input));
28+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
<IsTestProject>true</IsTestProject>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
14+
<PackageReference Include="xunit" Version="2.4.2" />
15+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
16+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17+
<PrivateAssets>all</PrivateAssets>
18+
</PackageReference>
19+
<PackageReference Include="coverlet.collector" Version="6.0.0">
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
<PrivateAssets>all</PrivateAssets>
22+
</PackageReference>
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<ProjectReference Include="..\MaxSubArray\MaxSubArray.csproj" />
27+
</ItemGroup>
28+
29+
</Project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.8.34511.84
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MaxSubArray", "MaxSubArray\MaxSubArray.csproj", "{30B85A6A-FBC3-47D3-AD72-063D49F1D449}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MaxSubArrayTests", "MaxSubArrayTests\MaxSubArrayTests.csproj", "{A58D8ED0-84DF-4C7B-83E4-528782010E0A}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{30B85A6A-FBC3-47D3-AD72-063D49F1D449}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{30B85A6A-FBC3-47D3-AD72-063D49F1D449}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{30B85A6A-FBC3-47D3-AD72-063D49F1D449}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{30B85A6A-FBC3-47D3-AD72-063D49F1D449}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{A58D8ED0-84DF-4C7B-83E4-528782010E0A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{A58D8ED0-84DF-4C7B-83E4-528782010E0A}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{A58D8ED0-84DF-4C7B-83E4-528782010E0A}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{A58D8ED0-84DF-4C7B-83E4-528782010E0A}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {755B3D90-E656-41A8-AF2D-DAEA8FF0CD65}
30+
EndGlobalSection
31+
EndGlobal

0 commit comments

Comments
 (0)