-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Stock Buy and Sell – Max one Transaction Allowed
- Loading branch information
1 parent
2c56710
commit f569293
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//Stock Buy and Sell – Max one Transaction Allowed | ||
|
||
import java.io.*; | ||
import java.lang.*; | ||
import java.util.*; | ||
|
||
class GFG { | ||
public static void main(String args[]) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
int t = Integer.parseInt(br.readLine()); | ||
|
||
while(t-- > 0) { | ||
String arr[] = br.readLine().split(" "); | ||
int prices[] = new int[arr.length]; | ||
|
||
for(int i = 0; i < arr.length; i++) { | ||
prices[i] = Integer.parseInt(arr[i]); | ||
} | ||
|
||
Solution obj = new Solution(); | ||
int res = obj.maximumProfit(prices); | ||
System.out.println(res); | ||
} | ||
} | ||
} | ||
|
||
class Solution { | ||
public int maximumProfit(int prices[]) { | ||
int profit = 0; | ||
int minPrice = prices[0]; | ||
|
||
for(int i = 0; i < prices.length ; i++) { | ||
minPrice = Math.min(prices[i], minPrice); | ||
profit = Math.max(profit, prices[i] - minPrice); | ||
} | ||
|
||
return profit; | ||
} | ||
} |