forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Longest_Increasing_Subsequence.kt
52 lines (44 loc) · 1.13 KB
/
Longest_Increasing_Subsequence.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//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
*/