Longest consecutive subsequence

Here given code implementation process.

/*
    Java Program
    Longest consecutive subsequence
*/
import java.util.HashSet;
public class Subsequence
{
	// Function which is display array elements
	public void display(int[] arr, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			System.out.print("   " + arr[i]);
		}
		System.out.print("\n");
	}
	// Find the length of longest contiguous subsequence in given array
	public void longestConsecutive(int[] arr, int n)
	{
		// Display given array
		display(arr, n);
		// Use to collect unique element
		HashSet < Integer > container = new HashSet < Integer > ();
		// Define counter variable
		int result = 0;
		// Loop controlling variables
		int i = 0;
		int j = 0;
		// Get unique element in given array
		// iterate the loop through by size
		for (i = 0; i < n; ++i)
		{
			// collect element
			container.add(arr[i]);
		}
		// iterate the loop through by size
		for (i = 0; i < n; ++i)
		{
			// Check if element of array -1 is not exist in container 
			if (container.contains(arr[i] - 1) == false)
			{
				// Count consecutive sequence
				while (container.contains(arr[i] + j) == true)
				{
					// Next consecutive elements
					j = j + 1;
				}
				if (result < j)
				{
					// Get new result
					result = j;
				}
				// reset the value of variable j
				j = 0;
			}
		}
		// Display calculated result        
		System.out.print(" Length of longest consecutive subsequence is : " + result+"\n");
	}
	public static void main(String[] arg)
	{
		Subsequence task = new Subsequence();
		// Define array of integer elements
		int[] arr = 
        {
			7, 1, 7, 9, 2, 8, 6, 11, 15 , 5
		};
		// Get number of element
		int size = arr.length;
        // Test
      	// 5,6,7,8,9 (consecutive subsequence)
		task.longestConsecutive(arr, size);
	}
}

Output

   7   1   7   9   2   8   6   11   15   5
 Length of longest consecutive subsequence is : 5
// Include header file
#include <iostream>
#include <set>
using namespace std;

class Subsequence
{
	public:
		// Function which is display array elements
		void display(int arr[], int n)
		{
			for (int i = 0; i < n; ++i)
			{
				cout << "   " << arr[i];
			}
			cout << "\n";
		}
	// Find the length of longest contiguous subsequence in given array
	void longestConsecutive(int arr[], int n)
	{
		// Display given array
		this->display(arr, n);
		// Use to collect unique element
		set <int> container;
		// Define counter variable
		int result = 0;
		// Loop controlling variables
		int i = 0;
		int j = 0;
		// Get unique element in given array
		// iterate the loop through by size
		for (i = 0; i < n; ++i)
		{
			container.insert(arr[i]);
		}
		// iterate the loop through by size
		for (i = 0; i < n; ++i)
		{
			// Check if element of array -1 is not exist in container
			if (container.find(arr[i] - 1) == container.end())
			{
				// Count consecutive sequence
				while (container.find(arr[i] + j) != container.end())
				{
					// Next consecutive elements
					j = j + 1;
				}
				if (result < j)
				{
					// Get new result
					result = j;
				}
				// reset the value of variable j
				j = 0;
			}
		}
		// Display calculated result
		cout << " Length of longest consecutive subsequence is : " << result << "\n";
	}
};
int main()
{
	Subsequence task = Subsequence();
	// Define array of integer elements
	int arr[] = 
    {
		7 , 1 , 7 , 9 , 2 , 8 , 6 , 11 , 15 , 5
	};
	// Get number of element
	int size = sizeof(arr) / sizeof(arr[0]);
	// Test
	// 5,6,7,8,9 (consecutive subsequence)
	task.longestConsecutive(arr, size);
	return 0;
}

Output

   7   1   7   9   2   8   6   11   15   5
 Length of longest consecutive subsequence is : 5
