Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Yet Another Array Problem! On the occasion of Independence Day, Punjab Engineering College decides to conduct a Flag Hoisting Ceremony and decides to give Flag

Yet Another Array Problem!

On the occasion of Independence Day, Punjab Engineering College decides to conduct a Flag Hoisting Ceremony and decides to give Flag Hoisting opportunity to one of the students. So, they decide to give students an interesting array problem and the student which comes up with the best solution fastest gets the golden opportunity.

Formally, the problem is described below.

You are given an array A with N integers. You can remove an element either from the front or end of the array A and append it to another array B (initially B is empty). You have to simulate this process till there is atleast one element left in A . The task seems pretty easy but isn't. You have to find is there any way to perform the above steps in any order such that the we get B sorted in non-decreasing fashion.

Akshit doesn't want to miss this golden opportunity but since he is a noob in programming, he asks your help to solve this problem for him. Can you solve it for him?

Input Format:

  • The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
  • The first line of each test case contains a single integer N, the number of elements present in the array A.
  • The second line contains N space-separated integers A1,A2,...,AN.

Output Format:

For each test case, print a single line containing a string "YES" if there is a way to perform the above steps in any order such that the we get B sorted in non-decreasing fashion else print "NO".

Constraints:

1T105

1N106

1Ai104

Sum of N over all test cases doesn't exceed 3106

SAMPLE INPUT

3

3

1 2 5

5

1 2 2 3 1

7

3 5 7 2 1 5 3

SAMPLE OUTPUT

YES

YES

NO

Explanation

Testcase 2:

Initial Configuration of A and B.

A=[1,2,2,3,1]B=[]

Steps:

Remove A[0] and append it to B.A=[2,2,3,1]B=[1]

Remove A[3] and append it to B.A=[2,2,3]B=[1,1]

Remove A[0] and append it to B.A=[2,3]B=[1,1,2]

Remove A[0] and append it to B.A=[3]B=[1,1,2,2]

Remove A[0] and append it to B.A=[2,3]B=[1,1,2,2,3]

We get B as desired.

You are expected to think about testcases 1 and 3 on your own.

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

Step: 3

blur-text-image

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

=+Provide a brief summary of those criticisms.

Answered: 1 week ago