Skip to content
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

Max_circular_sum_added 2-C/C++ #2354

Merged
merged 1 commit into from
Mar 21, 2020
Merged
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
77 changes: 77 additions & 0 deletions Max_Circular_SubArray_Sum/max_circular_subArray_sum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Question- Given n numbers (both +ve and -ve), arranged in a circle, find the maximum sum of consecutive number.
Solution- There are two cases for this problem
Case-1 The elements that contribute to the maximum sum are arranged such that no wrapping is there.Kadane will simply work.
Case-2 The elements which contribute to the maximum sum are arranged such that wrapping is there.
*/

#include<stdio.h>

//Returns maximum of two numbers.
int max(int num1, int num2)
{
return (num1 > num2 ) ? num1 : num2;
}

/*Function to find contiguous sub-array with the largest sum
in given set of integers*/
int kadaneAlgo(int a[], int n)
{
int max_so_far = 0, max_ending_here = 0;

for(int i = 0; i < n ; i++)
{
max_ending_here += a[i];

if(max_ending_here < 0)
{
max_ending_here = 0;
}

max_so_far = max(max_ending_here, max_so_far);
}

return max_so_far;
}

int circular_subarray_sum(int a[], int n)
{
//answer for case 1
int kadane_max = kadaneAlgo(a, n);

//find total sum and negate all elements of the array
int total_sum = 0;
for(int i = 0; i < n ; i++)
{
total_sum + = a[i];
a[i] = -a[i];

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this line

}

/*find out the sum of non contributing elements and subtract this sum from the
total sum.*/
total_sum = total_sum + kadaneAlgo(a, n);
return max(kadane_max, total_sum);
}

int main()
{
int n;
scanf("%d", &n);
int a[n];

for(int i = 0; i < n ; i++)
{
scanf("%d", &a[i]);
}
printf("The sum of subarray with the largest sum is: %d", circular_subarray_sum(a, n));
return 0;
}

/*
Input-
9
2 1 -8 4 -5 1 -7 4 -1
Output-
The sum of subarray with the largest sum is 6
*/
70 changes: 70 additions & 0 deletions Max_Circular_SubArray_Sum/max_circular_subarray_sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Question- Given n numbers (both +ve and -ve), arranged in a circle, find the maximum sum of consecutive number.
Solution- There are two cases for this problem.
Case-1 The elements that contribute to the maximum sum are arranged such that no wrapping is there.Kadane will simply work.
Case-2 The elements which contribute to the maximum sum are arranged such that wrapping is there.
*/

#include<iostream>
using namespace std;

/* Function to find contiguous sub-array with the largest sum
in given set of integers*/
int kadaneAlgo(int a[], int n)
{
int max_so_far=0, max_ending_here = 0;

for(int i = 0; i < n; i++)
{
max_ending_here += a[i];
if(max_ending_here < 0)
{
max_ending_here = 0;
}

max_so_far = max(max_ending_here, max_so_far);
}
return max_so_far;
}

int circular_subarray_sum(int a[], int n)
{
//answer for case 1.
int kadane_max = kadaneAlgo(a, n);

//Find total sum and negate all elements of the array.
int total_sum = 0;
for(int i = 0; i < n; i++)
{
total_sum += a[i];
a[i] = -a[i];

}
/*Ans for test case 2.
Find out the sum of non contributing elements and subtract this sum from the Total sum.*/
total_sum = total_sum + kadaneAlgo(a, n);
return max(kadane_max, total_sum);
}

int main()
{
int n;
cin >> n;
int a[n];

for(int i = 0; i < n; i++)
{
cin >> a[i];
}

cout << "The sum of subarray with the largest sum is: " << circular_subarray_sum(a, n) << endl;
return 0;
}

/*
Input-
9
2 1 -8 4 -5 1 -7 4 -1
Output-
The sum of subarray with the largest sum is 6
*/