Inversion graphs
Practice
3.9 (7 votes)
Algorithms
C++
Dynamic programming
Problem
41% Success 2217 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given an array \(A\) with \(N\) elements. You create a graph \(G\) with \(N\) vertices using \(A\) as follows:

For any pair of indices \(( i,\ j )\) where \( 1 \le i < j \le N\), if \(A[i] > A[j]\), then add an undirected edge between vertices \(i\) and \(j\).

In graph \(G\), find the maximum possible size of set \(S\) (of vertices in G) such that there exists an edge between every pair of vertices that are available in \(S\).

Input format

  • The first line contains an integer \(N\) denoting the number of elements in \(A\).
  • The second line contains \(N\) space-separated integers denoting the elements of \(A\).

Output format

Among all the vertices available in \(S\), find the maximum possible size of \(S\).

Constraints
\(1 \le N \le 10^5\) 
\(1 \le A[i] \le 10^6\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
5 votes
Tags:
AlgorithmsDynamic ProgrammingMedium
Points:30
11 votes
Tags:
ApprovedData StructuresDynamic ProgrammingMediumOpen
Points:30
15 votes
Tags:
AlgorithmsDynamic ProgrammingHiringMedium