Join Regular Classroom : Visit ClassroomTech

Revature Coding Solution with proof | Array Operation: Ascending order | Codewindow.in

Array Operation: Ascending Order

You are given an array A of length N.
In one operation you can remove a number either from the start of the array or from the end of the array or from the
end of the array.

You have to find the number of operations to be performed, so the remaining elements will in ascending order.

Function Description
In the provided code snippet, implement the provided ascendingOrder(…) method using the variables to print the number of
operations to be performed so the remaining elements will in non-decreasing order. You can write your code in the space below
the phrase “WRITE YOUR LOGIC HERE”.
There will be multiple test cases running so the input and output should match exactly as provided.
The base output variable result is set to a default value of -404 which can be modified. Additionally, you can add or remove
these output variables.

Input Format
The first line of input contains one integer N.
The second line of input contains N integers of array A.

Sample Input
6 — denotes N
2 3 4 5 6 1 — denotes array A

Constraints
1<=N<=1000
1<=Ai<=100000

Output Format
The output contains the number of operations to be performed. So the remaining elements will in non-decreasing order.
Sample Output
1

Example 1

Input:
6

2 3 4 5 6 1

Output:
1

Solution: In C

#www.codewindow.in
# Ascending Order Solution
 
n=int(input())
h=list(map(int,input().split(' ')))
k=0
a=0
p=[]
for i in range(0,n):
 v=h[i]
 if h[i]==max(h):
   k=0
 else:
    for j in range(v+1,max(h)+1):
     if j in h:
      if j>v:
       k=j
       break
 m=h[i]
 if h[i]==min(h):
    a=0
 else:
    for s in range(0,h[i]):
     if s in h:
      if s<m:
       a=s
 p.append(a+k)
for i in range(0,n):
  print(p[i],end=' ')
 
         
#end

Also Checkout

Recent Posts
Categories
Pages