- public class MaxProfit {
- public static void main(final String[] args) {
- int a[] = { 4, 1, 10, 2, 3, 5, 6, 25, 48, 8, 26 };
- int max = a[a.length - 1];
- int min = a[a.length - 1];
- int indexOfMax = a.length - 1;
- int indexOfMin = a.length - 1;
- int dif = -1;
- for (int i = a.length - 2; i >= 0; i--) {
- if (max < a[i]) {
- max = a[i];
- indexOfMax = i;
- }
- if (max > a[i]) {
- min = a[i];
- indexOfMin = i;
- }
- if ((indexOfMax > indexOfMin)) {
- if (dif < (max - min)) {
- dif = max - min;
- }
- }
- }
- System.out.println(dif);
- }
- }
2018-04-24
Write a program to find maximum profit
Subscribe to:
Post Comments (Atom)
Hi Geeks !!
ReplyDeleteGreat job you have done here. This is one of the top most interview question asked to me by an interviewer. I hope this will help to other geeks as well who give an interview.