Hello Friends Today, through this tutorial, I will tell you How to Find Peak Element in the Array in Kotlin Language Program?
This approach iterates through the entire array and compares each element with its neighbors. If an element is greater than both its neighbors, it’s a peak element.
fun findPeakElementBruteForce(arr: IntArray): Int { if (arr.isEmpty()) return -1 // Handle empty array case for (i in 1 until arr.lastIndex) { if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) { return arr[i] } } // Check if the first or last element is the peak return if (arr[0] > arr[1]) arr[0] else arr[arr.lastIndex] }