How to find if an Array can be converted to Max Array (all elements equal to max value) constraints-
I was asked this question in an coding test for an javascript interview. But cannot quite solve it or find similar pattern on internet.
Given any array of natural numbers, for eg: [2, 1, 2, 3]
Find if array can be converted into Max array (print- "YES") or if not (print - "NO")
to make it Max array - convert every element of array equal to it's maximum element. In above eg it will be [3, 3, 3, 3] but by following these rules -
- Increment any two elements by 1 at a time (exactly 2 elements. you cannot increment one or more than two elements at a time)
- Do this multiple times until you convert every element equal to max elements (print "YES" if possible else "NO")
Sample input:
[2, 1, 2, 3]
Expected Output:
"YES"
Explanation:
Step 1: increment first and second element by 1
[3, 2, 2, 3]
Step 2: increment second and third element by 1
[3, 3, 3, 3]
Can anyone point to the solution - any link, similar question, pattern or solution? Thank you