Generating all sub-sequence of an array of limited size

Here given code implementation process.

// Java program for
// Generating all sub-sequence of an array of limited size
import java.util.ArrayList;

public class SubSequence
{
    public void printSubArray(ArrayList < Integer > record)
    {
        // Print record elements
        for (int i = 0; i < record.size(); i++)
        {
            System.out.print(" " + record.get(i));
        }
    
    }
    public void findSubArray(int []arr, int n)
    {

        // Use to collect information of sub-sequence
        ArrayList < Integer > record = new ArrayList < Integer > ();

        for (int i = 1; i < (1 << n); i++)
        {
            // This loop are generating all sub-sequence
            for (int j = 0; j < n; j++)
            {
                if ((i & (1 << j)) > 0)
                {
                    // Add sub-sequence element
                    record.add(arr[j]);
                }
            }
            // Display sub array element
            printSubArray(record);

            // Add new line
            System.out.print("\n");

            // Remove all record element
            record.clear();
        }
        System.out.print("\n");
    }
    public static void main(String[] args)
    {
        SubSequence task = new SubSequence();

        int []arr1 = 
        {
            1 , 2 , 3 , 6
        };
        int []arr2 = 
        {
            7 , 8 , 1 , 6, 4
        };

        // Test Case A
        int n = arr1.length;
        task.findSubArray(arr1, n);

        // Test Case B
        n = arr2.length;
        task.findSubArray(arr2, n);
    }
}

input

 1
 2
 1 2
 3
 1 3
 2 3
 1 2 3
 6
 1 6
 2 6
 1 2 6
 3 6
 1 3 6
 2 3 6
 1 2 3 6

 7
 8
 7 8
 1
 7 1
 8 1
 7 8 1
 6
 7 6
 8 6
 7 8 6
 1 6
 7 1 6
 8 1 6
 7 8 1 6
 4
 7 4
 8 4
 7 8 4
 1 4
 7 1 4
 8 1 4
 7 8 1 4
 6 4
 7 6 4
 8 6 4
 7 8 6 4
 1 6 4
 7 1 6 4
 8 1 6 4
 7 8 1 6 4
// Include header file
#include <iostream>
#include <vector>
using namespace std;

// C++ program for
// Generating all sub-sequence of an array of limited size

class SubSequence
{
	public: void printSubArray(vector < int > record)
	{
		// Print record elements
		for (int i = 0; i < record.size(); i++)
		{
			cout << " " << record.at(i);
		}
	}
	void findSubArray(int arr[], int n)
	{
		// Use to collect information of sub-sequence
		vector < int > record ;
		for (int i = 1; i < (1 << n); i++)
		{
			// This loop are generating all sub-sequence
			for (int j = 0; j < n; j++)
			{
				if ((i &(1 << j)) > 0)
				{
					// Add sub-sequence element
					record.push_back(arr[j]);
				}
			}
			// Display sub array element
			this->printSubArray(record);
			// Add new line
			cout << "\n";
			// Remove all record element
			record.clear();
		}
		cout << "\n";
	}
};
int main()
{
	SubSequence *task = new SubSequence();
	int arr1[] = {
		1 , 2 , 3 , 6
	};
	int arr2[] = {
		7 , 8 , 1 , 6 , 4
	};
	// Test Case A
	int n = sizeof(arr1) / sizeof(arr1[0]);
	task->findSubArray(arr1, n);
	// Test Case B
	n = sizeof(arr2) / sizeof(arr2[0]);
	task->findSubArray(arr2, n);
	return 0;
}

input

 1
 2
 1 2
 3
 1 3
 2 3
 1 2 3
 6
 1 6
 2 6
 1 2 6
 3 6
 1 3 6
 2 3 6
 1 2 3 6

 7
 8
 7 8
 1
 7 1
 8 1
 7 8 1
 6
 7 6
 8 6
 7 8 6
 1 6
 7 1 6
 8 1 6
 7 8 1 6
 4
 7 4
 8 4
 7 8 4
 1 4
 7 1 4
 8 1 4
 7 8 1 4
 6 4
 7 6 4
 8 6 4
 7 8 6 4
 1 6 4
 7 1 6 4
 8 1 6 4
 7 8 1 6 4
// Include namespace system
using System;
using System.Collections.Generic;
// Csharp program for
// Generating all sub-sequence of an array of limited size
public class SubSequence
{
	public void printSubArray(List < int > record)
	{
		// Print record elements
		for (int i = 0; i < record.Count; i++)
		{
			Console.Write(" " + record[i]);
		}
	}
	public void findSubArray(int[] arr, int n)
	{
		// Use to collect information of sub-sequence
		List < int > record = new List < int > ();
		for (int i = 1; i < (1 << n); i++)
		{
			// This loop are generating all sub-sequence
			for (int j = 0; j < n; j++)
			{
				if ((i & (1 << j)) > 0)
				{
					// Add sub-sequence element
					record.Add(arr[j]);
				}
			}
			// Display sub array element
			this.printSubArray(record);
			// Add new line
			Console.Write("\n");
			// Remove all record element
			record.Clear();
		}
		Console.Write("\n");
	}
	public static void Main(String[] args)
	{
		SubSequence task = new SubSequence();
		int[] arr1 = {
			1 , 2 , 3 , 6
		};
		int[] arr2 = {
			7 , 8 , 1 , 6 , 4
		};
		// Test Case A
		int n = arr1.Length;
		task.findSubArray(arr1, n);
		// Test Case B
		n = arr2.Length;
		task.findSubArray(arr2, n);
	}
}

input

 1
 2
 1 2
 3
 1 3
 2 3
 1 2 3
 6
 1 6
 2 6
 1 2 6
 3 6
 1 3 6
 2 3 6
 1 2 3 6

 7
 8
 7 8
 1
 7 1
 8 1
 7 8 1
 6
 7 6
 8 6
 7 8 6
 1 6
 7 1 6
 8 1 6
 7 8 1 6
 4
 7 4
 8 4
 7 8 4
 1 4
 7 1 4
 8 1 4
 7 8 1 4
 6 4
 7 6 4
 8 6 4
 7 8 6 4
 1 6 4
 7 1 6 4
 8 1 6 4
 7 8 1 6 4
<?php
// Php program for
// Generating all sub-sequence of an array of limited size
class SubSequence
{
	public	function printSubArray($record)
	{
		// Print record elements
		for ($i = 0; $i < count($record); $i++)
		{
			echo(" ".$record[$i]);
		}
	}
	public	function findSubArray($arr, $n)
	{
		// Use to collect information of sub-sequence
		$record = array();
		for ($i = 1; $i < (1 << $n); $i++)
		{
			// This loop are generating all sub-sequence
			for ($j = 0; $j < $n; $j++)
			{
				if (($i & (1 << $j)) > 0)
				{
					// Add sub-sequence element
					$record[] = $arr[$j];
				}
			}
			// Display sub array element
			$this->printSubArray($record);
			// Add new line
			echo("\n");
			// Remove all record element
			$record = array();
		}
		echo("\n");
	}
}

function main()
{
	$task = new SubSequence();
	$arr1 = array(1, 2, 3, 6);
	$arr2 = array(7, 8, 1, 6, 4);
	// Test Case A
	$n = count($arr1);
	$task->findSubArray($arr1, $n);
	// Test Case B
	$n = count($arr2);
	$task->findSubArray($arr2, $n);
}
main();

input

 1
 2
 1 2
 3
 1 3
 2 3
 1 2 3
 6
 1 6
 2 6
 1 2 6
 3 6
 1 3 6
 2 3 6
 1 2 3 6

 7
 8
 7 8
 1
 7 1
 8 1
 7 8 1
 6
 7 6
 8 6
 7 8 6
 1 6
 7 1 6
 8 1 6
 7 8 1 6
 4
 7 4
 8 4
 7 8 4
 1 4
 7 1 4
 8 1 4
 7 8 1 4
 6 4
 7 6 4
 8 6 4
 7 8 6 4
 1 6 4
 7 1 6 4
 8 1 6 4
 7 8 1 6 4
