×

GCD of Different SubSequences in Java

The positive numbers are provided in an array called inArr. The aim is to determine the number of distinct GCDs (Greatest Common Divisors) in each subsequence present in the input array. A note subsequence is created by selecting one or more elements at a time from an array.

Brute Force Approach

According to this method, we will compute each subsequence of the input array and store them in an array list. After that compute the GCD for each subsequence contained in the array list, we will group them into sets. The size of the set will be our response because the set only consists of distinct components. Recursion will be used to find the subsequences. Take note of the programme below.

GCDDiff1.java

// importing important required packages
import java . util . ArrayList ;  
import java . util . Set ;  
import java . util . HashSet ;  
 // public class with name GCDDiff1 is created
public class GCDDiff1  
{  
// arraylist with name arrlist is created
ArrayList <ArrayList<Integer>>  arrlist = new ArrayList <ArrayList<Integer>> ( ) ;  
Set<Integer> set = new HashSet<Integer> ( ) ;  
  // public method to finf sub sequence
public void find_sub_seq ( int arr1 [ ] , int size , int i1 , ArrayList < Integer >  tempvalue )   
{  
if ( i1 >= size )  
{  
  


ArrayList < Integer > t1 = new ArrayList < Integer > ( ) ;  
for ( int idx1 = 0 ; idx1 < tempvalue . size ( ) ; idx1++ )  
{  
t1 . add ( tempvalue . get ( idx1 ) ) ;  
}  
arrlist .add( t1 ) ;  
return ;  
}   
tempvalue . add ( arr1 [ i1 ] ) ;  
find_sub_seq ( arr1 , size ,  i1 + 1 , tempvalue ) ;  
int index1 = tempvalue . size() – 1 ;  
tempvalue . remove ( index1 ) ;  
find_sub_seq ( arr1 , size , i1 + 1 , tempvalue ) ;  
}  
  
// public method to find gcd
public int find_GCD ( int num1 ,  int num2 )  
{  
if ( num1 == 0 )  
{  
return num2 ;  
}  
return find_GCD ( num2 % num1 , num1 ) ;  
}  
// public method to find gcd of the subsequences
public void find_GCD_sub_seq ( ArrayList < Integer > arrlist1 )  
{  
// if array size is 0 return 
if ( arrlist1 . size ( )  == 0 )  
{  
return ;  
}  
// if array size is 1 return
if ( arrlist1 . size ( )  == 1 )  
{  
set . add ( arrlist1 . get ( 0 ) ) ;  
return ;  
}  
// assigning two integer variables to first two values of 
int t11 = arrlist1 . get ( 0 ) ;  
int t22 = arrlist1 . get( 1 ) ;  
int tt = find_GCD ( t11 , t22 ) ;  
for ( int i1 = 2; i1 < arrlist1 . size ( ) ; i1++ )  
{  
tt = find_GCD ( tt , arrlist1 . get ( i1 ) ) ;  
}   
Set . add ( tt ) ;  
}  
public void find_GCD_Util()  
{  
int size1 = arrlist . size();  
  
for ( int i1 = 0 ; i1 < size1 ; i1++ )  
{  
find_GCD_sub_seq ( arrlist . get ( i1 ) ) ;  
}  
}
// main section where execution of the program starts
public static void main ( String args [ ] )  
{  


GCDDiff1 object1 = new GCDDiff1 ( ) ;  
int inarr [ ] = {4 , 6 , 8 } ;  
int size = inarr . length ;  
ArrayList < Integer > tempvalue = new ArrayList < Integer > ( ) ;  
object1 . find_sub_seq ( inarr , size , 0 , tempvalue ) ;  
object1 . find_GCD_Util ( ) ;  
System . out . println ( " For the input array: " ) ;  
for ( int i1 = 0 ; i1 < size ; i1++ )  
{  
System . out . print ( inarr [ i1 ] + "  " ) ;  
}  
System . out . println ( ) ;  
System . out . println ( " The total number of unique GCDs is: " + object1 . set . size ( ) ) ;  
System . out . println ( ) ;  
object1 . set = new HashSet < Integer > ( ) ;  
object1 . arrlist = new ArrayList < ArrayList < Integer > > ( ) ;  
 
int inarr1 [ ]  = {3 , 6 , 9 } ;  
size = inarr1 . length ;  
tempvalue = new ArrayList < Integer > ( ) ;  
object1 . find_sub_seq ( inarr1 , size , 0 , tempvalue ) ;  
object1 . find_GCD_Util ( ) ;  
System . out . println ( " For the input array: " ) ;  
for( int i1 = 0; i1 < size; i1++ )  
{  
System . out . print ( inarr1 [ i1 ] + " " ) ;  
}  
System . out . println ( ) ;  
System . out . println ( " The total number of unique GCDs is: " + object1 . set . size ( ) ) ;  
}  
}  

