Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are given an array 'A' of 'N' integers. The beauty of the array is defined as the difference between the maximum and minimum elements

You are given an array 'A' of 'N' integers.

The beauty of the array is defined as the difference between the maximum and minimum elements of the array.

Your task is to return the maximum number of elements you can remove from the back of the array such that the beauty of the array remains unchanged.

Note: that you cannot remove all the elements from the array, as beauty is not defined for an empty array.

Example:

N=4

A = [6, 2, 4, 3]

The initial beauty of the array is '6-2=4'.

We can remove the last two elements from the array, so the resultant array is [6, 2], which also has beauty equal to '6-2 = 4'.

We can see that we can not remove more than two elements from the array without affecting the beauty of the array, Therefore, the answer for this case is 2.

Detailed explanation (Input/output format, Notes, Images) The first line of each test case contains an integer 'N', denoting the length of the array 'A'.

The second line of each test case contains 'N' space-separated integers, denoting the elements of the array 'A'.

Output format:

Return the maximum number of elements you can remove from the back of the array such that the beauty of the array doesn't change.

Note:

You don't need to print anything. It has already been taken care of. Just implement the given function. Sample input 1:

2 3 2 2 2 2 1 4 Sample output 1:

2 0

Explanation of sample input 1:

For the test case 1:

The initial beauty of the array is '2-2' = '0'.

Here, we can remove the last two elements from the array, so the resultant array is [2], which also has beauty equal to '0'.

We can see that we can only remove up to two elements from the array, as we need at least one element in the array. Therefore, the answer for this case is '2'.

For the test case 2:

The initial beauty of the array is '41' = '3'. ='3'.

If we remove the last element, we will have the array with only one element whose beauty is '0'. So, we can not remove any elements from the array 'A'. So, the answer for this case is '0'.

def maximumRemovals(n,arr):

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases DeMYSTiFieD

Authors: Andy Oppel

2nd Edition

0071747990, 978-0071747998

More Books

Students also viewed these Databases questions

Question

=+3. Mechanisms for contingency management.

Answered: 1 week ago

Question

Explain the focus of safety programs.

Answered: 1 week ago

Question

Describe the consequences of musculoskeletal disorders.

Answered: 1 week ago