Find Peak Element

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.
A peak element is an element that is strictly greater than its neighbors.
Given a 0-indexed integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks.
You may imagine that nums[-1] = nums[n] = -∞. In other words, an element is always considered to be strictly greater than a neighbor that is outside the array.
You must write an algorithm that runs in O(log n) time.
LeetCode Problem - 162 : Link | Click Here
class Solution {
public int findPeakElement(int[] nums) {
int result = 0; // Variable to store the result, initialized to 0.
int len = nums.length; // Length of the input array.
boolean secondLoop = false; // Flag to indicate if the second loop is needed.
// First loop to find a potential peak for sorted array.
for (int j = 0; j < nums.length - 1; j++) {
if (nums[j + 1] > nums[j]) {
result = j + 1; // Update result to the index of a potential peak.
} else {
result = 0; // Reset result to 0 if a array is not sorted.
secondLoop = true; // Set the flag to enter the second loop.
break;
}
}
// Second loop to find the actual peak if array is not sorted.
if (secondLoop) {
for (int i = 1; i < nums.length - 1; i++) {
if (nums[i] > nums[i - 1] && nums[i] > nums[i + 1]) {
result = i; // Update result to the index of the peak
break;
}
}
}
return result; // Return the final result, which represents the peak index.
}
}