// Node JS program for
// Generating all sub-sequence of an array of limited size
class SubSequence
{
	printSubArray(record)
	{
		// Print record elements
		for (var i = 0; i < record.length; i++)
		{
			process.stdout.write(" " + record[i]);
		}
	}
	findSubArray(arr, n)
	{
		// Use to collect information of sub-sequence
		var record = [];
		for (var i = 1; i < (1 << n); i++)
		{
			// This loop are generating all sub-sequence
			for (var j = 0; j < n; j++)
			{
				if ((i & (1 << j)) > 0)
				{
					// Add sub-sequence element
					record.push(arr[j]);
				}
			}
			// Display sub array element
			this.printSubArray(record);
			// Add new line
			process.stdout.write("\n");
			// Remove all record element
			record = [];
		}
		process.stdout.write("\n");
	}
}

function main()
{
	var task = new SubSequence();
	var arr1 = [1, 2, 3, 6];
	var arr2 = [7, 8, 1, 6, 4];
	// Test Case A
	var n = arr1.length;
	task.findSubArray(arr1, n);
	// Test Case B
	n = arr2.length;
	task.findSubArray(arr2, n);
}
main();

input

 1
 2
 1 2
 3
 1 3
 2 3
 1 2 3
 6
 1 6
 2 6
 1 2 6
 3 6
 1 3 6
 2 3 6
 1 2 3 6

 7
 8
 7 8
 1
 7 1
 8 1
 7 8 1
 6
 7 6
 8 6
 7 8 6
 1 6
 7 1 6
 8 1 6
 7 8 1 6
 4
 7 4
 8 4
 7 8 4
 1 4
 7 1 4
 8 1 4
 7 8 1 4
 6 4
 7 6 4
 8 6 4
 7 8 6 4
 1 6 4
 7 1 6 4
 8 1 6 4
 7 8 1 6 4
#  Python 3 program for
#  Generating all sub-sequence of an array of limited size
class SubSequence :
	def printSubArray(self, record) :
		#  Print record elements
		i = 0
		while (i < len(record)) :
			print(" ", record[i], end = "")
			i += 1
		
	
	def findSubArray(self, arr, n) :
		#  Use to collect information of sublist
		record = []
		i = 1
		while (i < (1 << n)) :
			
			j = 0
			#  This loop are generating all sublists
			while (j < n) :
				if ((i & (1 << j)) > 0) :
					#  Add sublist element
					record.append(arr[j])
				
				j += 1
			
			#  Display sub list element
			self.printSubArray(record)
			#  Add new line
			print(end = "\n")
			#  Remove all record element
			record.clear()
			i += 1
		
		print(end = "\n")
	

def main() :
	task = SubSequence()
	arr1 = [1, 2, 3, 6]
	arr2 = [7, 8, 1, 6, 4]
	#  Test Case A
	n = len(arr1)
	task.findSubArray(arr1, n)
	#  Test Case B
	n = len(arr2)
	task.findSubArray(arr2, n)

if __name__ == "__main__": main()

input

  1
  2
  1  2
  3
  1  3
  2  3
  1  2  3
  6
  1  6
  2  6
  1  2  6
  3  6
  1  3  6
  2  3  6
  1  2  3  6

  7
  8
  7  8
  1
  7  1
  8  1
  7  8  1
  6
  7  6
  8  6
  7  8  6
  1  6
  7  1  6
  8  1  6
  7  8  1  6
  4
  7  4
  8  4
  7  8  4
  1  4
  7  1  4
  8  1  4
  7  8  1  4
  6  4
  7  6  4
  8  6  4
  7  8  6  4
  1  6  4
  7  1  6  4
  8  1  6  4
  7  8  1  6  4
