Skip to main content

Command Palette

Search for a command to run...

Find the Peaks

Published
1 min read
Find the Peaks
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

You are given a 0-indexed array mountain. Your task is to find all the peaks in the mountain array.

Return an array that consists of indices of peaks in the given array in any order*.*

Notes:

  • A peak is defined as an element that is strictly greater than its neighboring elements.

  • The first and last elements of the array are not a peak.

LeetCode Problem - 2951

class Solution {
    public List<Integer> findPeaks(int[] mountain) {
        // Initialize a list to store the indices of the peaks
        List<Integer> peak = new ArrayList<>();

        // Iterate through the mountain array starting from the second element to the second-to-last element
        for(int i = 1; i < mountain.length - 1; i++) {
            int currentPeak = mountain[i]; // Current element in the array

            // Check if the current element is greater than its neighbors (previous and next elements)
            if(currentPeak > mountain[i - 1] && currentPeak > mountain[i + 1]) {
                peak.add(i); // If true, add the index of the current element to the peak list
            }
        }

        // Return the list of peak indices
        return peak;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.