Length of longest increasing subsequence
Here given code implementation process.
// C Program
// Length of longest increasing subsequence
#include <stdio.h>
// Print the elements of given array
void printElement(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
printf(" %d", arr[i]);
}
}
void longestSubsequences(int arr[], int n)
{
if (n <= 0)
{
return;
}
// Define auxiliary array
int length[n];
// Define some auxiliary variables
int i = 0;
int j = 0;
int result = 1;
// Set the initial value in auxiliary array
for (i = 0; i < n; i++)
{
length[i] = 1;
}
// Execute loop through by size of arr
for (i = 0; i < n; i++)
{
// calculate increasing subsequence length
for (j = 0; j < i; j++)
{
if (arr[i] > arr[j])
{
if (length[j] + 1 > length[i])
{
length[i] = length[j] + 1;
if (length[i] > result)
{
result = length[i];
}
}
}
}
}
// Display given array
printElement(arr, n);
// Display calculated result
printf("\n Length of Longest increasing subsequence is : %d\n", result);
}
int main(int argc, char
const *argv[])
{
// Define array of integer elements
int arr1[] = {
8 , 4 , 7 , 5 , 1 , 9 , 3
};
int arr2[] = {
9 , 10 , 3 , 4 , 8 , 7 , 10
};
// Test Case A
// [ 8 , 4 , 7 , 5 , 1 , 9, 3]
// length of Longest increasing subsequence 3
// (4, 5, 9)
// Result = 3
int n = sizeof(arr1) / sizeof(arr1[0]);
longestSubsequences(arr1, n);
// Test Case B
// [ 9, 10, 3, 4, 8, 7, 10]
// length of Longest increasing subsequence 4
// (3, 4, 8, 10), (3, 4, 7, 10)
// Result = 4
n = sizeof(arr2) / sizeof(arr2[0]);
longestSubsequences(arr2, n);
return 0;
}
Output
8 4 7 5 1 9 3
Length of Longest increasing subsequence is : 3
9 10 3 4 8 7 10
Length of Longest increasing subsequence is : 4
// Java program for
// Length of longest increasing subsequence
public class Subsequence
{
// Print the elements of given array
public void printElement(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
System.out.print(" " + arr[i]);
}
}
public void longestSubsequences(int[] arr, int n)
{
if (n <= 0)
{
return;
}
// Define auxiliary array
int[] length = new int[n];
// Define some auxiliary variables
int i = 0;
int j = 0;
int result = 1;
// Set the initial value in auxiliary array
for (i = 0; i < n; i++)
{
length[i] = 1;
}
// Execute loop through by size of arr
for (i = 0; i < n; i++)
{
// calculate increasing subsequence length
for (j = 0; j < i; j++)
{
if (arr[i] > arr[j])
{
if (length[j] + 1 > length[i])
{
length[i] = length[j] + 1;
if (length[i] > result)
{
result = length[i];
}
}
}
}
}
// Display given array
printElement(arr, n);
// Display calculated result
System.out.print("\n Length of Longest increasing subsequence is : " +
result + "\n");
}
public static void main(String[] args)
{
Subsequence task = new Subsequence();
// Define array of integer elements
int[] arr1 = {
8 , 4 , 7 , 5 , 1 , 9 , 3
};
int[] arr2 = {
9 , 10 , 3 , 4 , 8 , 7 , 10
};
// Test Case A
// [ 8 , 4 , 7 , 5 , 1 , 9, 3]
// length of Longest increasing subsequence 3
// (4, 5, 9)
// Result = 3
int n = arr1.length;
task.longestSubsequences(arr1, n);
// Test Case B
// [ 9, 10, 3, 4, 8, 7, 10]
// length of Longest increasing subsequence 4
// (3, 4, 8, 10), (3, 4, 7, 10)
// Result = 4
n = arr2.length;
task.longestSubsequences(arr2, n);
}
}
Output
8 4 7 5 1 9 3
Length of Longest increasing subsequence is : 3
9 10 3 4 8 7 10
Length of Longest increasing subsequence is : 4
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Length of longest increasing subsequence
class Subsequence
{
public:
// Print the elements of given array
void printElement(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
cout << " " << arr[i];
}
}
void longestSubsequences(int arr[], int n)
{
if (n <= 0)
{
return;
}
// Define auxiliary array
int length[n];
// Define some auxiliary variables
int i = 0;
int j = 0;
int result = 1;
// Set the initial value in auxiliary array
for (i = 0; i < n; i++)
{
length[i] = 1;
}
// Execute loop through by size of arr
for (i = 0; i < n; i++)
{
// calculate increasing subsequence length
for (j = 0; j < i; j++)
{
if (arr[i] > arr[j])
{
if (length[j] + 1 > length[i])
{
length[i] = length[j] + 1;
if (length[i] > result)
{
result = length[i];
}
}
}
}
}
// Display given array
this->printElement(arr, n);
// Display calculated result
cout << "\n Length of Longest increasing subsequence is : "
<< result
<< "\n";
}
};
int main()
{
Subsequence *task = new Subsequence();
// Define array of integer elements
int arr1[] = {
8 , 4 , 7 , 5 , 1 , 9 , 3
};
int arr2[] = {
9 , 10 , 3 , 4 , 8 , 7 , 10
};
// Test Case A
// [ 8 , 4 , 7 , 5 , 1 , 9, 3]
// length of Longest increasing subsequence 3
// (4, 5, 9)
// Result = 3
int n = sizeof(arr1) / sizeof(arr1[0]);
task->longestSubsequences(arr1, n);
// Test Case B
// [ 9, 10, 3, 4, 8, 7, 10]
// length of Longest increasing subsequence 4
// (3, 4, 8, 10), (3, 4, 7, 10)
// Result = 4
n = sizeof(arr2) / sizeof(arr2[0]);
task->longestSubsequences(arr2, n);
return 0;
}
Output
8 4 7 5 1 9 3
Length of Longest increasing subsequence is : 3
9 10 3 4 8 7 10
Length of Longest increasing subsequence is : 4
// Include namespace system
using System;
// Csharp program for
// Length of longest increasing subsequence
public class Subsequence
{
// Print the elements of given array
public void printElement(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
Console.Write(" " + arr[i]);
}
}
public void longestSubsequences(int[] arr, int n)
{
if (n <= 0)
{
return;
}
// Define auxiliary array
int[] length = new int[n];
// Define some auxiliary variables
int i = 0;
int j = 0;
int result = 1;
// Set the initial value in auxiliary array
for (i = 0; i < n; i++)
{
length[i] = 1;
}
// Execute loop through by size of arr
for (i = 0; i < n; i++)
{
// calculate increasing subsequence length
for (j = 0; j < i; j++)
{
if (arr[i] > arr[j])
{
if (length[j] + 1 > length[i])
{
length[i] = length[j] + 1;
if (length[i] > result)
{
result = length[i];
}
}
}
}
}
// Display given array
this.printElement(arr, n);
// Display calculated result
Console.Write("\n Length of Longest increasing subsequence is : " +
result + "\n");
}
public static void Main(String[] args)
{
Subsequence task = new Subsequence();
// Define array of integer elements
int[] arr1 = {
8 , 4 , 7 , 5 , 1 , 9 , 3
};
int[] arr2 = {
9 , 10 , 3 , 4 , 8 , 7 , 10
};
// Test Case A
// [ 8 , 4 , 7 , 5 , 1 , 9, 3]
// length of Longest increasing subsequence 3
// (4, 5, 9)
// Result = 3
int n = arr1.Length;
task.longestSubsequences(arr1, n);
// Test Case B
// [ 9, 10, 3, 4, 8, 7, 10]
// length of Longest increasing subsequence 4
// (3, 4, 8, 10), (3, 4, 7, 10)
// Result = 4
n = arr2.Length;
task.longestSubsequences(arr2, n);
}
}
Output
8 4 7 5 1 9 3
Length of Longest increasing subsequence is : 3
9 10 3 4 8 7 10
Length of Longest increasing subsequence is : 4
package main
import "fmt"
// Go program for
// Length of longest increasing subsequence
type Subsequence struct {}
func getSubsequence() * Subsequence {
var me *Subsequence = &Subsequence {}
return me
}
// Print the elements of given array
func(this Subsequence) printElement(arr[] int, n int) {
for i := 0 ; i < n ; i++ {
fmt.Print(" ", arr[i])
}
}
func(this Subsequence) longestSubsequences(arr[] int, n int) {
if n <= 0 {
return
}
// Define auxiliary array
var length = make([] int, n)
// Set initial 1 length of each element
for i := 0; i < n; i++ {
length[i] = 1
}
// Define some auxiliary variables
var i int = 0
var j int = 0
var result int = 1
// Execute loop through by size of arr
for i = 0 ; i < n ; i++ {
// calculate increasing subsequence length
for j = 0 ; j < i ; j++ {
if arr[i] > arr[j] {
if length[j] + 1 > length[i] {
length[i] = length[j] + 1
if length[i] > result {
result = length[i]
}
}
}
}
}
// Display given array
this.printElement(arr, n)
// Display calculated result
fmt.Println("\n Length of Longest increasing subsequence is : ",
result)
}
func main() {
var task * Subsequence = getSubsequence()
// Define array of integer elements
var arr1 = [] int { 8 , 4 , 7 , 5 , 1 , 9 , 3 }
var arr2 = [] int { 9 , 10 , 3 , 4 , 8 , 7 , 10 }
// Test Case A
// [ 8 , 4 , 7 , 5 , 1 , 9, 3]
// length of Longest increasing subsequence 3
// (4, 5, 9)
// Result = 3
var n int = len(arr1)
task.longestSubsequences(arr1, n)
// Test Case B
// [ 9, 10, 3, 4, 8, 7, 10]
// length of Longest increasing subsequence 4
// (3, 4, 8, 10), (3, 4, 7, 10)
// Result = 4
n = len(arr2)
task.longestSubsequences(arr2, n)
}
Output
8 4 7 5 1 9 3
Length of Longest increasing subsequence is : 3
9 10 3 4 8 7 10
Length of Longest increasing subsequence is : 4
<?php
// Php program for
// Length of longest increasing subsequence
class Subsequence
{
// Print the elements of given array
public function printElement($arr, $n)
{
for ($i = 0; $i < $n; ++$i)
{
echo(" ".$arr[$i]);
}
}
public function longestSubsequences($arr, $n)
{
if ($n <= 0)
{
return;
}
// Define auxiliary array
// Set initial 1 length of each element
$length = array_fill(0, $n, 1);
// Define some auxiliary variables
$i = 0;
$j = 0;
$result = 1;
// Execute loop through by size of arr
for ($i = 0; $i < $n; $i++)
{
// calculate increasing subsequence length
for ($j = 0; $j < $i; $j++)
{
if ($arr[$i] > $arr[$j])
{
if ($length[$j] + 1 > $length[$i])
{
$length[$i] = $length[$j] + 1;
if ($length[$i] > $result)
{
$result = $length[$i];
}
}
}
}
}
// Display given array
$this->printElement($arr, $n);
// Display calculated result
echo("\n Length of Longest increasing subsequence is : ".$result.
"\n");
}
}
function main()
{
$task = new Subsequence();
// Define array of integer elements
$arr1 = array(8, 4, 7, 5, 1, 9, 3);
$arr2 = array(9, 10, 3, 4, 8, 7, 10);
// Test Case A
// [ 8 , 4 , 7 , 5 , 1 , 9, 3]
// length of Longest increasing subsequence 3
// (4, 5, 9)
// Result = 3
$n = count($arr1);
$task->longestSubsequences($arr1, $n);
// Test Case B
// [ 9, 10, 3, 4, 8, 7, 10]
// length of Longest increasing subsequence 4
// (3, 4, 8, 10), (3, 4, 7, 10)
// Result = 4
$n = count($arr2);
$task->longestSubsequences($arr2, $n);
}
main();
Output
8 4 7 5 1 9 3
Length of Longest increasing subsequence is : 3
9 10 3 4 8 7 10
Length of Longest increasing subsequence is : 4
// Node JS program for
// Length of longest increasing subsequence
class Subsequence
{
// Print the elements of given array
printElement(arr, n)
{
for (var i = 0; i < n; ++i)
{
process.stdout.write(" " + arr[i]);
}
}
longestSubsequences(arr, n)
{
if (n <= 0)
{
return;
}
// Define auxiliary array
// Set initial 1 length of each element
var length = Array(n).fill(1);
// Define some auxiliary variables
var i = 0;
var j = 0;
var result = 1;
// Execute loop through by size of arr
for (i = 0; i < n; i++)
{
// calculate increasing subsequence length
for (j = 0; j < i; j++)
{
if (arr[i] > arr[j])
{
if (length[j] + 1 > length[i])
{
length[i] = length[j] + 1;
if (length[i] > result)
{
result = length[i];
}
}
}
}
}
// Display given array
this.printElement(arr, n);
// Display calculated result
console.log("\n Length of Longest increasing subsequence is : " +
result );
}
}
function main()
{
var task = new Subsequence();
// Define array of integer elements
var arr1 = [8, 4, 7, 5, 1, 9, 3];
var arr2 = [9, 10, 3, 4, 8, 7, 10];
// Test Case A
// [ 8 , 4 , 7 , 5 , 1 , 9, 3]
// length of Longest increasing subsequence 3
// (4, 5, 9)
// Result = 3
var n = arr1.length;
task.longestSubsequences(arr1, n);
// Test Case B
// [ 9, 10, 3, 4, 8, 7, 10]
// length of Longest increasing subsequence 4
// (3, 4, 8, 10), (3, 4, 7, 10)
// Result = 4
n = arr2.length;
task.longestSubsequences(arr2, n);
}
main();
Output
8 4 7 5 1 9 3
Length of Longest increasing subsequence is : 3
9 10 3 4 8 7 10
Length of Longest increasing subsequence is : 4
# Python 3 program for
# Length of longest increasing subsequence
class Subsequence :
# Print the elements of given list
def printElement(self, arr, n) :
i = 0
while (i < n) :
print(" ", arr[i], end = "")
i += 1
def longestSubsequences(self, arr, n) :
if (n <= 0) :
return
# Define auxiliary list
# Set initial 1 length of each element
length = [1] * (n)
# Define some auxiliary variables
i = 0
j = 0
result = 1
i = 0
# Execute loop through by size of arr
while (i < n) :
j = 0
# calculate increasing subsequence length
while (j < i) :
if (arr[i] > arr[j]) :
if (length[j] + 1 > length[i]) :
length[i] = length[j] + 1
if (length[i] > result) :
result = length[i]
j += 1
i += 1
# Display given list
self.printElement(arr, n)
# Display calculated result
print("\n Length of Longest increasing subsequence is : ", result)
def main() :
task = Subsequence()
# Define list of integer elements
arr1 = [8, 4, 7, 5, 1, 9, 3]
arr2 = [9, 10, 3, 4, 8, 7, 10]
# Test Case A
# [ 8 , 4 , 7 , 5 , 1 , 9, 3]
# length of Longest increasing subsequence 3
# (4, 5, 9)
# Result = 3
n = len(arr1)
task.longestSubsequences(arr1, n)
# Test Case B
# [ 9, 10, 3, 4, 8, 7, 10]
# length of Longest increasing subsequence 4
# (3, 4, 8, 10), (3, 4, 7, 10)
# Result = 4
n = len(arr2)
task.longestSubsequences(arr2, n)
if __name__ == "__main__": main()
Output
8 4 7 5 1 9 3
Length of Longest increasing subsequence is : 3
9 10 3 4 8 7 10
Length of Longest increasing subsequence is : 4
# Ruby program for
# Length of longest increasing subsequence
class Subsequence
# Print the elements of given array
def printElement(arr, n)
i = 0
while (i < n)
print(" ", arr[i])
i += 1
end
end
def longestSubsequences(arr, n)
if (n <= 0)
return
end
# Define auxiliary array
# Set initial 1 length of each element
length = Array.new(n) {1}
# Define some auxiliary variables
i = 0
j = 0
result = 1
i = 0
# Execute loop through by size of arr
while (i < n)
j = 0
# calculate increasing subsequence length
while (j < i)
if (arr[i] > arr[j])
if (length[j] + 1 > length[i])
length[i] = length[j] + 1
if (length[i] > result)
result = length[i]
end
end
end
j += 1
end
i += 1
end
# Display given array
self.printElement(arr, n)
# Display calculated result
print("\n Length of Longest increasing subsequence is : ",
result, "\n")
end
end
def main()
task = Subsequence.new()
# Define array of integer elements
arr1 = [8, 4, 7, 5, 1, 9, 3]
arr2 = [9, 10, 3, 4, 8, 7, 10]
# Test Case A
# [ 8 , 4 , 7 , 5 , 1 , 9, 3]
# length of Longest increasing subsequence 3
# (4, 5, 9)
# Result = 3
n = arr1.length
task.longestSubsequences(arr1, n)
# Test Case B
# [ 9, 10, 3, 4, 8, 7, 10]
# length of Longest increasing subsequence 4
# (3, 4, 8, 10), (3, 4, 7, 10)
# Result = 4
n = arr2.length
task.longestSubsequences(arr2, n)
end
main()
Output
8 4 7 5 1 9 3
Length of Longest increasing subsequence is : 3
9 10 3 4 8 7 10
Length of Longest increasing subsequence is : 4
import Foundation;
// Swift 4 program for
// Length of longest increasing subsequence
class Subsequence
{
// Print the elements of given array
func printElement(_ arr: [Int], _ n: Int)
{
var i: Int = 0;
while (i < n)
{
print(" ", arr[i], terminator: "");
i += 1;
}
}
func longestSubsequences(_ arr: [Int], _ n: Int)
{
if (n <= 0)
{
return;
}
// Define auxiliary array
// Set initial 1 length of each element
var length: [Int] = Array(repeating: 1, count: n);
// Define some auxiliary variables
var i: Int = 0;
var j: Int = 0;
var result: Int = 1;
// Execute loop through by size of arr
while (i < n)
{
j = 0;
// calculate increasing subsequence length
while (j < i)
{
if (arr[i] > arr[j])
{
if (length[j] + 1 > length[i])
{
length[i] = length[j] + 1;
if (length[i] > result)
{
result = length[i];
}
}
}
j += 1;
}
i += 1;
}
// Display given array
self.printElement(arr, n);
// Display calculated result
print("\n Length of Longest increasing subsequence is : ",
result);
}
}
func main()
{
let task: Subsequence = Subsequence();
// Define array of integer elements
let arr1: [Int] = [8, 4, 7, 5, 1, 9, 3];
let arr2: [Int] = [9, 10, 3, 4, 8, 7, 10];
// Test Case A
// [ 8 , 4 , 7 , 5 , 1 , 9, 3]
// length of Longest increasing subsequence 3
// (4, 5, 9)
// Result = 3
var n: Int = arr1.count;
task.longestSubsequences(arr1, n);
// Test Case B
// [ 9, 10, 3, 4, 8, 7, 10]
// length of Longest increasing subsequence 4
// (3, 4, 8, 10), (3, 4, 7, 10)
// Result = 4
n = arr2.count;
task.longestSubsequences(arr2, n);
}
main();
Output
8 4 7 5 1 9 3
Length of Longest increasing subsequence is : 3
9 10 3 4 8 7 10
Length of Longest increasing subsequence is : 4
// Scala program for
// Length of longest increasing subsequence
class Subsequence()
{
// Print the elements of given array
def printElement(arr: Array[Int], n: Int): Unit = {
var i: Int = 0;
while (i < n)
{
print(" " + arr(i));
i += 1;
}
}
def longestSubsequences(arr: Array[Int], n: Int): Unit = {
if (n <= 0)
{
return;
}
// Define auxiliary array
// Set initial 1 length of each element
var length: Array[Int] = Array.fill[Int](n)(1);
// Define some auxiliary variables
var i: Int = 0;
var j: Int = 0;
var result: Int = 1;
// Execute loop through by size of arr
while (i < n)
{
j = 0;
// calculate increasing subsequence length
while (j < i)
{
if (arr(i) > arr(j))
{
if (length(j) + 1 > length(i))
{
length(i) = length(j) + 1;
if (length(i) > result)
{
result = length(i);
}
}
}
j += 1;
}
i += 1;
}
// Display given array
printElement(arr, n);
// Display calculated result
println("\n Length of Longest increasing subsequence is : " +
result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Subsequence = new Subsequence();
// Define array of integer elements
var arr1: Array[Int] = Array(8, 4, 7, 5, 1, 9, 3);
var arr2: Array[Int] = Array(9, 10, 3, 4, 8, 7, 10);
// Test Case A
// [ 8 , 4 , 7 , 5 , 1 , 9, 3]
// length of Longest increasing subsequence 3
// (4, 5, 9)
// Result = 3
var n: Int = arr1.length;
task.longestSubsequences(arr1, n);
// Test Case B
// [ 9, 10, 3, 4, 8, 7, 10]
// length of Longest increasing subsequence 4
// (3, 4, 8, 10), (3, 4, 7, 10)
// Result = 4
n = arr2.length;
task.longestSubsequences(arr2, n);
}
}
Output
8 4 7 5 1 9 3
Length of Longest increasing subsequence is : 3
9 10 3 4 8 7 10
Length of Longest increasing subsequence is : 4
// Kotlin program for
// Length of longest increasing subsequence
class Subsequence
{
// Print the elements of given array
fun printElement(arr: Array < Int > , n: Int): Unit
{
var i: Int = 0;
while (i < n)
{
print(" " + arr[i]);
i += 1;
}
}
fun longestSubsequences(arr: Array < Int > , n: Int): Unit
{
if (n <= 0)
{
return;
}
// Define auxiliary array
// Set initial 1 length of each element
var length: Array < Int > = Array(n)
{
1
};
// Define some auxiliary variables
var i: Int = 0;
var j: Int ;
var result: Int = 1;
// Execute loop through by size of arr
while (i < n)
{
j = 0;
// calculate increasing subsequence length
while (j < i)
{
if (arr[i] > arr[j])
{
if (length[j] + 1 > length[i])
{
length[i] = length[j] + 1;
if (length[i] > result)
{
result = length[i];
}
}
}
j += 1;
}
i += 1;
}
// Display given array
this.printElement(arr, n);
// Display calculated result
println("\n Length of Longest increasing subsequence is : " +
result);
}
}
fun main(args: Array < String > ): Unit
{
val task: Subsequence = Subsequence();
// Define array of integer elements
val arr1: Array < Int > = arrayOf(8, 4, 7, 5, 1, 9, 3);
val arr2: Array < Int > = arrayOf(9, 10, 3, 4, 8, 7, 10);
// Test Case A
// [ 8 , 4 , 7 , 5 , 1 , 9, 3]
// length of Longest increasing subsequence 3
// (4, 5, 9)
// Result = 3
var n: Int = arr1.count();
task.longestSubsequences(arr1, n);
// Test Case B
// [ 9, 10, 3, 4, 8, 7, 10]
// length of Longest increasing subsequence 4
// (3, 4, 8, 10), (3, 4, 7, 10)
// Result = 4
n = arr2.count();
task.longestSubsequences(arr2, n);
}
Output
8 4 7 5 1 9 3
Length of Longest increasing subsequence is : 3
9 10 3 4 8 7 10
Length of Longest increasing subsequence is : 4
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment