GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

How to solve this problem from codechef?

Chef has given you N numbers in an array A and asks you if the array is beautiful or not. 
An array is said to be beautiful if there exists at least one pair (i, j) such that 
i < j and A[i] <= A[j] 

If the array is beautiful then print “YES”, otherwise “NO”.

asked Mar 19 by anonymous

2 Answers

Consider an array which is sorted. {1,2,3,4}. There always exists i,j pair such that i<j and A[i]<=A[j].

If , on the other hand the array is reverse sorted, {4,3,2,1} , there is no such i,j pair. 

Now,If the array is not reverse sorted, then by definition,there must exist some pair i,j such that i<j and A[i]<=A[j], because if there exists no such pair, the array must be reverse sorted.

Hence, this problem reduces to find if the array is reverse sorted or not. If the array is reverse sorted, print NO else print YES

answered Mar 20 by nikhil chakravartula

Thank you for the answer. This is what I need. You helped me solve my problem.

Only case when this will not happen is when the array is a sorted array in decreasing order.

In all other cases atleast one such case will exist where A[i] <  A[j] where i < j

answered Mar 21 by mm_learner