Minimum Operations in Adjacent Elements
Coding Problem Keys
Minimum Operations in Adjacent Elements
Problem Statement
You are given an array A of N distinct numbers. In one operation, you can choose two adjacent elements and remove the minimum one.
Find the minimum number of operations needed to make the array in increasing order.
Input Format
The first line contains an integer, N, denoting the number of elements in A.
Each line i of the N subsequent lines (where 0 <= i <= N) contains an integer describing A[i].
Output Format
The output prints the minimum number of operations needed to make the array to be in increasing order.
Constraints
1 <= N <= 10⁵
1 <= A[i] <= 10⁹
Example Input/Output 1
Input
2
8
10
Output
0
Explanation
We don't have to remove any element as it is already in increasing order.
Example Input/Output 2
Input
3
10
3
4
Output
2
Note
We can remove the second and the third element.
Example Input/Output 3
Input
4
5
4
8
9
Output
1
Note
We can remove the second element.
Solution
Programming Language: Python 3 Language
def Minimum_operations(N,A):
B=[None]*N j=0;count=0 for i in range(N): if i==0: B[j]=A[i] else: if(A[i]>B[j]): j+=1 B[j]=A[i] else: count+=1 return count # Published By PKJCODERS
def main(): N=int(input()) A=[None]*N for j in range(N): A[j] = int(input()) result = Minimum_operations(N,A); print(result) if __name__=="__main__": main()
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments