diff --git a/largest sum of subarray.cpp b/largest sum of subarray.cpp index cdda3d4..fc4b2b9 100644 --- a/largest sum of subarray.cpp +++ b/largest sum of subarray.cpp @@ -1,31 +1,31 @@ -#include +#include //Including header file using namespace std; -int main(){ - int n,el,i,j,max; - cout << "Enter size of array " << endl; - cin >> n; - int data[n]; - cout << "Enter the elements " << endl; - for (i=0;i> el; - data[i] = el; +int main(){ //Main Function + int n,el,i,j,max; //Declaring variables + cout << "Enter size of array " << endl; //getting the size of array + cin >> n; // Storing that value in var n + int data[n]; //Defining an array of size n + cout << "Enter the elements " << endl; + for (i=0;i> el; // Getting all array value and storing it in the prev created array + data[i] = el; // } - int ans[n]; - ans[0] = data[0]; - for (i=1;i data[i]){ - ans[i] = data[i] + ans[i-1]; - } - else{ - ans[i] = data[i]; + int ans[n]; //Creating a new array 'ans' + ans[0] = data[0]; //First value of ans = First value in data + for (i=1;i data[i]){ //Condition check if value at 'ans' and corresponding next value in data sum is greater than data[i] + ans[i] = data[i] + ans[i-1]; //If so, that is the current max sub array sum + } + else{ + ans[i] = data[i]; // Else value without adding is the max sub array sum } } max = -100000; for (i=0;i