Find maximum element in array of alternate sequences

Problem Statement

Given an array of elements which is first increasing and then decreasing, find maximum element in array of alternate sequences. Here is how an alternate sequence may look like :

  1. { 1, 3, 4, 7, 9, 6, 4, 2, 1 }

Test Cases

Here are few test scenarios which we must take into account before jumping into the code.

  1. input array is null
  2. input is empty array
  3. input has 1 element
  4. input has 2 element – increasing sequence.
  5. input has 2 element – decreasing sequence.
  6. input has N elements with proper increasing-decreasing sequence
  7. input has increasing sequence
  8. input has decreasing sequence

Approach to find maximum element in array of alternate sequences

Few things which must be a part of the solution

  1. Check for null or empty array input and return say an error code like -1
  2. Traverse through the array till the last but one element and check if a decreasing sequence is found at an index i.
    1. If found then return element at index i
  3. If loop ends and we have yet not returned, just return the element at the last index of the array

Source Code

Please suggest more test cases, so that it can help everyone who is preparing for an interview…

Stay tuned and Stay Subscribed