Skip to content

Add files via upload #283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions classicalAlgos/kadanesAlgorithm/kdanesAlgorithm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Name: Kdane's Algorithm in C#
// Function: Finds the subarray with the maximum sum for an array of numbers
// Time Complexity: O(n)
// Space Complexity: O(1)

static int kdanesAlgorithm(int[] array)
{
// Store the highest sum encountered thus far and the current sum
int maxSum = Int32.MinValue;
int currentSum = 0;

for (int i = 0; i < array.Length; i++)
{
// Decide whether to (largest is picked):
// (1) Extend the existing subarray (currentSum + array[i])
// (2) Start a new subarray from the current element (array[i])
currentSum = Math.Max(array[i], currentSum + array[i]);

// Update maxSum if the currentSum exceeds it
if (currentSum > maxSum)
maxSum = currentSum;
}

return maxSum;
}

// MAIN(), using Top-Level Statements

// Test Case 1: Mix of positive and negative numbers
// Expected Output: 6
int[] test1 = { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
Console.WriteLine("Test 1 (Mixed Numbers): " + KdanesAlgorithm(test1));

// Test Case 2: All negative numbers
// Expected Output: -1
int[] test2 = { -5, -2, -3, -7, -1 };
Console.WriteLine("Test 2 (All Negatives): " + KdanesAlgorithm(test2));

// Test Case 3: All positive numbers
// Expected Output: 15
int[] test3 = { 2, 3, 1, 5, 4 };
Console.WriteLine("Test 3 (All Positives): " + KdanesAlgorithm(test3));
47 changes: 47 additions & 0 deletions classicalAlgos/kadanesAlgorithm/kdanesAlgorithm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Name: Kdane's Algorithm in Rust
// Function: Finds the subarray with the maximum sum for an array of numbers
// Time Complexity: O(n)
// Space Complexity: O(1)

use std::cmp;

// Function takes an immutable reference to an array as mutability/ownership is not needed
fn kdanesAlgorithm(array: &[i32]) -> i32 {
// Store the highest sum encountered thus far and the current sum
let mut maxSum = i32::MIN;
let mut currentSum = 0;

for currentElement in array.iter() {
// Decide whether to (largest is picked):
// (1) Extend the existing subarray (currentSum + array[i])
// (2) Start a new subarray from the current element (array[i])

// Use .clone() to retain ownership of currentSum
currentSum = cmp::max(currentSum.clone() + currentElement, currentElement.clone());

// Update maxSum if the currentSum exceeds it
if currentSum > maxSum {
maxSum = currentSum;
}
}
// Return maxSum
maxSum
}

fn main() {
// Test Case 1: Mixed positive and negative numbers
// Expected Output: 6
let test1 = [-2, 1, -3, 4, -1, 2, 1, -5, 4];
println!("Test 1 (Mixed Numbers): {}", kdanesAlgorithm(&test1));

// Test Case 2: All negative numbers
// Expected Output: -1
let test2 = [-5, -2, -3, -7, -1];
println!("Test 2 (All Negatives): {}", kdanesAlgorithm(&test2));

// Test Case 3: All positive numbers
// Expected Output: 15
let test3 = [2, 3, 1, 5, 4];
println!("Test 3 (All Positives): {}", kdanesAlgorithm(&test3));
}