Output

GCD of Different SubSequences in Java

Effective Approach:

We are able to perform the optimization using the greedy method. The GCD of any two numbers, n1 and n2, must lie between 1 and a maximum of ( n1 , n2 ) , and the same principle holds true for any sequence with more than two members. The GCD will always fall between [ 1 , M ] if the highest element in a subsequence is M. As a result, if we iterate between 1 and M, and any integer in the range is a factor of one of the array's elements, we can display that element as one of the GCD's results.

GCDDiff2.java

import java . util . ArrayList ;  
import java . util . Set ;  
import java . util . HashSet ;  
// public class 
public class GCDDiff2   
{  
public int find_GCD ( int num1 , int num2 )  
{  
// if num1 is 0 return num2
if ( num1 == 0 )  
{  
return num2 ;  
}  
return find_GCD ( num2 % num1, num1 ) ;  
}  
public int find_GCDs_Sub_seq ( ArrayList < Integer > arrList )  
{ 
ArrayList < Integer > ans = new ArrayList < Integer > ( ) ;  


HashSet < Integer > hs1 = new HashSet < Integer > ( ) ;  
for ( int i1 = 0 ; i1 < arrList . size ( ) ; i1++ )  
{  
hs1 . add ( arrList . get ( i1 ) ) ;  
}  
  
int Maximum = Integer . MIN_VALUE ;  
for ( int i1 = 0; i1 < arrList . size ( ) ; i1++ )  
{  
if ( arrList . get ( i1 ) > Maximum )  
{  
Maximum = arrList . get ( i1 ) ;  
}  
}     
for ( int j1 = 1 ; j1 <= Maximum ; j1++ )  
{  
int compute_GCD = 0 ;  
for ( int k1 = j1 ; k1 < Maximum + 1 ; k1 += j1 )  
{  
if  ( hs1 . contains ( k1 ) )   
{  
compute_GCD = find_GCD (compute_GCD , k1 ) ;  
}  
}  
if ( compute_GCD == j1 )  
{  
ans . add ( j1 ) ;  
}  
}  
return ans . size ( ) ;  
} 
// main section of the program where execution starts
public static void main ( String argvs [ ] )  
{  
// creating object to class
GCDDiff2 object1 = new GCDDiff2 ( ) ;  
ArrayList < Integer > arrList1 = new ArrayList < Integer > ( ) ;  
arrList1 . add ( 4 ) ;  
arrList1 . add ( 6 ) ;  
arrList1 . add ( 8 ) ;  
int size = arrList1 . size ( ) ;  
int ans = object1 . find_GCDs_Sub_seq ( arrList1 ) ;  
  
System . out . println( " For the input array: " ) ;  
for ( int i1 = 0 ; i1 < size ; i1++ )  
{  
System . out . print ( arrList1 . get ( i1 ) + " " ) ;  
}  
System . out . println ( ) ;  
System . out . println ( " The total number of unique GCDs is: " + ans ) ;  
System . out . println ( ) ;  
ArrayList < Integer > arrList2 = new ArrayList < Integer > ( ) ;  
arrList2 . add ( 3 ) ;  
arrList2 . add ( 6 ) ;  
arrList2 . add ( 9 ) ;  
size = arrList2 . size ( ) ;  
ans = object1 . find_GCDs_Sub_seq ( arrList2 ) ;  
System . out . println ( " For the input array: " ) ;  
for( int i1 = 0 ; i1 < size ; i1++ )  
{  
System . out . print ( arrList2 . get ( i1 ) + " " ) ;  
}  
System . out . println ( ) ;  
System . out . println ( " The total number of unique GCDs is: " + ans ) ;  
}  
}  