#  Ruby program for
#  Generating all sub-sequence of an array of limited size
class SubSequence 
	def printSubArray(record) 
		i = 0
		#  Print record elements
		while (i < record.length) 
			print(" ", record[i])
			i += 1
		end

	end

	def findSubArray(arr, n) 
		#  Use to collect information of sub-sequence
		record = []
		i = 1
		while (i < (1 << n)) 
			#  This loop are generating all sub-sequence
			j = 0
			while (j < n) 
				if ((i & (1 << j)) > 0) 
					#  Add sub-sequence element
					record.push(arr[j])
				end

				j += 1
			end

			#  Display sub array element
			self.printSubArray(record)
			#  Add new line
			print("\n")
			#  Remove all record element
			record.clear()
			i += 1
		end

		print("\n")
	end

end

def main() 
	task = SubSequence.new()
	arr1 = [1, 2, 3, 6]
	arr2 = [7, 8, 1, 6, 4]
	#  Test Case A
	n = arr1.length
	task.findSubArray(arr1, n)
	#  Test Case B
	n = arr2.length
	task.findSubArray(arr2, n)
end

main()

input

 1
 2
 1 2
 3
 1 3
 2 3
 1 2 3
 6
 1 6
 2 6
 1 2 6
 3 6
 1 3 6
 2 3 6
 1 2 3 6

 7
 8
 7 8
 1
 7 1
 8 1
 7 8 1
 6
 7 6
 8 6
 7 8 6
 1 6
 7 1 6
 8 1 6
 7 8 1 6
 4
 7 4
 8 4
 7 8 4
 1 4
 7 1 4
 8 1 4
 7 8 1 4
 6 4
 7 6 4
 8 6 4
 7 8 6 4
 1 6 4
 7 1 6 4
 8 1 6 4
 7 8 1 6 4

import scala.collection.mutable._;
// Scala program for
// Generating all sub-sequence of an array of limited size
class SubSequence()
{
	def printSubArray(record: ArrayBuffer[Int]): Unit = {
	
		var i: Int = 0;
      	// Print record elements
		while (i < record.size)
		{
			print(" " + record(i));
			i += 1;
		}
	}
	def findSubArray(arr: Array[Int], n: Int): Unit = {
		// Use to collect information of sub-sequence
		var record: ArrayBuffer[Int] = new ArrayBuffer[Int]();
		var i: Int = 1;
		while (i < (1 << n))
		{
			var j: Int = 0;
          	// This loop are generating all sub-sequence
			while (j < n)
			{
				if ((i & (1 << j)) > 0)
				{
					// Add sub-sequence element
					record += arr(j);
				}
				j += 1;
			}
			// Display sub array element
			printSubArray(record);
			// Add new line
			print("\n");
			// Remove all record element
			record.clear();
			i += 1;
		}
		print("\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: SubSequence = new SubSequence();
		var arr1: Array[Int] = Array(1, 2, 3, 6);
		var arr2: Array[Int] = Array(7, 8, 1, 6, 4);
		// Test Case A
		var n: Int = arr1.length;
		task.findSubArray(arr1, n);
		// Test Case B
		n = arr2.length;
		task.findSubArray(arr2, n);
	}
}

input

 1
 2
 1 2
 3
 1 3
 2 3
 1 2 3
 6
 1 6
 2 6
 1 2 6
 3 6
 1 3 6
 2 3 6
 1 2 3 6

 7
 8
 7 8
 1
 7 1
 8 1
 7 8 1
 6
 7 6
 8 6
 7 8 6
 1 6
 7 1 6
 8 1 6
 7 8 1 6
 4
 7 4
 8 4
 7 8 4
 1 4
 7 1 4
 8 1 4
 7 8 1 4
 6 4
 7 6 4
 8 6 4
 7 8 6 4
 1 6 4
 7 1 6 4
 8 1 6 4
 7 8 1 6 4
import Foundation;
// Swift 4 program for
// Generating all sub-sequence of an array of limited size
class SubSequence
{
	func printSubArray(_ record: [Int])
	{
		// Print record elements
		var i = 0;
		while (i < record.count)
		{
			print(" ", record[i], terminator: "");
			i += 1;
		}
	}
	func findSubArray(_ arr: [Int], _ n: Int)
	{
		// Use to collect information of sub-sequence
		var record = [Int]();
		var i = 1;
		while (i < (1 << n))
		{
			
			var j = 0;
          	// This loop are generating all sub-sequence
			while (j < n)
			{
				if ((i & (1 << j)) > 0)
				{
					// Add sub-sequence element
					record.append(arr[j]);
				}
				j += 1;
			}
			// Display sub array element
			self.printSubArray(record);
			// Add new line
			print(terminator: "\n");
			// Remove all record element
			record.removeAll();
			i += 1;
		}
		print(terminator: "\n");
	}
}
func main()
{
	let task = SubSequence();
	let arr1 = [1, 2, 3, 6];
	let arr2 = [7, 8, 1, 6, 4];
	// Test Case A
	var n = arr1.count;
	task.findSubArray(arr1, n);
	// Test Case B
	n = arr2.count;
	task.findSubArray(arr2, n);
}
main();

input

  1
  2
  1  2
  3
  1  3
  2  3
  1  2  3
  6
  1  6
  2  6
  1  2  6
  3  6
  1  3  6
  2  3  6
  1  2  3  6

  7
  8
  7  8
  1
  7  1
  8  1
  7  8  1
  6
  7  6
  8  6
  7  8  6
  1  6
  7  1  6
  8  1  6
  7  8  1  6
  4
  7  4
  8  4
  7  8  4
  1  4
  7  1  4
  8  1  4
  7  8  1  4
  6  4
  7  6  4
  8  6  4
  7  8  6  4
  1  6  4
  7  1  6  4
  8  1  6  4
  7  8  1  6  4
// Kotlin program for
// Generating all sub-sequence of an array of limited size
class SubSequence
{
	fun printSubArray(record: MutableList < Int >  ): Unit
	{
		// Print record elements
		var i: Int = 0;
		while (i < record.size)
		{
			print(" " + record[i]);
			i += 1;
		}
	}
	fun findSubArray(arr: Array < Int > , n: Int): Unit
	{
		// Use to collect information of sub-sequence
		val record: MutableList < Int > = mutableListOf < Int > ();
		var i: Int = 1;
		while (i < (1 shl n))
		{
			// This loop are generating all sub-sequence
			var j: Int = 0;
			while (j < n)
			{
				if ((i and (1 shl j)) > 0)
				{
					// Add sub-sequence element
					record.add(arr[j]);
				}
				j += 1;
			}
			// Display sub array element
			this.printSubArray(record);
			// Add new line
			print("\n");
			// Remove all record element
			record.clear();
			i += 1;
		}
		print("\n");
	}
}
fun main(args: Array < String > ): Unit
{
	val task: SubSequence = SubSequence();
	val arr1: Array < Int > = arrayOf(1, 2, 3, 6);
	val arr2: Array < Int > = arrayOf(7, 8, 1, 6, 4);
	// Test Case A
	var n: Int = arr1.count();
	task.findSubArray(arr1, n);
	// Test Case B
	n = arr2.count();
	task.findSubArray(arr2, n);
}

input

 1
 2
 1 2
 3
 1 3
 2 3
 1 2 3
 6
 1 6
 2 6
 1 2 6
 3 6
 1 3 6
 2 3 6
 1 2 3 6

 7
 8
 7 8
 1
 7 1
 8 1
 7 8 1
 6
 7 6
 8 6
 7 8 6
 1 6
 7 1 6
 8 1 6
 7 8 1 6
 4
 7 4
 8 4
 7 8 4
 1 4
 7 1 4
 8 1 4
 7 8 1 4
 6 4
 7 6 4
 8 6 4
 7 8 6 4
 1 6 4
 7 1 6 4
 8 1 6 4
 7 8 1 6 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







© 2021, kalkicode.com, All rights reserved