Kmspico Download | Official KMS Activator Website [New Version 2024] Fast and Easy Converter YouTube to MP3 Online KMSAuto Net Activator Download 2024 Immediate Byte Pro Neoprofit AI Blacksprut without borders. Discover new shopping opportunities here where each link is an entrance to a world ruled by anonymity and freedom.

How to Find Peak Element in the Array in Kotlin?

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]
}