Skip to content

Commit 567d697

Browse files
committed
Added one more solution to app dynamics
1 parent c6f2826 commit 567d697

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

  • InterviewsPreparation/src/main/java/com/javaaid/ip/app_dynamics
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
*
3+
*/
4+
package com.javaaid.ip.app_dynamics;
5+
6+
import java.util.Scanner;
7+
8+
/**
9+
* @author Kanahaiya Gupta
10+
*
11+
*/
12+
public class Solution1 {
13+
public static int maxProfit(int[] a) {
14+
int max_so_far = 0, curr_max = 0;
15+
for (int i = 1; i < a.length; i++) {
16+
curr_max += a[i] - a[i - 1];
17+
curr_max = Math.max(curr_max, 0);
18+
max_so_far = Math.max(max_so_far, curr_max);
19+
}
20+
return max_so_far <= 0 ? -1 : max_so_far;
21+
}
22+
23+
public static void main(String[] args) {
24+
Scanner sc = new Scanner(System.in);
25+
int n = sc.nextInt();
26+
int a[] = new int[n];
27+
for (int i = 0; i < n; i++) {
28+
a[i] = sc.nextInt();
29+
}
30+
int result = maxProfit(a);
31+
System.out.println(result);
32+
}
33+
34+
}

0 commit comments

Comments
 (0)