// Include namespace system
using System;
using System.Collections.Generic;
/*
    C# Program
    Longest consecutive subsequence
*/
public class Subsequence
{
	// Function which is display array elements
	public void display(int[] arr, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			Console.Write("   " + arr[i]);
		}
		Console.Write("\n");
	}
	// Find the length of longest contiguous subsequence in given array
	public void longestConsecutive(int[] arr, int n)
	{
		// Display given array
		display(arr, n);
		// Use to collect unique element
		HashSet < int > container = new HashSet < int > ();
		// Define counter variable
		int result = 0;
		// Loop controlling variables
		int i = 0;
		int j = 0;
		// Get unique element in given array
		// iterate the loop through by size
		for (i = 0; i < n; ++i)
		{
			container.Add(arr[i]);
		}
		// iterate the loop through by size
		for (i = 0; i < n; ++i)
		{
			// Check if element of array -1 is not exist in container
			if (container.Contains(arr[i] - 1) == false)
			{
				// Count consecutive sequence
				while (container.Contains(arr[i] + j) == true)
				{
					// Next consecutive elements
					j = j + 1;
				}
				if (result < j)
				{
					// Get new result
					result = j;
				}
				// reset the value of variable j
				j = 0;
			}
		}
		// Display calculated result
		Console.Write(" Length of longest consecutive subsequence is : " + result + "\n");
	}
	public static void Main(String[] arg)
	{
		Subsequence task = new Subsequence();
		// Define array of integer elements
		int[] arr = {
			7 , 1 , 7 , 9 , 2 , 8 , 6 , 11 , 15 , 5
		};
		// Get number of element
		int size = arr.Length;
		// Test
		// 5,6,7,8,9 (consecutive subsequence)
		task.longestConsecutive(arr, size);
	}
}

Output

   7   1   7   9   2   8   6   11   15   5
 Length of longest consecutive subsequence is : 5
<?php
/*
    Php Program
    Longest consecutive subsequence
*/
class Subsequence
{
	// Function which is display array elements
	public	function display( $arr, $n)
	{
		for ($i = 0; $i < $n; ++$i)
		{
			echo "   ". $arr[$i];
		}
		echo "\n";
	}
	// Find the length of longest contiguous subsequence in given array
	public	function longestConsecutive( $arr, $n)
	{
		// Display given array
		$this->display($arr, $n);
		// Use to collect unique element
		$container = array();
		// Define counter variable
		$result = 0;
		// Loop controlling variables
		$i = 0;
		$j = 0;
		// Get unique element in given array
		// iterate the loop through by size
		for ($i = 0; $i < $n; ++$i)
		{
          	if (in_array( $arr[$i], $container) == false)
			{
				$container[] = $arr[$i];
			}
		}
		// iterate the loop through by size
		for ($i = 0; $i < $n; ++$i)
		{
			// Check if element of array -1 is not exist in container
			if (in_array($arr[$i] - 1, $container, TRUE) == false)
			{
				// Count consecutive sequence
				while (in_array($arr[$i] + $j, $container, TRUE) == true)
				{
					// Next consecutive elements
					$j = $j + 1;
				}
				if ($result < $j)
				{
					// Get new result
					$result = $j;
				}
				// reset the value of variable j
				$j = 0;
			}
		}
		// Display calculated result
		echo " Length of longest consecutive subsequence is : ". $result ."\n";
	}
}

function main()
{
	$task = new Subsequence();
	// Define array of integer elements
	$arr = array(7, 1, 7, 9, 2, 8, 6, 11, 15, 5);
	// Get number of element
	$size = count($arr);
	// Test
	// 5,6,7,8,9 (consecutive subsequence)
	$task->longestConsecutive($arr, $size);
}
main();

Output

   7   1   7   9   2   8   6   11   15   5
 Length of longest consecutive subsequence is : 5
/*
    Node Js Program
    Longest consecutive subsequence
*/
class Subsequence
{
	// Function which is display array elements
	display(arr, n)
	{
		for (var i = 0; i < n; ++i)
		{
			process.stdout.write("   " + arr[i]);
		}
		process.stdout.write("\n");
	}
	// Find the length of longest contiguous subsequence in given array
	longestConsecutive(arr, n)
	{
		// Display given array
		this.display(arr, n);
		// Use to collect unique element
		var container = new Set();
		// Define counter variable
		var result = 0;
		// Loop controlling variables
		var i = 0;
		var j = 0;
		// Get unique element in given array
		// iterate the loop through by size
		for (i = 0; i < n; ++i)
		{
			container.add(arr[i]);
		}
		// iterate the loop through by size
		for (i = 0; i < n; ++i)
		{
			// Check if element of array -1 is not exist in container
			if (container.has(arr[i] - 1) == false)
			{
				// Count consecutive sequence
				while (container.has(arr[i] + j) == true)
				{
					// Next consecutive elements
					j = j + 1;
				}
				if (result < j)
				{
					// Get new result
					result = j;
				}
				// reset the value of variable j
				j = 0;
			}
		}
		// Display calculated result
		process.stdout.write(" Length of longest consecutive subsequence is : " + result + "\n");
	}
}

function main()
{
	var task = new Subsequence();
	// Define array of integer elements
	var arr = [7, 1, 7, 9, 2, 8, 6, 11, 15, 5];
	// Get number of element
	var size = arr.length;
	// Test
	// 5,6,7,8,9 (consecutive subsequence)
	task.longestConsecutive(arr, size);
}
main();

