Maximum difference between two elements such that larger element appears after the smaller number | GeeksforGeeks
Given an array arr[] of integers, find out the difference between any two elements such that larger element appears after the smaller number in arr[].
We take the difference with the minimum element found so far. So we need to keep track of 2 things:
1) Maximum difference found so far (max_diff).
2) Minimum number visited so far (min_element).
Also check http://massivealgorithms.blogspot.com/2014/07/sell-stock-darrens-blog.html
http://www.geeksforgeeks.org/maximum-profit-by-buying-and-selling-a-share-at-most-twice/
Read full article from Maximum difference between two elements such that larger element appears after the smaller number | GeeksforGeeks
Given an array arr[] of integers, find out the difference between any two elements such that larger element appears after the smaller number in arr[].
We take the difference with the minimum element found so far. So we need to keep track of 2 things:
1) Maximum difference found so far (max_diff).
2) Minimum number visited so far (min_element).
int maxDiff(int arr[], int arr_size){ int max_diff = arr[1] - arr[0]; int min_element = arr[0]; int i; for(i = 1; i < arr_size; i++) { if(arr[i] - min_element > max_diff) max_diff = arr[i] - min_element; if(arr[i] < min_element) min_element = arr[i]; } return max_diff;}
Like min element, we can also keep track of max element from right side. See below code suggested by Katamaran
int maxDiff(int arr[], int n){ int maxDiff = -1; // Initialize Result int maxRight = arr[n-1]; // Initialize max element from right side for (int i = n-2; i >= 0; i--) { if (arr[i] > maxRight) maxRight = arr[i]; else { int diff = maxRight - arr[i]; if (diff > maxDiff) { maxDiff = diff; } } } return maxDiff;} |
Method 3 (Another Tricky Solution)
First find the difference between the adjacent elements of the array and store all differences in an auxiliary array diff[] of size n-1. Now this problems turns into finding the maximum sum subarray of this difference array.int maxDiff(int arr[], int n)=
{
// Create a diff array of size n-1. The array will hold
// the difference of adjacent elements
int diff[n-1];
for (int i=0; i < n-1; i++)
diff[i] = arr[i+1] - arr[i];
// Now find the maximum sum subarray in diff array
int max_diff = diff[0];
for (int i=1; i<n-1; i++)
{
if (diff[i-1] > 0)
diff[i] += diff[i-1];
if (max_diff < diff[i])
max_diff = diff[i];
}
return max_diff;
}
We can modify the above method to work in O(1) extra space. Instead of creating an auxiliary array, we can calculate diff and max sum in same loop. Following is the space optimized version.
int maxDiff (int arr[], int n)
{
// Initialize diff, current sum and max sum
int diff = arr[1]-arr[0];
int curr_sum = diff;
int max_sum = curr_sum;
for(int i=1; i<n-1; i++)
{
// Calculate current diff
diff = arr[i+1]-arr[i];
// Calculate current sum
if (curr_sum > 0)
curr_sum += diff;
else
curr_sum = diff;
// Update max sum, if needed
if (curr_sum > max_sum)
max_sum = curr_sum;
}
return max_sum;
}
http://www.geeksforgeeks.org/maximum-profit-by-buying-and-selling-a-share-at-most-twice/
Read full article from Maximum difference between two elements such that larger element appears after the smaller number | GeeksforGeeks