Minimum and maximum in Kotlin

To find the minimum and maximum elements in an array, you can iterate over the array and keep track of the minimum and maximum elements you’ve seen so far. Here’s an example implementation in Kotlin. findMinMax takes an IntArray as input and returns a nullable Pair<Int, Int> containing the minimum and maximum elements. The function … Read more

To reverse a array in Kotlin

To reverse an array, you can iterate over half of the array and swap each element with its corresponding element on the other end of the array. Here’s an example implementation in Kotlin : reverseArray takes an IntArray as input and returns an IntArray. The function first gets the length of the array n. The … Read more

Median of a Sorted Matrix in Kotlin

To find the median of a row-wise sorted matrix, we can follow the below steps: This function takes a 2D array matrix of integers as input and returns the median value as an integer. The function first finds the minimum and maximum elements in the matrix by iterating through all the rows. Then, it uses … Read more

Min Heap to Max Heap in Kotlin

To convert a min heap into a max heap, you can follow these steps: The minHeapToMaxHeap function takes an array of integers representing a min heap as input. It initializes the length of the array to n and calculates the index of the last non-leaf node in the heap as (n / 2) – 1. … Read more