Output

   7   1   7   9   2   8   6   11   15   5
 Length of longest consecutive subsequence is : 5
#  Python 3 Program
#  Longest consecutive subsequence

class Subsequence :
	#  Function which is display list elements
	def display(self, arr, n) :
		i = 0
		while (i < n) :
			print("   ", arr[i], end = "")
			i += 1
		
		print(end = "\n")
	
	#  Find the length of longest contiguous subsequence in given list
	def longestConsecutive(self, arr, n) :
		#  Display given list
		self.display(arr, n)
		#  Use to collect unique element
		container = set()
		#  Define counter variable
		result = 0
		#  Loop controlling variables
		i = 0
		j = 0
		#  Get unique element in given list
		#  iterate the loop through by size
		while (i < n) :
			#  collect element
			container.add(arr[i])
			i += 1
		
		i = 0
		#  iterate the loop through by size
		while (i < n) :
			#  Check if element of list -1 is not exist in container
			if ((arr[i] - 1 in container) == False) :
				#  Count consecutive sequence
				while ((arr[i] + j in container) == True) :
					#  Next consecutive elements
					j = j + 1
				
				if (result < j) :
					#  Get new result
					result = j
				
				#  reset the value of variable j
				j = 0
			
			i += 1
		
		#  Display calculated result
		print(" Length of longest consecutive subsequence is : ", result )
	

def main() :
	task = Subsequence()
	#  Define list of integer elements
	arr = [7, 1, 7, 9, 2, 8, 6, 11, 15, 5]
	#  Get number of element
	size = len(arr)
	#  Test
	#  5,6,7,8,9 (consecutive subsequence)
	task.longestConsecutive(arr, size)

if __name__ == "__main__": main()

Output

    7    1    7    9    2    8    6    11    15    5
 Length of longest consecutive subsequence is :  5
#  Ruby Program
#  Longest consecutive subsequence

class Subsequence 
	#  Function which is display array elements
	def display(arr, n) 
		i = 0
		while (i < n) 
			print("   ", arr[i])
			i += 1
		end

		print("\n")
	end

	#  Find the length of longest contiguous subsequence in given array
	def longestConsecutive(arr, n) 
		#  Display given array
		self.display(arr, n)
		#  Use to collect unique element
		container = []
		#  Define counter variable
		result = 0
		#  Loop controlling variables
		i = 0
		j = 0
		#  Get unique element in given array
		#  iterate the loop through by size
		while (i < n) 
          	if((container.include?(arr[i])) == false) 
				container.push(arr[i])
            end
			i += 1
		end

		i = 0
		#  iterate the loop through by size
		while (i < n) 
			#  Check if element of array -1 is not exist in container
			if ((container.include?(arr[i] - 1)) == false) 
				#  Count consecutive sequence
				while ((container.include?(arr[i] + j)) == true) 
					#  Next consecutive elements
					j = j + 1
				end

				if (result < j) 
					#  Get new result
					result = j
				end

				#  reset the value of variable j
				j = 0
			end

			i += 1
		end

		#  Display calculated result
		print(" Length of longest consecutive subsequence is : ", result ,"\n")
	end

end

def main() 
	task = Subsequence.new()
	#  Define array of integer elements
	arr = [7, 1, 7, 9, 2, 8, 6, 11, 15, 5]
	#  Get number of element
	size = arr.length
	#  Test
	#  5,6,7,8,9 (consecutive subsequence)
	task.longestConsecutive(arr, size)
end

main()

Output

   7   1   7   9   2   8   6   11   15   5
 Length of longest consecutive subsequence is : 5
import scala.collection.mutable._;
/*
    Scala Program
    Longest consecutive subsequence
*/
class Subsequence
{
	// Function which is display array elements
	def display(arr: Array[Int], n: Int): Unit = {
		var i: Int = 0;
		while (i < n)
		{
			print("   " + arr(i));
			i += 1;
		}
		print("\n");
	}
	// Find the length of longest contiguous subsequence in given array
	def longestConsecutive(arr: Array[Int], n: Int): Unit = {
		// Display given array
		this.display(arr, n);
		// Use to collect unique element
		var container: Set[Int] = Set();
		// Define counter variable
		var result: Int = 0;
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		// Get unique element in given array
		// iterate the loop through by size
		while (i < n)
		{
			container.add(arr(i));
			i += 1;
		}
		i = 0;
		// iterate the loop through by size
		while (i < n)
		{
			// Check if element of array -1 is not exist in container
			if (container.contains(arr(i) - 1) == false)
			{
				// Count consecutive sequence
				while (container.contains(arr(i) + j) == true)
				{
					// Next consecutive elements
					j = j + 1;
				}
				if (result < j)
				{
					// Get new result
					result = j;
				}
				// reset the value of variable j
				j = 0;
			}
			i += 1;
		}
		// Display calculated result
		print(" Length of longest consecutive subsequence is : " + result + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Subsequence = new Subsequence();
		// Define array of integer elements
		var arr: Array[Int] = Array(7, 1, 7, 9, 2, 8, 6, 11, 15, 5);
		// Get number of element
		var size: Int = arr.length;
		// Test
		// 5,6,7,8,9 (consecutive subsequence)
		task.longestConsecutive(arr, size);
	}
}

Output

