HackerRank : NSE ISB CodeSprint – CrossOver

By | March 29, 2017
CrossOver : Hackerrank

This problem is all about share prices and moving averages!

You are given n integers p1, p2, p3, p4 , where pi  denotes the price of the stock at day  i .

Then, we define:

  • STMA(i):= the average price of the stock over days i, i-1, i-2……. i-59 , so it is an average measured at day i over the most recent 60 days. This value is called Short term moving average.
  • LTMA(i):= the average price of the stock over days i, i-1, i-2……. i-299, so it is an average measured at day over the most recent 300 days. This value is called Long term moving average.

Notice that STMA(i)  is defined for 60<=i<=n, while LTMA(i)  is defined for  300<=i<=n.

In this problem, values of  STMA(i) and LTMA(i)  are rounded to exactly 2 decimal points, so for example 44.3449  is rounded to 44.34.

Now, let’s say that a crossover occurs at day if and only if both and are defined at days and , and either one of the below conditions is fulfilled:

CrossOver
Given the stock prices across days, detect all crossovers. For each crossover, print the day at which it occurred along with the values of STMA and LTMA for this day.

Input Format

The first line of input contains a single integer (the number of days).

The second line of input contains space-separated integers denoting the stock prices for each day.

Output Format

For each occurrence of a crossover, print three space-separated values for the date at which it occurred, the value of for this date, and the value of for this date. Print each crossover on its own line and order the crossovers in order from the smallest date to the largest. The values of both and must be correct to decimal places.

Solution

The solution is working fine in my local system, but few test cases are having Runtime 
error when I submit it. 
Hint for the question: 

I have here created two arrays, each one for LTMA and STMA.
Then in final array, i am checking conditions based on the LTMA[i] and STMA[i] value.

package nSEISBCodeSprint;

import java.util.Scanner;

public class CrossOver {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int p[] = new int[n];

		for (int i = 0; i < n; i++)
			p[i] = scan.nextInt();

		final int LTMALIMIT = 300;// 5;
		final int STMALIMIT = 60;// 2;

		int initialLTMASum = 0;

		for (int k = 0; k < LTMALIMIT; k++)
			initialLTMASum = initialLTMASum + p[k];

		int LTMAArr[] = new int[LTMALIMIT + 1];
		int firstIndex = 0;
		int lastIndex = n - (LTMALIMIT + 1);
		int referenceLastIndex = lastIndex;

		for (int j = 0; j < (n - (LTMALIMIT - 1)); j++) {
			if (j == 0)
				LTMAArr[j] = initialLTMASum;
			else {
				int temp = initialLTMASum - p[firstIndex - 1];
				LTMAArr[j] = temp + p[lastIndex];
				initialLTMASum = LTMAArr[j];
			}
			firstIndex++;
			lastIndex++;
		}

		int STMAArr[] = new int[LTMALIMIT + 1];
		int firstSTMAindex = referenceLastIndex - (STMALIMIT - 1);
		int lastSTMAindex = referenceLastIndex;

		/*
		 * System.out.println("p[firstSTMAindex] "+ p[firstSTMAindex]);
		 * System.out.println("p[lastSTMAindex] "+ p[lastSTMAindex]);
		 */
		int initialSTMASum = 0;

		for (int k = firstSTMAindex; k <= lastSTMAindex; k++)
			initialSTMASum = initialSTMASum + p[k];

		for (int m = 0; m < (n - (LTMALIMIT - 1)); m++) {
			if (m == 0)
				STMAArr[m] = initialSTMASum;
			else {
				int temp = initialSTMASum - p[firstSTMAindex - 1];
				STMAArr[m] = temp + p[lastSTMAindex];
				initialSTMASum = STMAArr[m];
			}
			firstSTMAindex++;
			lastSTMAindex++;

		}
		int index = LTMALIMIT;

		for (int g = 1; g < (n - (LTMALIMIT - 1)); g++) {
			index++;
			double Ltma = (double) LTMAArr[g] / LTMALIMIT;
			double Stma = (double) STMAArr[g] / STMALIMIT;

			double prevLtma = (double) LTMAArr[g - 1] / LTMALIMIT;
			double prevStma = (double) STMAArr[g - 1] / STMALIMIT;

			if ((Stma <= Ltma && prevStma > prevLtma) || (Stma >= Ltma && prevStma < prevLtma)
					|| (Stma != Ltma && prevStma == prevLtma)) {
				System.out.println(index + " " + Stma + " " + Ltma);
			}
		}

	}

}