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.

Add Comment
3 Answer(s)

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.

Add Comment

So, the return value is an example of functional programming || is working as if condition.

return increasing || decreasing; 

abobe line is similar to

 if (increasing || decreasing)      return true;  else      return false 
Add Comment

it will return true if one of them or both is true. False if both are false.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.