Median of a Array in Kotlin

To find the median of an array, we first need to sort the array in ascending or descending order, depending on whether the array has an even or odd number of elements. If the array has an odd number of elements, then the median is simply the middle element of the sorted array. If the … Read more

Factorial of a Number in Kotlin

To find the factorial of a number, you can use a loop to multiply the number by each integer less than it until you reach 1. For example, to find the factorial of 5, you would perform the following calculation: here’s an example Kotlin code to calculate the factorial of a number: In this code, … Read more

Find if Sum of Subarray = 0 in Kotlin

To find if there is any subarray with sum = 0 in an array, we can use a technique called the prefix sum approach. Here’s how it works: Here’s what the code would look like in Kotlin: The time complexity of the hasSubarrayWithSumZero function is O(n), where n is the length of the input array. … Read more

Rearrange Array in alternating pattern of positive and negative elements in Kotlin

To rearrange an array in an alternating pattern of positive and negative elements, you can follow these steps: Here’s some sample code in Kotlin to implement this algorithm with The space complexity of the function is O(1): The time complexity of the rearrangeArrayInPlace function is O(n), where n is the length of the input array. … Read more

Find the Common in Three Sorted Array in Kotlin

One common approach to finding common elements in three sorted arrays is to use a “merge” strategy, where you compare the elements of the three arrays and find the ones that are common. Here’s a step-by-step algorithm to accomplish this: Here’s an example Kotlin code for finding common elements in three sorted arrays: The time … Read more

Count Inversions Problem in Kotlin

In computer science, the count inversions problem is a classic algorithmic problem that involves counting the number of inversions in an array. An inversion is a pair of indices (i, j) in an array such that i < j and nums[i] > nums[j]. In other words, it represents two elements in the array that are … Read more

Next Permutation Problem in Kotlin

The next permutation problem is a classic problem in computer science that involves finding the lexicographically next permutation of a given sequence of numbers. In other words, given a sequence of numbers, the goal is to find the smallest possible sequence that comes after the original sequence in lexicographic order. For example, if the original … Read more