From 5de74e2f4aa5c5ea6b3157fc75cdaa81face7e31 Mon Sep 17 00:00:00 2001 From: sandhyabhan <52604131+sandhyabhan@users.noreply.github.com> Date: Sun, 31 May 2020 18:35:58 +0530 Subject: [PATCH] Add Longest_Increasing_Subsequence in Kotlin (#2935) Updated LIS --- .../Longest_Increasing_Subsequence.kt | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Longest_Increasing_Subsequence/Longest_Increasing_Subsequence.kt diff --git a/Longest_Increasing_Subsequence/Longest_Increasing_Subsequence.kt b/Longest_Increasing_Subsequence/Longest_Increasing_Subsequence.kt new file mode 100644 index 0000000000..c10c8c24f4 --- /dev/null +++ b/Longest_Increasing_Subsequence/Longest_Increasing_Subsequence.kt @@ -0,0 +1,52 @@ +//Kotlin code for Longest increasing subsequence + +class LIS +{ + fun lis(int:arr[], int:n):int + { + int lis[] = new int[n]; + int result = Integer.MIN_VALUE; + + for (i in 0 until n) + { + lis[i] = 1; + } + + for (i in 1 until n) + { + for (j in 0 until i) + { + if (arr[i] > arr[j] && lis[i] < lis[j] + 1) + { + lis[i] = lis[j] + 1; + } + } + result = Math.max(result, lis[i]); + } + return result; + } + + fun main() + { + var read = Scanner(System.`in`) + println("Enter the size of the Array:") + val arrSize = read.nextLine().toInt() + var arr = IntArray(arrSize) + println("Enter elements") + + for(i in 0 until arrSize) + { + arr[i] = read.nextLine().toInt() + } + + int n = arrSize; + println("\nLength of the Longest Increasing Subsequence:" + lis(arr, n)) + } +} + +/* +Enter the size of the array : 8 +Enter elements : 15 26 13 38 26 52 43 62 +Length of the Longest Increasing Subsequence : 5 +*/ +