Skip to content

Commit

Permalink
requested changes fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
MdAkdas committed Mar 13, 2020
1 parent a7cc42d commit 6654356
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 66 deletions.
62 changes: 30 additions & 32 deletions Max_Circular_SubArray_Sum/max_circular_subArray_sum.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
/*
Que-Given n numbers (both +ve and -ve), arranged in a circle,
find the maximum sum of consecutive number.
Sol-there are two cases for this problem
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.
Wrapping of contributing elements implies non wrapping of non contributing elements.
Invert sign of each element and then run Kadane’s algorithm.
and substract from total sum to find out the sum of contributing elements.
Case-2 The elements which contribute to the maximum sum are arranged such that wrapping is there.
*/

#include<stdio.h>

//Returns max. of two no.
//Returns maximum of two numbers.
int max(int num1, int num2)
{
return (num1 > num2 ) ? num1 : num2;
Expand All @@ -25,48 +21,50 @@ int kadaneAlgo(int a[], int n)

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

if(max_ending_here < 0)
max_ending_here = 0;
max_ending_here += a[i];

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

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

return max_so_far;
}

int circular_subarray_sum(int a[], int n)
{
//ans for case 1
int kadane_max=kadaneAlgo(a, 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];
int total_sum = 0;
for(int i = 0; i < n ; i++)
{
total_sum + = a[i];
a[i] = -a[i];

}
}

/*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);
/*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];
scanf("%d", &n);
int a[n];

for(int i = 0; i < n ; i++)
scanf("%d", &a[i]);
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;
return 0;
}

/*
Expand All @@ -75,4 +73,4 @@ Input-
2 1 -8 4 -5 1 -7 4 -1
Output-
The sum of subarray with the largest sum is 6
*/
*/
65 changes: 31 additions & 34 deletions Max_Circular_SubArray_Sum/max_circular_subarray_sum.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
/*
Que-Given n numbers (both +ve and -ve), arranged in a circle,
find the maximum sum of consecutive number.
Sol-there are two cases for this problem.
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.
Wrapping of contributing elements implies non wrapping of non contributing elements.
Invert sign of each element and then run Kadane’s algorithm.
and substract from total sum to find out the sum of contributing elements.
Case-2 The elements which contribute to the maximum sum are arranged such that wrapping is there.
*/

#include<iostream>
Expand All @@ -16,50 +12,51 @@ using namespace std;
in given set of integers*/
int kadaneAlgo(int a[], int n)
{
int max_so_far=0,max_ending_here = 0;
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;
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;
max_so_far = max(max_ending_here, max_so_far);
}
return max_so_far;
}

int circular_subarray_sum(int a[], int n)
{
//Ans for case 1.
int kadane_max = kadaneAlgo(a, 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];
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);
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];
cin >> n;
int a[n];

for(int i = 0; i < n; i++)
cin >> a[i];
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;
return 0;
}

/*
Expand All @@ -68,4 +65,4 @@ Input-
2 1 -8 4 -5 1 -7 4 -1
Output-
The sum of subarray with the largest sum is 6
*/
*/

0 comments on commit 6654356

Please sign in to comment.