Member-only story

How to Solve Longest Mountain in Array Problem

Java Solution for Leetcode 845 for finding Longest Mountain

Suraj Mishra
3 min readNov 1, 2022

Originally Published in https://asyncq.com/

Photo by Mark Basarab on Unsplash

Introduction

  • In this article, we will solve Leetcode 845, which is based on Two Pointer Solution.
  • So we will see how Two pointer algorithm works.

Problem Statement

  • We have been given an array and there may or may not mountain exists.
  • A mountain is defined such that,
    arr[0] < arr[1] < arr[i-1] <arr[i]
    arr[i] > arr[i+1] > arr[i+2] …> arr[arr.length — 1]
  • We need to return the longest mountain in the array. if we cannot find the mountain then we return 0.

Examples

Solutions

Intitution

  • Our mountain has a special property where all the elements to the left are smaller than it and all the elements on the right side are smaller as well.
  • So at first, we can find such an element as the Peak element.

--

--

Suraj Mishra
Suraj Mishra

Written by Suraj Mishra

Staff Software Engineer @PayPal ( All opinions are my own and not of my employer )

No responses yet