Skip to content
Advertisement

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

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.

Advertisement

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement