candies price hackerearth solution in java

Solutions on MaxInterview for candies price hackerearth solution in java by the best coders in the world

showing results for - "candies price hackerearth solution in java"
Alonzo
26 Apr 2016
1import java.util.Scanner;
2
3class AliceAndCandy {
4	public static void main(String args[]) throws Exception {
5		Scanner scan = new Scanner(System.in);
6		int length = scan.nextInt();
7
8		int[] children = new int[length];
9		int[] candies = new int[length];
10
11		// seed
12		children[0] = scan.nextInt();
13		candies[0] = 1;
14
15		// search forward sequences
16		for (int i = 1; i < length; i++) {
17			children[i] = scan.nextInt();
18			candies[i] = 1;
19			if (children[i] > children[i - 1])
20				candies[i] = candies[i - 1] + 1;
21		}
22
23		// search reverse sequences
24		for (int i = length - 1; i > 0; i--) {
25			if (children[i] < children[i - 1])
26				candies[i - 1] = Math.max(candies[i - 1], candies[i] + 1);
27		}
28
29		long sum = 0;
30		for (int i = 0; i < candies.length; i++) {
31			sum += candies[i];
32		}
33
34		System.out.println(sum);
35		scan.close();
36	}
37}
38
similar questions
queries leading to this page
candies price hackerearth solution in java