An array is monotonic if it is either monotone increasing or monotone decreasing

Tags: , ,



class Solution {

    public boolean isMonotonic(int[] A) 
    {
        
        boolean increasing = true;
        boolean decreasing = true;
        
        for (int i = 0; i < A.length - 1; ++i) 
        {
            
            if (A[i] > A[i+1])
                increasing = false;
            
            if (A[i] < A[i+1])
                decreasing = false;

        }

        return increasing || decreasing;

     }

}

Can anyone please explain how the return value is working.

Answer

increasing || decreasing means increasing OR decreasing. If either variable is true then the whole method will return true, otherwise it will return false.

|| is the logical OR operator.



Source: stackoverflow