Highly Profitable Months
Coding Problem Keys
Highly Profitable Months
Problem Statement
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
Following are the groups of k months in which the stock prices are strictly increasing:
Hence the answer is 2.
Function Description
countHighlyProfitableMonths has the following parameters:
int stockPrices[n]: the stock prices for n months.
int k: the analysis parameter.
Returns
int: the number of highly profitable months.Constraints
- 1 ≤ k ≤ n ≤ 2 · 10⁵
- 1 ≤ stockPrices[i] ≤ 10⁹
Input Format For Custom Testing
Sample Input For Custom testing
STDIN FUNCTIONSample Output
2Explanation
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 ]
Solution
Programming Language: Java Language
// Published By PKJCODERSimport java.util.Scanner; public class Main{ static int countHighlyProfitableMonths(int n,int stockprices[],int k){ int count=0,sum=0; boolean flag=false; for(int i=0;i<=n-k;i++){ sum=0; for(int j=i;j<(i+k)-1;j++){ if(stockprices[j]<stockprices[j+1]) sum++; } if(sum==k-1) count++; }return count; } public static void main(String args[]){ int n,k; Scanner ip=new Scanner(System.in); n=ip.nextInt(); int stockprices[]=new int[n]; for(int i=0;i<n;i++) stockprices[i]=ip.nextInt(); k=ip.nextInt(); System.out.println(countHighlyProfitableMonths(n,stockprices,k)); } }
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments