File tree Expand file tree Collapse file tree
InterviewsPreparation/src/main/java/com/javaaid/ip/app_dynamics Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments