Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

candies price hackerearth solution in java

import java.util.Scanner;

class AliceAndCandy {
	public static void main(String args[]) throws Exception {
		Scanner scan = new Scanner(System.in);
		int length = scan.nextInt();

		int[] children = new int[length];
		int[] candies = new int[length];

		// seed
		children[0] = scan.nextInt();
		candies[0] = 1;

		// search forward sequences
		for (int i = 1; i < length; i++) {
			children[i] = scan.nextInt();
			candies[i] = 1;
			if (children[i] > children[i - 1])
				candies[i] = candies[i - 1] + 1;
		}

		// search reverse sequences
		for (int i = length - 1; i > 0; i--) {
			if (children[i] < children[i - 1])
				candies[i - 1] = Math.max(candies[i - 1], candies[i] + 1);
		}

		long sum = 0;
		for (int i = 0; i < candies.length; i++) {
			sum += candies[i];
		}

		System.out.println(sum);
		scan.close();
	}
}
Comment

PREVIOUS NEXT
Code Example
Java :: trivers json node as node type2 
Java :: snapshot for loop android java 
Java :: Access Members of a Class Java 
Java :: determine the distance between two circles in java 
Java :: Reason: Failed to load driver class com.mysql.cj.jdbc.Driver in either of HikariConfig class loader or Thread context classloader 
Java :: replace range from array java 
Java :: spring converter in initbinder 
Java :: how to add classpath in spring boot 
Java :: display pop up window overlay permission in redmi android studio 
Java :: java default constructor 
Java :: scroll 
Java :: query spring boot 
Java :: findview by id in android 
Java :: Jlabel icon 
Java :: instanceof operator java 
Java :: Calculate weeks from date using java 
Java :: how to draw a circle in java swing 
Java :: button event 
Java :: convert array to arraylist 
Java :: java icon to image 
Java :: $950 at 6% per annum for three years. 
Java :: devoluciones redsys api 
Sql :: mysql show users 
Sql :: cannot truncate a table referenced in a foreign key constraint 
Sql :: mysql alter column default 
Sql :: oracle get current schema 
Sql :: ci last query 
Sql :: how to list columns for particular tables in postgresql 
Sql :: how to install postgresql on wsl 
Sql :: oracle rename table 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =