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 

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.

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                FUNCTION
    ---------               -----------------
        6                 stockPrices[]   size, n
        = 6            
        1                 stockPrices = [ 1, 2, 3, 3, 4, 5 ]
        2                
        3                
        3                
        4                
        5                    
        3                  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.

 Solution 

 Programming Language: Java Language 

import 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));
    }
}
// Published By PKJCODERS

 (Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.) 

Comments

Popular Posts

Flowchart Logic 047

Desktop Products

Words Starting with Upper Case

Find Maxima/Minima

Matrix Print Excluding Border Elements

Musical Bench