-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmindiff.cpp
More file actions
30 lines (26 loc) · 722 Bytes
/
Copy pathmindiff.cpp
File metadata and controls
30 lines (26 loc) · 722 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// C++ program given an array find max(aj-ai) where j>i
//*******CAUTION: NOT TO SORT******
// j is selling day and i is buying day , when to buy and when to sell to get the maximum profit
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a[] = { 15, 8, 19, 1, 4};
int n = sizeof(a)/sizeof(a[0]);
int i=0; // assuming smallest value to be in i
int j=1;
int minj=j;
int m=a[j]-a[i];
j++;
while(j<n)
{
if(a[i]>a[j-1])
i=j-1; //this will maintain i to be the smallest among the traversed j
if(a[j]-a[i]>m)
{m=a[j]-a[i]; //finding the maximum difference
minj=j;
}
j++;
}
cout<<"the max diff for j>i is with "<<a[j]<<" and "<<a[i]<<" which is "<<m<<endl;
}