Skip to content

Commit

Permalink
Status:solved
Browse files Browse the repository at this point in the history
  • Loading branch information
Sundar2k4 committed Aug 13, 2024
1 parent c639fa9 commit c22ec14
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
25 changes: 25 additions & 0 deletions maximum_of_minimum_subarray_sum.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution
{
public:
// Function to find pair with maximum sum
int pairWithMaxSum(vector<int> &arr)
{
int n = arr.size();
if (n < 2)
return -1; // If there are fewer than 2 elements, return -1

int maxSum = INT_MIN;

// Iterate through the array, considering each consecutive pair
for (int i = 0; i < n - 1; i++)
{
int currentSum = arr[i] + arr[i + 1]; // Calculate the sum of each pair
if (currentSum > maxSum)
{
maxSum = currentSum; // Update maxSum if the current pair's sum is greater
}
}

return maxSum;
}
};
27 changes: 27 additions & 0 deletions stockbuyandsell.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution
{
public:
int maxProfit(vector<int> &prices)
{
int n = prices.size();
int minprice = INT_MAX;
int maxprofit = 0;
for (int i = 0; i < n; i++)
{
if (prices[i] < minprice)
{
minprice = prices[i];
}

else
{
int profit = prices[i] - minprice;
if (profit > maxprofit)
{
maxprofit = profit;
}
}
}
return maxprofit;
}
};

0 comments on commit c22ec14

Please sign in to comment.