forked from gdscucer/open_DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request gdscucer#2 from Kartikey0205/dsa
Added array programs
- Loading branch information
Showing
5 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |