21. Write a program to flatten a nested list into a single list.

Required Input:

Nested list: [[1, 2], [3, 4], [5, [6, 7]]]

Expected Output:

Flattened list: [1, 2, 3, 4, 5, 6, 7]

Code In Kotlin

fun flattenList(nestedList: List<Any>): List<Int> { // Add your code here to flatten the nested list } fun main() { val nestedList = listOf(listOf(1, 2), listOf(3, 4), listOf(5, listOf(6, 7))) println("Flattened list: ${flattenList(nestedList)}") }

Run Code?

Click Run Button to view compiled output

22. Create a function to check if a number is a perfect number.

Required Input:

Number: 28

Expected Output:

28 is a perfect number

Code In Kotlin

fun isPerfectNumber(num: Int): Boolean { // Add your code here to check if the number is perfect } fun main() { val number = 28 if (isPerfectNumber(number)) { println("$number is a perfect number") } else { println("$number is not a perfect number") } }

Run Code?

Click Run Button to view compiled output

23. Write a program to calculate the nth Fibonacci number using dynamic programming.

Required Input:

n = 10

Expected Output:

The 10-th Fibonacci number is: 55

Code In Kotlin

fun fibonacci(n: Int): Int { // Add your code here to calculate the nth Fibonacci number } fun main() { val n = 10 println("The $n-th Fibonacci number is: ${fibonacci(n)}") }

Run Code?

Click Run Button to view compiled output

24. Implement a function to find the largest rectangle area in a histogram.

Required Input:

Histogram heights: [2, 1, 5, 6, 2, 3]

Expected Output:

Largest rectangle area: 10

Code In Kotlin

fun largestRectangleArea(heights: Array<Int>): Int { // Add your code here to find the largest rectangle area } fun main() { val heights = arrayOf(2, 1, 5, 6, 2, 3) println("Largest rectangle area: ${largestRectangleArea(heights)}") }

Run Code?

Click Run Button to view compiled output

25. Write a Kotlin program to perform matrix multiplication.

Required Input:

Matrix A = [[1, 2], [3, 4]], Matrix B = [[5, 6], [7, 8]]

Expected Output:

19 22
43 50

Code In Kotlin

fun multiplyMatrices(a: Array<Array<Int>>, b: Array<Array<Int>>): Array<Array<Int>> { // Add your code here to multiply the matrices } fun main() { val a = arrayOf(arrayOf(1, 2), arrayOf(3, 4)) val b = arrayOf(arrayOf(5, 6), arrayOf(7, 8)) val result = multiplyMatrices(a, b) result.forEach { println(it.joinToString(" ")) } }

Run Code?

Click Run Button to view compiled output

26. Create a program to find all permutations of a string.

Required Input:

String: "abc"

Expected Output:

Permutations: [abc, acb, bac, bca, cba, cab]

Code In Kotlin

fun findPermutations(input: String): List<String> { // Add your code here to generate permutations } fun main() { val input = "abc" println("Permutations: ${findPermutations(input)}") }

Run Code?

Click Run Button to view compiled output

27. Write a function to group anagrams from a list of strings.

Required Input:

List: ["eat", "tea", "tan", "ate", "nat", "bat"]

Expected Output:

Grouped anagrams: [[eat, tea, ate], [tan, nat], [bat]]

Code In Kotlin

fun groupAnagrams(strings: List<String>): List<List<String>> { // Add your code here to group anagrams } fun main() { val strings = listOf("eat", "tea", "tan", "ate", "nat", "bat") println("Grouped anagrams: ${groupAnagrams(strings)}") }

Run Code?

Click Run Button to view compiled output

28. Implement a function to calculate the maximum product of three numbers in an array.

Required Input:

Array: [-10, -10, 5, 2]

Expected Output:

Maximum product: 500

Code In Kotlin

fun maxProductOfThree(nums: List<Int>): Int { // Add your code here to calculate the maximum product } fun main() { val nums = listOf(-10, -10, 5, 2) println("Maximum product: ${maxProductOfThree(nums)}") }

Run Code?

Click Run Button to view compiled output

29. Write a program to find all subsets of a given set.

Required Input:

Set: [1, 2, 3]

Expected Output:

Subsets: [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]

Code In Kotlin

fun findSubsets(nums: List<Int>): List<List<Int>> { // Add your code here to find subsets } fun main() { val nums = listOf(1, 2, 3) println("Subsets: ${findSubsets(nums)}") }

Run Code?

Click Run Button to view compiled output

30. Create a function to find the smallest missing positive integer in an array.

Required Input:

Array: [3, 4, -1, 1]

Expected Output:

Smallest missing positive integer: 2

Code In Kotlin

fun findSmallestMissingPositive(nums: List<Int>): Int { // Add your code here to find the smallest missing positive integer } fun main() { val nums = listOf(3, 4, -1, 1) println("Smallest missing positive integer: ${findSmallestMissingPositive(nums)}") }

Run Code?

Click Run Button to view compiled output

ad vertical

3 of 3