All Tracks Data Structures Stacks Basics of Stacks Problem

Little Shino and Pairs
/

Data Structures, Stack, Stack

Problem
Editorial
Analytics

Given a permutation of number from 1 to N. Among all the subarrays, find the number of unique pairs \((a, b)\) such that \(a \neq b\) and a is maximum and b is second maximum in that subarray.

Input:
First line contains an integer, N \((1 \le N \le 10^5)\). Second line contains N space separated distinct integers, \(A_i\) \((1 \le A_i \le N)\), denoting the permutation.

Output:
Print the required answer.

SAMPLE INPUT
5
1 2 3 4 5
SAMPLE OUTPUT
4
Explanation

All the possible subarrays are:

1
\(1\; 2\)
\(1\; 2\; 3\)
\(1\; 2\; 3\; 4\)
\(1\; 2\; 3\; 4\; 5\)
2
\(2\; 3\)
\(2\; 3\; 4\)
\(2\; 3\; 4\; 5\)
3
\(3\; 4\)
\(3\; 4\; 5\)
4
\(4\; 5\)
5

The 4 unique pairs are:
\((2,\; 1)\)
\((3,\; 2)\)
\((4,\; 3)\)
\((5,\; 4)\)

Time Limit: 1.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB

Best Submission

Similar Problems

Contributors

This Problem was Asked in

Initializing Code Editor...
Notifications
View All Notifications

?