QualityKiosk | Onsite | Unique Plants | Easy
Anonymous User
6723

Searching Algorithm: Unique Plants

There are N plants kept in a row in the nursery, each having a height of hi units.
A plant is considered to be unique if the height of that plant is strictly less than the height of its neighboring plants.
Find the total number of unique plants in the nursery.

Function Description
In the provided code snippet, implement the provided uniquePlants(...) method using the variables to print the total number of unique plants. You can write your code in the space below the phrase “WRITE YOUR LOGIC HERE”.

There will be multiple test cases running so the Input and Output should match exactly as provided.
The base Output variable result is set to a default value of -404 which can be modified. Additionally, you can add or remove these output variables.

Input Format
The first line of input contains an integer N.
The next line of input contains N space-separated integers denoting the height of the plants.

Sample Input

4                            --denotes N
12 10 15 9             --denotes the height of the plants

Constraints

2 <= N <= 100
1 <= Hi <= 109

Output Format
The output contains an integer denoting the total number of unique plants.

Sample Output

2

Explanation
The 2nd plant is unique as its height is strictly lesser than its neighbors (10 < 12 and 10 < 15).
Similarly, the 4th plant is unique (9 < 15).
Hence, there are 2 unique plants.

Code Snippet:

import java.util.*;
import java.io.*;
import java.lang.Math;

public class Main
{
	public static int uniquePlants(int N, int[] H)
	{
	// this is default OUTPUT. You can change it.
	int result = -404;
	
	// write your Logic here:
	
	return result;
	}
	public static void main (String[]args)
	{
	Scanner sc = new Scanner (System.in);
	
	// INPUT [uncomment & modify if required]
	int N = sc.nextInt();
	
	int[] H = new int[N];
	for (int i =0; i <N ; i++)
	{
		H[i] = sc.nextInt();
 	}
	
	sc.close();
	
	//OUTPUT [uncomment & modify if required]
	System.out.print(uniquePlants(N,H));
	}
}

Output:

Case 1:

Input
22
672 679 100 283 351 552 985 858 661 550 709 447 961 235 735 8 304 390 684 340 654 94
Expected Output
8

Case 2:

Input
100
6990 6304 738 8705 6792 2219 1120 2707 9143 11850 20993 32843 53836 86679 140515 227194 367709 594903 962612 1557515 2520127 4077642 6597769 10675411 17273180 27948591 45221771 73170362 118392133 191562495 309954628 501517123 811471751 8298 454 6727 159 7296 6792 6543 6425 8263 5400 603 5191 5637 6530 8676 8056 9864 1628 8446 7627 9144 2019 3915 8027 5887 7325 195 6184 7122 8653 8555 6907 5144 3152 9454 4291 4669 1176 1285 1089 1725 6252 962 8306 9071 9776 3277 9326 8295 3058 5090 5905 5876 6383 33 33 33 33 33 33 33 33 33 33 33 33 33
Expected Output
20

Case 3:

Input
4 
12 10 15 9
Expected Output
2
Comments (2)