File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed
Longest_Increasing_Subsequence Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change
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 (" \n Length 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
+
You can’t perform that action at this time.
0 commit comments