QUESTION

Text
Image

Solve in java


3. Highly Profitable Months The stocks of a company are being surveyed to analyse the net profit of the company over a period of several months. For an analysis parameter $k$, a group of $k$ consecutive months is said to be highly profitable if the values of the stock prices are strictly increasing for those months. Given the stock prices of the company for $n$ months and the analysis parameter $k$, find the number of highly profitable months. Example Let the number of months be $n=8$, the stock prices be stockPrices $=[5,3,5,7,8]$ and the analysis parameter be $k=3$. Following are the groups of $k$ months in which the stock prices are strictly increasing: Hence the answer is 2. Function Description Complete the function countHighlyProfitableMonths in the editor below. countHighyProfitableMonths has the following parameters: int stockPrices[n]t the stock prices for $n$ months int $k$ : the analysis parameter Returns int: the number of highly profitable months Constraints - $1 \leq k \leq n \leq 2 \cdot 10^{5}$ - $1 \leq \operatorname{stockPrices[i]\leq 10^{9}}$ Input Format For Custom Testing Sample Case 0 Sample Input For Custom Testing $\begin{array}{lll}\text { STDIN } & & \text { FUNCTION } \\ -6 & \rightarrow & \text { stockPrices [ ] size, } n=6 \\ 6 & \rightarrow & \text { stockPrices }=[1,2,3,3,4,5] \\ 1 & & \\ 2 & & \\ 3 & & \\ 3 & & \\ 4 & & k=3 \\ 3 & \rightarrow & \end{array}$ STDIN FUNCTION $\rightarrow$ stockPrices[] size, $n=6$ $\rightarrow \quad$ stockPrices $=[1,2,3,3,4,5]$ $1 \rightarrow$ stockPrices $=[1,2,3,3,4,5]$ 3 4 $3 \rightarrow \mathrm{k}=3$ Sample Output 2 Explanation Following are the groups of $k$ months which are highly profitable: - Months 1 to $3:[1,2,3,3,4,5]$ - Months 4 to $6:[1,2,3,3,4,5]$ Hence the answer is 2.

Starter code

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

class Result {

/*
* Complete the 'countHighlyProfitableMonths' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER_ARRAY stockPrices
* 2. INTEGER k
*/

public static int countHighlyProfitableMonths(List<Integer> stockPrices, int k) {
}

}

public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));

int stockPricesCount = Integer.parseInt(bufferedReader.readLine().trim());

List<Integer> stockPrices = new ArrayList<>();

for (int i = 0; i < stockPricesCount; i++) {
int stockPricesItem = Integer.parseInt(bufferedReader.readLine().trim());
stockPrices.add(stockPricesItem);
}

int k = Integer.parseInt(bufferedReader.readLine().trim());

int result = Result.countHighlyProfitableMonths(stockPrices, k);

bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();

bufferedReader.close();
bufferedWriter.close();
}
}

Starter input:

EXAMPLE 1:

8
7
1
2
3
6
9
12
3
2

EXAMPLE 2:

4
1
2
3
4
4

EXAMPLE 3:

6
1
2
3
3
4
5
3

Public Answer

TNLZQS The First Answerer