During this COVID19 outbreak, the gold price are very much unstable. Raja Indraverma is very much tensed. He has asked Bheem to analyse the gold price rate. Now Bheem is given a report of N consicutive days contaning gold price of those N consicutive days. Bheem has to check for how many days (including the ith day) the ith price was greater than or equal to its previous consicutive days. He will report the final result for each given N days.
Example:
Input: N=5, arr[] = { 10, 5, 4, 15, 25 }
Output: 1 1 1 4 5
Approach
Java
import java.io.IOException;import java.util.Deque;import java.util.LinkedList;public class FinacialProblemDholokpur {public static void main(String[] argv) throws IOException {int N = 5;Deque<Data> stack = new LinkedList<>();StringBuilder sb = new StringBuilder();int arr[] = { 10, 5, 4, 15, 25 };for (int i = 0; i < N; i++) {int p = arr[i];Data d = stack.peekFirst();int c = 1;while (d != null && p >= d.max) {d = stack.pollFirst();c = c + d.count;d = stack.peekFirst();}sb.append(c + " ");stack.push(new Data(p, c));}System.out.println(sb.toString());}private static class Data {int max;int count;public Data(int max, int c) {this.max = max;this.count = c;}}}
No comments:
Post a Comment