forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Longest_Increasing_Subsequence in Kotlin (jainaman224#2935)
Updated LIS
- Loading branch information
1 parent
9f500f9
commit 5de74e2
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
Longest_Increasing_Subsequence/Longest_Increasing_Subsequence.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
*/ | ||
|