问题
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[].
Max Difference = Max { arr[x] - arr[y] | x > y }
Examples:
If array is
[2, 3, 10, 6, 4, 8, 1, 7]
then returned value should be 8 (Diff between 10 and 2).If array is
[ 7, 9, 5, 6, 3, 2 ]
then returned value should be 2 (Diff between 7 and 9)
My Algorithm:
I thought of using D&C algorithm. Explanation
2, 3, 10, 6, 4, 8, 1, 7
then
2,3,10,6 and 4,8,1,7
then
2,3 and 10,6 and 4,8 and 1,7
then
2 and 3 10 and 6 4 and 8 1 and 7
Here as these elements will remain in same order, i will get the maximum difference, here it's 6.
Now i will move back to merege these arrays and again find the difference between minimum of first block and maximum of second block and keep doing this till end.
I am not able to implement this in my code. can anyone please provide a pseudo code for this?
回答1:
We have max { A[i] - A[j] | i > j } = max { A[i] - min { A[j] | j < i } | i }
, which yields
a straightforward O(n) algorithm:
prefix_min = A[0]
result = -infinity
for i := 1 to n - 1:
# invariant: We have prefix_min = min { A[j] | j < i }
result = max(result, A[i] - prefix_min)
prefix_min = min(prefix_min, A[i])
Divide & Conquer is conceptionally more complicated, but also leads to a linear time solution (with a higher constant factor).
回答2:
Say you want to find the largest Difference LD(A[])
Complete Psuedocode as desired:
Divide the array in two parts A1[] and A2[].
Find minimum & maximum element in A1[] and LD(A1).
Find minimum & maximum element in A2[] and LD(A2).
LD(A) = max( LD(A1), LD(A2), MAX(A2) - MIN(A1) )
MAX(A) = max( MAX(A1), MAX(A2) )
MIN(A) = min( MIN(A1), MIN(A2) )
Base Case (length(A) == 2
):
If A[1] > A[0],
LD(A) = A[1] - A[0].
MAX(A) = A[1]
MIN(A) = A[0]
else
LD(A) = 0.
MAX(A) = A[0]
MIN(A) = A[1]
Note:
If (length(A) == 1)
LD(A) = 0
MIN(A) = MAX(A) = A[0]
Similarly you can calculate the min and max elements in each subarray.
回答3:
Below is the code in C using Divide and Conquer.
Plz. do feel free to comment.
#include <stdio.h>
struct data{
int min_;
int max_;
};
struct data crossminmax(int *p,int lo,int mid,int hi){
int i,min,max;
struct data temp;
min = p[mid];
for(i=mid;i>=lo;i--){
if(p[i] < min){
min = p[i];
}
}
max = p[mid+1];
for(i=mid+1;i<=hi;i++){
if(p[i] > max){
max = p[i];
}
}
temp.min_ = min;
temp.max_ = max;
return temp;
}
/* MinMax calculates the difference between Biggest and Smallest element *
* of an array using divide and conquer principles such that the position *
* of Biggest element is always greater than the Samllest element */
struct data minmax(int *p,int lo,int hi){
int mid,leftdiff,rightdiff,crossdiff;
struct data left,right,cross,temp;
if(lo == hi){
temp.min_ = p[lo];
temp.max_ = p[hi];
return temp;
}
mid = (lo+hi)/2;
left = minmax(p,lo,mid);
right = minmax(p,mid+1,hi);
cross = crossminmax(p,lo,mid,hi);
leftdiff = left.max_ - left.min_;
rightdiff = right.max_ - right.min_;
crossdiff = cross.max_ - cross.min_;
if(leftdiff > rightdiff && leftdiff > crossdiff){
return left;
}else if(rightdiff > crossdiff){
return right;
}else{
return cross;
}
}
int main(){
int arr[] = {5,2,3,10,1,3,16,4,3};
struct data dt;
dt = minmax(arr,0,8);
printf("Max difference = %d, Max Element=%d, Min Element = %d \n",dt.max_ - dt.min_,dt.max_,dt.min_);
return 0;
}
回答4:
largest differance in array
condition : - larger number should appear after smaller number
{ 10, 3, 6, 4, 8, 1, 7 } 6
{ 2, 3, 10, 6, 4, 8, 1 } 8
{ 7, 9, 5, 6, 3, 2 } 1
{ 1, 2, 3, 4 } 3
{ 4, 3, 2, 1 } 0
#include<stdio.h>
int main(){
int n = 7;
int arr[7] = { 10, 3, 6, 14, 8, 1, 7 };
int i;
int max_diff = 0;
int min = arr[0];
for( i=0; i<n; i++){
if( (arr[i] - min) > max_diff ){
max_diff = arr[i] - min;
}
if(arr[i] < min){
min = arr[i];
}
}
printf("max diff = %d", max_diff);
return 0;
}
//time complexity O(n)
//space complexity O(1)
for better understanding please visit https://www.youtube.com/watch?v=thPG6eTPf68&t=130s
来源:https://stackoverflow.com/questions/24055608/divide-and-conquer-algo-to-find-maximum-difference-between-two-ordered-elements