Minimum steps
Tag(s):

## Algorithms, Binary search algorithm, Data Structures, Dynamic Programming, Introduction to Dynamic Programming 1, Medium, open

Problem
Editorial
Analytics

You are given an array Arr that contains N integers. In one step, you can pick an element from position p and place it before or after some other elements. For example, if you are given an array  $Arr[]$={$1,3,2$}, then you can pick $3$ and place it after $2$. Therefore, the updated array is  $Arr[]$={$1,2,3$}.

Your task is to determine the minimum number of steps that is required to sort the data in increasing or decreasing order.

Input format

• First line: A single integer $N$ denoting the size of array $Arr$
• Second line: $N$ space-separated integers, where the $i^{th}$ integer denotes $Arr[i]$

Output format

Print a single integer value that denotes the minimum number of steps that is required to sort the data in increasing or decreasing order.

Constraints

$1 \le N \le 5 \times 10^5$

$1 \le Arr[i] \le 10^9$

SAMPLE INPUT
3
1 3 2
SAMPLE OUTPUT
1
Explanation

Given Arr[]={1,3,2} . For increasing order sorted data Arr will be {1,2,3}, steps required is 1. For decreasing order sorted data Arr will be {3,2,1}, steps required is 1. So minimum steps are 1.

Time Limit: 1.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: Bash, C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Swift-4.1, TypeScript, Visual Basic

## CODE EDITOR

Initializing Code Editor...

## This Problem was Asked in

Challenge Name

Java Easy : Mock Online Coding Assessments

OTHER PROBLEMS OF THIS CHALLENGE
• Algorithms > Dynamic Programming
• Algorithms > Dynamic Programming
• Algorithms > Searching
• Algorithms > Searching
• Math > Combinatorics
• Algorithms > Graphs
• Algorithms > Sorting
• Algorithms > String Algorithms
• Algorithms > Searching
• Algorithms > String Algorithms
• Algorithms > Dynamic Programming
• Algorithms > Graphs
• Algorithms > Dynamic Programming
• Algorithms > Greedy Algorithms
• Math > Combinatorics
• Basic Programming > Implementation
• Algorithms > Dynamic Programming