Skip to content

Commit 5de74e2

Browse files
authored
Add Longest_Increasing_Subsequence in Kotlin (jainaman224#2935)
Updated LIS
1 parent 9f500f9 commit 5de74e2

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//Kotlin code for Longest increasing subsequence
2+
3+
class LIS
4+
{
5+
fun lis(int:arr[], int:n):int
6+
{
7+
int lis[] = new int[n];
8+
int result = Integer.MIN_VALUE;
9+
10+
for (i in 0 until n)
11+
{
12+
lis[i] = 1;
13+
}
14+
15+
for (i in 1 until n)
16+
{
17+
for (j in 0 until i)
18+
{
19+
if (arr[i] > arr[j] && lis[i] < lis[j] + 1)
20+
{
21+
lis[i] = lis[j] + 1;
22+
}
23+
}
24+
result = Math.max(result, lis[i]);
25+
}
26+
return result;
27+
}
28+
29+
fun main()
30+
{
31+
var read = Scanner(System.`in`)
32+
println("Enter the size of the Array:")
33+
val arrSize = read.nextLine().toInt()
34+
var arr = IntArray(arrSize)
35+
println("Enter elements")
36+
37+
for(i in 0 until arrSize)
38+
{
39+
arr[i] = read.nextLine().toInt()
40+
}
41+
42+
int n = arrSize;
43+
println("\nLength of the Longest Increasing Subsequence:" + lis(arr, n))
44+
}
45+
}
46+
47+
/*
48+
Enter the size of the array : 8
49+
Enter elements : 15 26 13 38 26 52 43 62
50+
Length of the Longest Increasing Subsequence : 5
51+
*/
52+

0 commit comments

Comments
 (0)