Skip to content

Commit

Permalink
Merge pull request gdscucer#2 from Kartikey0205/dsa
Browse files Browse the repository at this point in the history
Added array programs
  • Loading branch information
gdscucer authored Oct 1, 2022
2 parents c845f37 + 4c0c0ce commit 2535d2d
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 0 deletions.
31 changes: 31 additions & 0 deletions array/subarrays/kadane_Algo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <iostream>
#include <climits>
using namespace std;
int main()
{
int size;
cout << "Enter the size of an array ";
cin >> size;

int arr[size];
cout << "Enter the elements of an array" << endl;
for (int i = 0; i < size; i++)
{
cin >> arr[i];
}
int maxSum = INT_MIN;

int currSum = 0;
for (int i = 0; i < size; i++)
{
currSum = currSum + arr[i];
if (currSum < 0)
{
currSum = 0;
}
maxSum = max(maxSum, currSum);
}

cout << "Maximum sum of subarray is " << maxSum;
return 0;
}
43 changes: 43 additions & 0 deletions array/subarrays/maxCircularSum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <iostream>
#include <climits>
using namespace std;
int kadane_Sum(int arr[], int size)
{
int maxSum = INT_MIN;

int currSum = 0;
for (int i = 0; i < size; i++)
{
currSum = currSum + arr[i];
if (currSum < 0)
{
currSum = 0;
}
maxSum = max(maxSum, currSum);
}
return maxSum;
}
int main()
{
int size;
cout << "Enter the size of an array ";
cin >> size;

int arr[size];
cout << "Enter the elements of an array" << endl;
for (int i = 0; i < size; i++)
{
cin >> arr[i];
}
int normalSum = kadane_Sum(arr, size);
int totalSum = 0, circularSum;
for (int i = 0; i < size; i++)
{
totalSum += arr[i];
arr[i] = -arr[i];
}

circularSum = totalSum - (-kadane_Sum(arr, size));
cout << "Maximum sum of subarray is " << max(circularSum, normalSum);
return 0;
}
33 changes: 33 additions & 0 deletions array/subarrays/maxSubarraySum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>
#include <climits>
using namespace std;
int main()
{
int size;
cout << "Enter the size of an array ";
cin >> size;

int arr[size];
cout << "Enter the elements of an array" << endl;
for (int i = 0; i < size; i++)
{
cin >> arr[i];
}

int maxSum = INT_MIN;
for (int i = 0; i < size; i++)
{

for (int j = i; j < size; j++)
{
int sum = 0;
for (int k = i; k <= j; k++)
{
sum += arr[k];
}
maxSum = max(maxSum, sum);
}
}
cout << "Maximum sum of subarray is " << maxSum;
return 0;
}
26 changes: 26 additions & 0 deletions array/subarrays/pairSum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <iostream>
using namespace std;
bool pairSum(int arr[], int size, int k)
{
for (int i = 0; i < size - 1; i++)
{
for (int j = i + 1; j < size; j++)
{
if (arr[i] + arr[j] == k)
{
cout << i << " and " << j << endl;
return true;
}
}
}
return false;
}
int main()
{
int arr[] = {2, 4, 7, 11, 14, 16, 20, 21};
int k = 31;

cout << pairSum(arr, 8, k);

return 0;
}
31 changes: 31 additions & 0 deletions array/subarrays/possibleSubarrays.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <iostream>
using namespace std;
int main()
{
int size;
cout << "Enter the size of an array ";
cin >> size;

int arr[size];
cout << "Enter the elements of an array" << endl;
for (int i = 0; i < size; i++)
{
cin >> arr[i];
}

for (int i = 0; i < size; i++)
{

for (int j = i; j < size; j++)
{
for (int k = i; k <= j; k++)
{
cout << arr[k] << " ";
}
cout << endl;
}
cout << endl;
}

return 0;
}

0 comments on commit 2535d2d

Please sign in to comment.