Maximum profit by buying and selling a share at most twice |  ; adding array ; 80 / 30 / / \ 25 / 15 / / \ / 2 10 / \ / 8 ; traversing through array from ( i + 1 ) th position
import java . io . * ; import java . util . * ; class GFG { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int price [ ] = { 2 , 30 , 15 , 10 , 8 , 25 , 80 } ; int profit = 0 ; for ( int i = 1 ; i < price . length ; i ++ ) { int sub = price [ i ] - price [ i - 1 ] ; if ( sub > 0 ) profit += sub ; } System . out . print ( " Maximum ▁ Profit = " + profit ) ; } }