Output

GCD of Different SubSequences in Java

Related Topics

Interchange Diagonal elements in Java

In this article, you will be very well acknowledged about what is a matrix with an example. You will also be acknowledged about how to interchange the diagonal elements in...

4 minutes read.

Java String length() method

Java String length() method returns length of the characters in a String. Syntax: public int length() Returns It returns length of the characters Java String length() method example 1          public class JavaStringLengthEx1  ...

1 minute read.

Java logger

Logging is a crucial Java feature that aids developers in identifying issues. The logging methodology is included with Java as the programming language. It offers the Logging API, which debuted...

7 minutes read.

Java program to count the occurrences of each character

The count of each character is the frequency of the characters. For example, the given String is “Programming”. In the given string, the count of the characters ‘P’ -1, ’r’-2,...

6 minutes read.

Tetranacci Number in Java

This article mainly describes tetranacci number identification and the Java Program for Tetranacci numbers. Tetranacci number Tetranacci numbers and Fibonacci numbers are related. The key contrast is that a Tetranacci number depends...

3 minutes read.

Convert JSON to Map in Java

JACKSON and GSON libraries are two extremely capable JSON-related libraries offered by Java. To efficiently work with the returned JSON data, we frequently need to convert JSON responses into a...

6 minutes read.

Armstrong Number Program in Java

Armstrong Number Program in Java: A positive number is called an Armstrong number if the sum of the cube of each digit is equal to the number itself. There are...

6 minutes read.

Difference between JIT and JVM in Java

In this tutorial, we will discuss the difference between JIT (Just In Time Compiler) and JVM (Java Virtual Machine) in Java. Before we move to the differences, let's understand what...

4 minutes read.

String Pool in Java

String Pool in Java: String is one of the most important discussed topics in Java. There are a lot of concepts related to the String and one of them is...

5 minutes read.

How to convert double to String in Java

How to Convert double to String in Java It is used when we want to convert double primitive to String type. There are two methods to convert double to String. Using String.valueOf()...

2 minutes read.

What is anagram in Java?

In this section will explain what an anagram is in Java and demonstrate how to determine whether or not a text is an anagram. In Java interviews, the anagram Java...

4 minutes read.

Class definition in Java

The class definition in Java Java is an object-oriented programming language. We essentially know that programming languages based on object-oriented paradigms have classes and objects in their concepts as main, which...

6 minutes read.

Java Boolean Keyword

Boolean keyword In java programming language we basically have two types of primitive data types, Boolean and Numeric (integer and floating-point data types). In this article we are going to learn...

4 minutes read.

Buffer reader to read string in Java

The Buffered Reader class of Java is used to read the stream of characters from the input stream. Program to read string using Buffer reader import java.io.*; class  Demo {   public static void main(String...

3 minutes read.

Stack vs Heap in Java

In Java, whenever we declare an object or create a variable, whether it is an instance variable, local variable, or static variable, a certain memory is used to store the...

4 minutes read.

Statements in java

What is Statement in java: A statement in java is an instruction that explains what will happen based on the condition. Types of java statements: There are different statements in java Expression statementDeclaration statementControl...

2 minutes read.

How to Compare Two Strings in Java

How to Compare Two Strings in Java On the basis of reference or content, one can compare two strings in Java. String comparison is used in reference matching (== operator), sorting...

4 minutes read.

Menu Driven Program in Java

Menu Driven Program in Java The menu-driven program in Java is a program that displays a menu and then takes input from the user to choose an option from the displayed...

3 minutes read.

Cast Operator in Java

A cast is a unique operator that completely converts one type of data into another. Casts are unary operators and have the same priority as other unary operators. Type casting in...

3 minutes read.

Iccanobif Numbers in Java

In this article, you will be very well equipped with the knowledge of iccanobif numbers in Java. You will also know how they are formed and basic example programs on...

3 minutes read.