Close
Topic list
Hills and Soldier
MEDIUM
30 mins
Stacks & Queues
Topics (Covered in this problem)
Problem solved
Skill meter
Stacks & Queues
-
-
Other topics
Problem solved
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
-
-
Sorting
-
-
Binary Search
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

# Hills and Soldier

Contributed by
Medium
0/80
Avg time to solve 30 mins
Success Rate 67 %
Share

## Problem Statement

#### For a pair, (a, b) is same as (b, a).

##### Example:
``````Input: ‘N’ = 4, ‘HILLS’ = [3, 2, 1, 3]
Output: 5
If we consider the indices from 0 to 3 then the pairs are (0, 1), (0,3), (1, 2), (1,3), and (2, 3).
The soldier at hill 0 can not see hill 2 as hill[1]>hill[2].
``````

#### Note : Test cases are made in such a way that the answer will fit in 32-bit integer.

Detailed explanation ( Input/output format, Notes, Constraints, Images )
##### Sample Input 1 :
``````2
4
3 2 1 3
5
1 2 3 4 5
``````
##### Sample Output 1 :
``````5
4
``````
##### Explanation Of Sample Input 1 :
``````For the first test case:-
If we consider the indices from 0 to 3 then the pairs are (0, 1), (0,3), (1, 2), (1,3), and (2, 3).
The soldier at hill 0 can not see hill 2 as hill[1]>hill[2].

For the second test case:-
Only consecutive hill soldiers can see each other for others there is a higher hill in between them.
``````
##### Sample Input 2 :
``````2
5
3 2 1 3 5
6
2 2 1 3 4 6
``````
##### Sample Output 2 :
``````7
7
``````
Auto
Console