Skip to content

Commit

Permalink
Create Stock Buy and Sell – Max one Transaction Allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
dishathakurata authored Nov 22, 2024
1 parent 2c56710 commit f569293
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Stock Buy and Sell – Max one Transaction Allowed
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;
}
}

0 comments on commit f569293

Please sign in to comment.