   7   1   7   9   2   8   6   11   15   5
 Length of longest consecutive subsequence is : 5
import Foundation
/*
    Swift 4 Program
    Longest consecutive subsequence
*/
class Subsequence
{
	// Function which is display array elements
	func display(_ arr: [Int], _ n: Int)
	{
		var i: Int = 0;
		while (i < n)
		{
			print("   ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	// Find the length of longest contiguous subsequence in given array
	func longestConsecutive(_ arr: [Int], _ n: Int)
	{
		// Display given array
		self.display(arr, n);
		// Use to collect unique element
		var container = Set<Int>();
		// Define counter variable
		var result: Int = 0;
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		// Get unique element in given array
		// iterate the loop through by size
		while (i < n)
		{
			container.insert(arr[i]);
			i += 1;
		}
		i = 0;
		// iterate the loop through by size
		while (i < n)
		{
			// Check if element of array -1 is not exist in container
			if (container.contains(arr[i] - 1) == false)
			{
				// Count consecutive sequence
				while (container.contains(arr[i] + j) == true)
				{
					// Next consecutive elements
					j = j + 1;
				}
				if (result < j)
				{
					// Get new result
					result = j;
				}
				// reset the value of variable j
				j = 0;
			}
			i += 1;
		}
		// Display calculated result
		print(" Length of longest consecutive subsequence is : ", result );
	}
}
func main()
{
	let task: Subsequence = Subsequence();
	// Define array of integer elements
	let arr: [Int] = [7, 1, 7, 9, 2, 8, 6, 11, 15, 5];
	// Get number of element
	let size: Int = arr.count;
	// Test
	// 5,6,7,8,9 (consecutive subsequence)
	task.longestConsecutive(arr, size);
}
main();

Output

    7    1    7    9    2    8    6    11    15    5
 Length of longest consecutive subsequence is :  5
/*
    Kotlin Program
    Longest consecutive subsequence
*/
class Subsequence
{
	// Function which is display array elements
	fun display(arr: Array < Int > , n: Int): Unit
	{
		var i: Int = 0;
		while (i < n)
		{
			print("   " + arr[i]);
			i += 1;
		}
		print("\n");
	}
	// Find the length of longest contiguous subsequence in given array
	fun longestConsecutive(arr: Array < Int > , n: Int): Unit
	{
		// Display given array
		this.display(arr, n);
		// Use to collect unique element
		var container: MutableSet <Int> = mutableSetOf <Int> ();
		// Define counter variable
		var result: Int = 0;
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		// Get unique element in given array
		// iterate the loop through by size
		while (i < n)
		{
			container.add(arr[i]);
			i += 1;
		}
		i = 0;
		// iterate the loop through by size
		while (i < n)
		{
			// Check if element of array -1 is not exist in container
			if (container.contains(arr[i] - 1) == false)
			{
				// Count consecutive sequence
				while (container.contains(arr[i] + j) == true)
				{
					// Next consecutive elements
					j = j + 1;
				}
				if (result < j)
				{
					// Get new result
					result = j;
				}
				// reset the value of variable j
				j = 0;
			}
			i += 1;
		}
		// Display calculated result
		print(" Length of longest consecutive subsequence is : " + result + "\n");
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Subsequence = Subsequence();
	// Define array of integer elements
	var arr: Array < Int > = arrayOf(7, 1, 7, 9, 2, 8, 6, 11, 15, 5);
	// Get number of element
	var size: Int = arr.count();
	// Test
	// 5,6,7,8,9 (consecutive subsequence)
	task.longestConsecutive(arr, size);
}

Output

   7   1   7   9   2   8   6   11   15   5
 Length of longest consecutive subsequence is : 5


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







© 2021, kalkicode.com, All rights reserved