# Check if all array elements can be removed by the given operations

Given an array arr[] containing distinct elements, the task is to check if all array elements can be removed by the selecting any two adjacent indices such that arr[i] < arr[i+1] and removing one of the two elements or both at each step. If it is possible, then print “Yes”. Otherwise, print “No”.
Examples:

Input: arr[] = {2, 8, 6, 1, 3, 5}
Output: Yes
Explanation:
Select arr and arr. Since 3 < 5, remove 3, thus modifying the array arr[] = {2, 8, 6, 1, 5}
Select arr and arr. Since 2 < 8, remove 8, thus modifying the array arr[] = {2, 6, 1, 5}
Select arr and arr. Since 2 < 6, remove both 2 and 6, thus modifying the array arr[] = {1, 5}
Select arr and arr. Since 1 < 5, remove both 1 and 5, thus modifying the array arr[] = {}
Input: arr[] = {6, 5, 1}
Output: NO

Naive Approach:
The idea is to consider all adjacent pairs from the given array and if it satisfies the given condition then remove one of those numbers and repeat this process till all the elements are removed. If it is possible then print “Yes”. Otherwise print “No”
Time Complexity: O(N2
Auxiliary Space: O(1)
Efficient Approach:
As the elements of the array are distinct it can be observed that all elements of the array can be removed if the first element of the array is smaller than the last element of the array. Otherwise, all elements of the given array cannot be removed.
Follow the steps below to solve the problem:

• Check if arr < arr[n – 1], print “Yes”.
• Otherwise, print “No”.

Below is the implementation of the above approach:

## C++

 `// C++ program to implement  ` `// the above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to check if it is possible ` `// to remove all array elements ` `void` `removeAll(``int` `arr[], ``int` `n) ` `{ ` ` `  `    ``// Condition if we can remove ` `    ``// all elements from the array ` `    ``if` `(arr < arr[n - 1]) ` `        ``cout << ``"YES"``; ` `    ``else` `        ``cout << ``"NO"``; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` ` `  `    ``int` `Arr[] = { 10, 4, 7, 1, 3, 6 }; ` ` `  `    ``int` `size = ``sizeof``(Arr) / ``sizeof``(Arr); ` ` `  `    ``removeAll(Arr, size); ` `    ``return` `0; ` `}`

## Java

 `// Java program to implement  ` `// the above approach ` `class` `GFG{ ` `  `  `// Function to check if it is possible ` `// to remove all array elements ` `static` `void` `removeAll(``int` `arr[], ``int` `n) ` `{ ` `  `  `    ``// Condition if we can remove ` `    ``// all elements from the array ` `    ``if` `(arr[``0``] < arr[n - ``1``]) ` `        ``System.out.print(``"YES"``); ` `    ``else` `        ``System.out.print(``"NO"``); ` `} ` `  `  `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `Arr[] = { ``10``, ``4``, ``7``, ``1``, ``3``, ``6` `}; ` `  `  `    ``int` `size = Arr.length; ` `  `  `    ``removeAll(Arr, size); ` `} ` `} ` ` `  `// This code is contributed by Rohit_ranjan`

## Python3

 `# Python3 program to implement   ` `# the above approach  ` ` `  `# Function to check if it is possible  ` `# to remove all array elements  ` `def` `removeAll(arr, n): ` `   `  `  ``# Condition if we can remove  ` `  ``# all elements from the array  ` `  ``if` `arr[``0``] < arr[n ``-` `1``]: ` `    ``print``(``"YES"``) ` `  ``else``: ` `    ``print``(``"NO"``) ` `     `  `# Driver code ` `arr ``=` `[ ``10``, ``4``, ``7``, ``1``, ``3``, ``6` `] ` ` `  `removeAll(arr, ``len``(arr)) ` ` `  `# This code is contributed by dipesh99kumar`

## C#

 `// C# program to implement  ` `// the above approach ` `using` `System; ` ` `  `class` `GFG{ ` ` `  `// Function to check if it is possible ` `// to remove all array elements ` `static` `void` `removeAll(``int` `[]arr, ``int` `n) ` `{ ` ` `  `    ``// Condition if we can remove ` `    ``// all elements from the array ` `    ``if` `(arr < arr[n - 1]) ` `        ``Console.Write(``"YES"``); ` `    ``else` `        ``Console.Write(``"NO"``); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `[]Arr = { 10, 4, 7, 1, 3, 6 }; ` ` `  `    ``int` `size = Arr.Length; ` ` `  `    ``removeAll(Arr, size); ` `} ` `} ` ` `  `// This code is contributed by amal kumar choubey `

Output:

```NO
```

Time Complexity: O(1)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.