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:YesExplanation:

Select arr[4] and arr[5]. Since 3 < 5, remove 3, thus modifying the array arr[] = {2, 8, 6, 1, 5}

Select arr[0] and arr[1]. Since 2 < 8, remove 8, thus modifying the array arr[] = {2, 6, 1, 5}

Select arr[0] and arr[1]. Since 2 < 6, remove both 2 and 6, thus modifying the array arr[] = {1, 5}

Select arr[0] and arr[1]. 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(N^{2}) **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[0] < 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<bits/stdc++.h> ` `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[0] < 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[0]); ` ` ` ` ` `removeAll(Arr, size); ` ` ` `return` `0; ` `}` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

## 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[0] < 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 ` |

*chevron_right*

*filter_none*

**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.

## Recommended Posts:

- Minimum elements to be removed such that sum of adjacent elements is always odd
- Check if sum Y can be obtained from the Array by the given operations
- Minimum number of elements that should be removed to make the array good
- Check if quantities of 3 distinct colors can be converted to a single color by given merge-pair operations
- Maximum sum of all elements of array after performing given operations
- Make all the array elements odd with minimum operations of given type
- Make all the elements of array even with given operations
- Check if all elements of a Circular Array can be made equal by increments of adjacent pairs
- Minimum Circles needed to be removed so that all remaining circles are non intersecting
- Minimum digits to be removed to make either all digits or alternating digits same
- Check if elements of an array can be arranged satisfying the given condition
- Check if an Array can be Sorted by picking only the corner Array elements
- Minimum size substring to be removed to make a given string palindromic
- Minimum number of operations required to delete all elements of the array
- Minimum operations to make all elements equal using the second array
- Minimum count of elements required to obtain the given Array by repeated mirror operations
- Check if elements of array can be arranged in AP, GP or HP
- Length of the Smallest Subarray that must be removed in order to Maximise the GCD
- Maximize the Sum of the given array using given operations
- Minimum number of steps required to obtain the given Array by the given operations

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.