codeparrot/xlcost-text-to-code huggingface dataset:
Sum of absolute differences of indices of occurrences of each array element | Set 2 | Java program for the above approach ; Stores the count of occurrences and previous index of every element ; Constructor ; Function to calculate the sum of absolute differences of indices of occurrences of array element ; Stores the count of elements and their previous indices ; Initialize 2 arrays left [ ] and right [ ] of size N ; Traverse the given array ; If arr [ i ] is present in the Map ; Update left [ i ] to 0 and update the value of arr [ i ] in map ; Otherwise , get the value from the map and update left [ i ] ; Clear the map to calculate right [ ] array ; Traverse the array arr [ ] in reverse ; If arr [ i ] is present in theMap ; Update right [ i ] to 0 and update the value of arr [ i ] in the Map ; Otherwise get the value from the map and update right [ i ] ; Iterate in the range [ 0 , N - 1 ] and print the sum of left [ i ] and right [ i ] as the result ; Driver Code
import java . util . * ; class GFG { static class pair { int count , prevIndex ; pair ( int count , int prevIndex ) { this . count = count ; this . prevIndex = prevIndex ; } } static void findSum ( int [ ] arr , int n ) { Map < Integer , pair > map = new HashMap < > ( ) ; int [ ] left = new int [ n ] ; int [ ] right = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! map . containsKey ( arr [ i ] ) ) { left [ i ] = 0 ; map . put ( arr [ i ] , new pair ( 1 , i ) ) ; } else { pair tmp = map . get ( arr [ i ] ) ; left [ i ] = ( tmp . count ) * ( i - tmp . prevIndex ) + left [ tmp . prevIndex ] ; map . put ( arr [ i ] , new pair ( tmp . count + 1 , i ) ) ; } } map . clear ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( ! map . containsKey ( arr [ i ] ) ) { right [ i ] = 0 ; map . put ( arr [ i ] , new pair ( 1 , i ) ) ; } else { pair tmp = map . get ( arr [ i ] ) ; right [ i ] = ( tmp . count ) * ( Math . abs ( i - tmp . prevIndex ) ) + right [ tmp . prevIndex ] ; map . put ( arr [ i ] , new pair ( tmp . count + 1 , i ) ) ; } } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( left [ i ] + right [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 1 , 1 , 2 } ; int N = arr . length ; findSum ( arr , N ) ; } }
Convert a number to another by dividing by its factor or removing first occurrence of a digit from an array | Java program for the above approach ; Function to check if a digit x is present in the number N or not ; Convert N to string ; Traverse the string num ; Return first occurrence of the digit x ; Function to remove the character at a given index from the number ; Convert N to string ; Store the resultant string ; Traverse the string num ; If the number becomes empty after deletion , then return - 1 ; Return the number ; Function to check if A can be reduced to B by performing the operations any number of times ; Create a queue ; Push A into the queue ; Hashmap to check if the element is present in the Queue or not ; Set A as visited ; Iterate while the queue is not empty ; Store the front value of the queue and pop it from it ; If top is equal to B , then return true ; Traverse the array , D [ ] ; Divide top by D [ i ] if it is possible and push the result in q ; If D [ i ] is present at the top ; Remove the first occurrence of D [ i ] from the top and store the new number ; Push newElement into the queue q ; Return false if A can not be reduced to B ; Driver code
import java . util . * ; class GFG { static int isPresent ( int n , int x ) { String num = String . valueOf ( n ) ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) { if ( ( num . charAt ( i ) - '0' ) == x ) return i ; } return - 1 ; } static int removeDigit ( int n , int index ) { String num = String . valueOf ( n ) ; String ans = " " ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) { if ( i != index ) ans += num . charAt ( i ) ; } if ( ans == " " || ( ans . length ( ) == 1 && ans . charAt ( 0 ) == '0' ) ) return - 1 ; int x = Integer . valueOf ( ans ) ; return x ; } static boolean reduceNtoX ( int a , int b , int d [ ] , int n ) { Queue < Integer > q = new LinkedList < > ( ) ; q . add ( a ) ; Map < Integer , Boolean > visited = new HashMap < > ( ) ; visited . put ( a , true ) ; while ( ! q . isEmpty ( ) ) { int top = q . peek ( ) ; q . poll ( ) ; if ( top < 0 ) continue ; if ( top == b ) return true ; for ( int i = 0 ; i < n ; i ++ ) { if ( d [ i ] != 0 && top % d [ i ] == 0 && ! visited . getOrDefault ( top / d [ i ] , false ) ) { q . add ( top / d [ i ] ) ; visited . put ( top / d [ i ] , true ) ; } int index = isPresent ( top , d [ i ] ) ; if ( index != - 1 ) { int newElement = removeDigit ( top , index ) ; if ( newElement != - 1 && ( ! visited . getOrDefault ( newElement , false ) ) ) { q . add ( newElement ) ; visited . put ( newElement , true ) ; } } } } return false ; } public static void main ( String [ ] args ) { int A = 5643 , B = 81 ; int D [ ] = { 3 , 8 , 1 } ; int N = D . length ; if ( reduceNtoX ( A , B , D , N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Prime triplets consisting of values up to N having difference between two elements equal to the third | Java program for the above approach ; Stores 1 and 0 at indices which are prime and non - prime respectively ; Function to find all prime numbers from the range [ 0 , N ] ; Iterate over the range [ 2 , sqrt ( N ) ] ; If p is a prime ; Update all tultiples of p as false ; Function to find all prime triplets satisfying the given conditions ; Generate all primes up to N ; Stores the triplets ; Iterate over the range [ 3 , N ] ; Check for the condition ; Store the triplets ; Print all the stored triplets ; Driver Code
import java . util . * ; class GFG { static boolean [ ] prime = new boolean [ 100000 ] ; static void initialize ( ) { for ( int i = 0 ; i < 100000 ; i ++ ) prime [ i ] = true ; } static void SieveOfEratosthenes ( int n ) { for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) { prime [ i ] = false ; } } } } static void findTriplets ( int N ) { SieveOfEratosthenes ( N ) ; ArrayList < ArrayList < Integer > > V = new ArrayList < ArrayList < Integer > > ( ) ; for ( int i = 3 ; i <= N ; i ++ ) { if ( 2 + i <= N && prime [ i ] && prime [ 2 + i ] ) { ArrayList < Integer > a1 = new ArrayList < Integer > ( ) ; a1 . add ( 2 ) ; a1 . add ( i ) ; a1 . add ( i + 2 ) ; V . add ( a1 ) ; } } for ( int i = 0 ; i < V . size ( ) ; i ++ ) { System . out . println ( V . get ( i ) . get ( 0 ) + " ▁ " + V . get ( i ) . get ( 1 ) + " ▁ " + V . get ( i ) . get ( 2 ) ) ; } } public static void main ( String args [ ] ) { initialize ( ) ; int N = 8 ; findTriplets ( N ) ; } }
Smallest number which is not coprime with any element of an array | Java program for the above approach ; Function check if a number is prime or not ; Corner cases ; Check if n is divisible by 2 or 3 ; Check for every 6 th number . The above checking allows to skip middle 5 numbers ; Function to store primes in an array ; Function to calculate GCD of two numbers ; Function to find the smallest number which is not coprime with any element of the array arr [ ] ; Function call to fill the prime numbers ; Stores the answer ; Generate all non - empty subsets of the primes [ ] array ; Stores product of the primes ; Checks if temp is coprime with the array or not ; Check if the product temp is not coprime with the whole array ; If the product is not co - prime with the array ; Prlong the answer ; Driver code ; Given array ; Stores the size of the array
import java . util . * ; class GFG { static long MAX = 50 ; static boolean isPrime ( long n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( long i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static void findPrime ( ArrayList < Long > primes ) { for ( long i = 2 ; i <= MAX ; i ++ ) { if ( isPrime ( i ) ) primes . add ( i ) ; } } static long gcd ( long a , long b ) { if ( b == 0 ) return a ; else return gcd ( b , a % b ) ; } static void findMinimumNumber ( long [ ] arr , long N ) { ArrayList < Long > primes = new ArrayList < Long > ( ) ; findPrime ( primes ) ; long ans = 2147483647 ; int n = primes . size ( ) ; for ( int i = 1 ; i < ( 1 << n ) ; i ++ ) { long temp = 1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) > 0 ) { temp *= primes . get ( j ) ; } } boolean check = true ; for ( long k = 0 ; k < N ; k ++ ) { if ( gcd ( temp , arr [ ( int ) k ] ) == 1l ) { check = false ; break ; } } if ( check == true ) ans = Math . min ( ans , temp ) ; } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { long [ ] arr = { 3 , 4 , 6 , 7 , 8 , 9 , 10 } ; long N = arr . length ; findMinimumNumber ( arr , N ) ; } }
Count numbers from a given range that can be expressed as sum of digits raised to the power of count of digits | Java program for the above approach ; Function to check if a number N can be expressed as sum of its digits raised to the power of the count of digits ; Stores the number of digits ; Stores the resultant number ; Return true if both the numbers are same ; Function to precompute and store for all numbers whether they can be expressed ; Mark all the index which are plus perfect number ; If true , then update the value at this index ; Compute prefix sum of the array ; Function to count array elements that can be expressed as the sum of digits raised to the power of count of digits ; Precompute the results ; Traverse the queries ; Print the resultant count ; Driver Code ; Function call
import java . util . * ; import java . io . * ; class GFG { static int R = 100005 ; static int [ ] arr = new int [ R ] ; public static boolean canExpress ( int N ) { int temp = N ; int n = 0 ; while ( N != 0 ) { N /= 10 ; n ++ ; } N = temp ; int sum = 0 ; while ( N != 0 ) { sum += ( ( int ) Math . pow ( N % 10 , n ) ) ; N /= 10 ; } if ( sum == temp ) return true ; return false ; } public static void precompute ( ) { for ( int i = 1 ; i < R ; i ++ ) { if ( canExpress ( i ) ) { arr [ i ] = 1 ; } } for ( int i = 1 ; i < R ; i ++ ) { arr [ i ] += arr [ i - 1 ] ; } } public static void countNumbers ( int [ ] [ ] queries , int N ) { precompute ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int L1 = queries [ i ] [ 0 ] ; int R1 = queries [ i ] [ 1 ] ; System . out . print ( ( arr [ R1 ] - arr [ L1 - 1 ] ) + " ▁ " ) ; } } public static void main ( String args [ ] ) { int [ ] [ ] queries = { { 1 , 400 } , { 1 , 9 } } ; int N = queries . length ; countNumbers ( queries , N ) ; } }
Queries to calculate sum of the path from root to a given node in given Binary Tree | package whatever do not write package name here ; Function to find the sum of the path from root to the current node ; Sum of nodes in the path ; Iterate until root is reached ; Update the node value ; Print the resultant sum ; Function to print the path sum for each query ; Traverse the queries ; Driver Code ; arraylist to store integers
import java . io . * ; import java . util . ArrayList ; class GFG { public static void sumOfNodeInAPath ( int node_value ) { int sum_of_node = 0 ; while ( node_value > 0 ) { sum_of_node += node_value ; node_value /= 2 ; } System . out . print ( sum_of_node ) ; } public static void findSum ( ArrayList < Integer > Q ) { for ( int i = 0 ; i < Q . size ( ) ; i ++ ) { int node_value = Q . get ( i ) ; sumOfNodeInAPath ( node_value ) ; System . out . print ( " ▁ " ) ; } } public static void main ( String [ ] args ) { ArrayList < Integer > arr = new ArrayList < > ( ) ; arr . add ( 1 ) ; arr . add ( 5 ) ; arr . add ( 20 ) ; arr . add ( 100 ) ; findSum ( arr ) ; } }
Minimum removals required to make frequency of all remaining array elements equal | Java program for the above approach ; Stores frequency of all array elements ; Traverse the array ; Stores all the frequencies ; Traverse the map ; Sort the frequencies ; Count of frequencies ; Stores the final count ; Traverse the vector ; Count the number of removals for each frequency and update the minimum removals required ; Print the final count ; Driver code ; Given array ; Size of the array ; Function call to print the minimum number of removals required
import java . io . * ; import java . util . HashMap ; import java . util . Map ; import java . util . ArrayList ; import java . util . Collections ; class GFG { public static void minDeletions ( int arr [ ] , int N ) { HashMap < Integer , Integer > map = new HashMap < > ( ) ; ; for ( int i = 0 ; i < N ; i ++ ) { Integer k = map . get ( arr [ i ] ) ; map . put ( arr [ i ] , ( k == null ) ? 1 : k + 1 ) ; } ArrayList < Integer > v = new ArrayList < > ( ) ; for ( Map . Entry < Integer , Integer > e : map . entrySet ( ) ) { v . add ( e . getValue ( ) ) ; } Collections . sort ( v ) ; int size = v . size ( ) ; int ans = N - ( v . get ( 0 ) * size ) ; for ( int i = 1 ; i < v . size ( ) ; i ++ ) { if ( v . get ( i ) != v . get ( i - 1 ) ) { int safe = v . get ( i ) * ( size - i ) ; ans = Math . min ( ans , N - safe ) ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 3 , 2 , 5 , 3 } ; int N = 6 ; minDeletions ( arr , N ) ; } }
Check if a string is the typed name of the given name | Java program to implement run length encoding ; Check if the character is vowel or not ; Returns true if ' typed ' is a typed name given str ; Traverse through all characters of str . ; If current characters do not match ; If not vowel , simply move ahead in both ; Count occurrences of current vowel in str ; Count occurrences of current vowel in typed . ; Driver Code
public class Improve { static boolean isVowel ( char c ) { String vowel = " aeiou " ; for ( int i = 0 ; i < vowel . length ( ) ; ++ i ) if ( vowel . charAt ( i ) == c ) return true ; return false ; } static boolean printRLE ( String str , String typed ) { int n = str . length ( ) , m = typed . length ( ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) != typed . charAt ( j ) ) return false ; if ( isVowel ( str . charAt ( i ) ) == false ) { j ++ ; continue ; } int count1 = 1 ; while ( i < n - 1 && str . charAt ( i ) == str . charAt ( i + 1 ) ) { count1 ++ ; i ++ ; } int count2 = 1 ; while ( j < m - 1 && typed . charAt ( j ) == str . charAt ( i ) ) { count2 ++ ; j ++ ; } if ( count1 > count2 ) return false ; } return true ; } public static void main ( String args [ ] ) { String name = " alex " , typed = " aaalaeex " ; if ( printRLE ( name , typed ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Determine whether a universal sink exists in a directed graph | Java program to find whether a universal sink exists in a directed graph ; constructor to initialize number of vertices and size of adjacency matrix ; make adjacency_matrix [ i ] [ j ] = 1 if there is an edge from i to j ; if any element in the row i is 1 , it means that there is an edge emanating from the vertex , which means it cannot be a sink ; if any element other than i in the column i is 0 , it means that there is no edge from that vertex to the vertex we are testing and hence it cannot be a sink ; if none of the checks fails , return true ; we will eliminate n - 1 non sink vertices so that we have to check for only one vertex instead of all n vertices ; If the index is 1 , increment the row we are checking by 1 else increment the column ; If i exceeds the number of vertices , it means that there is no valid vertex in the given vertices that can be a sink ; Driver Code ; input set 1 g . insert ( 1 , 6 ) ; g . insert ( 2 , 6 ) ; g . insert ( 3 , 6 ) ; g . insert ( 4 , 6 ) ; g . insert ( 5 , 6 ) ; input set 2 ; returns 0 based indexing of vertex . returns - 1 if no sink exits . returns the vertex number - 1 if sink is found
import java . io . * ; import java . util . * ; class Graph { int vertices ; int [ ] [ ] adjacency_matrix ; public graph ( int vertices ) { this . vertices = vertices ; adjacency_matrix = new int [ vertices ] [ vertices ] ; } public void insert ( int source , int destination ) { adjacency_matrix [ destination - 1 ] = 1 ; } public boolean issink ( int i ) { for ( int j = 0 ; j < vertices ; j ++ ) { if ( adjacency_matrix [ i ] [ j ] == 1 ) return false ; if ( adjacency_matrix [ j ] [ i ] == 0 && j != i ) return false ; } return true ; } public int eliminate ( ) { int i = 0 , j = 0 ; while ( i < vertices && j < vertices ) { if ( adjacency_matrix [ i ] [ j ] == 1 ) i = i + 1 ; else j = j + 1 ; } if ( i > vertices ) return - 1 ; else if ( ! issink ( i ) ) return - 1 ; else return i ; } } public class Sink { public static void main ( String [ ] args ) throws IOException { int number_of_vertices = 6 ; int number_of_edges = 5 ; graph g = new graph ( number_of_vertices ) ; g . insert ( 1 , 6 ) ; g . insert ( 2 , 3 ) ; g . insert ( 2 , 4 ) ; g . insert ( 4 , 3 ) ; g . insert ( 5 , 3 ) ; int vertex = g . eliminate ( ) ; if ( vertex >= 0 ) System . out . println ( " Sink ▁ found ▁ at ▁ vertex ▁ " + ( vertex + 1 ) ) ; else System . out . println ( " No ▁ Sink " ) ; } }
Reverse alternate levels of a perfect binary tree | Java program to reverse alternate levels of a tree ; Base cases ; Swap subtrees if level is even ; Recur for left and right subtrees ( Note : left of root1 is passed and right of root2 in first call and opposite in second call . ; This function calls preorder ( ) for left and right children of root ; Inorder traversal ( used to print initial and modified trees ) ; A utility function to create a new node ; Driver program to test above functions
class Sol { static class Node { char key ; Node left , right ; } ; static void preorder ( Node root1 , Node root2 , int lvl ) { if ( root1 == null root2 == null ) return ; if ( lvl % 2 == 0 ) { char t = root1 . key ; root1 . key = root2 . key ; root2 . key = t ; } preorder ( root1 . left , root2 . right , lvl + 1 ) ; preorder ( root1 . right , root2 . left , lvl + 1 ) ; } static void reverseAlternate ( Node root ) { preorder ( root . left , root . right , 0 ) ; } static void printInorder ( Node root ) { if ( root == null ) return ; printInorder ( root . left ) ; System . out . print ( root . key + " ▁ " ) ; printInorder ( root . right ) ; } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . left = temp . right = null ; temp . key = ( char ) key ; return temp ; } public static void main ( String args [ ] ) { Node root = newNode ( ' a ' ) ; root . left = newNode ( ' b ' ) ; root . right = newNode ( ' c ' ) ; root . left . left = newNode ( ' d ' ) ; root . left . right = newNode ( ' e ' ) ; root . right . left = newNode ( ' f ' ) ; root . right . right = newNode ( ' g ' ) ; root . left . left . left = newNode ( ' h ' ) ; root . left . left . right = newNode ( ' i ' ) ; root . left . right . left = newNode ( ' j ' ) ; root . left . right . right = newNode ( ' k ' ) ; root . right . left . left = newNode ( ' l ' ) ; root . right . left . right = newNode ( ' m ' ) ; root . right . right . left = newNode ( ' n ' ) ; root . right . right . right = newNode ( ' o ' ) ; System . out . print ( "Inorder Traversal of given treeNEW_LINE"); printInorder ( root ) ; reverseAlternate ( root ) ; System . out . print ( " Inorder Traversal of modified tree "); printInorder ( root ) ; } }
Deletion in a Binary Tree | Java program to delete element in binary tree ; A binary tree node has key , pointer to left child and a pointer to right child ; Constructor ; Inorder traversal of a binary tree ; Function to delete deepest element in binary tree ; Do level order traversal until last node ; Function to delete given element in binary tree ; Do level order traversal until we find key and last node . ; Driver code
import java . util . LinkedList ; import java . util . Queue ; class GFG { static class Node { int key ; Node left , right ; Node ( int key ) { this . key = key ; left = null ; right = null ; } } static Node root ; static Node temp = root ; static void inorder ( Node temp ) { if ( temp == null ) return ; inorder ( temp . left ) ; System . out . print ( temp . key + " ▁ " ) ; inorder ( temp . right ) ; } static void deleteDeepest ( Node root , Node delNode ) { Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; Node temp = null ; while ( ! q . isEmpty ( ) ) { temp = q . peek ( ) ; q . remove ( ) ; if ( temp == delNode ) { temp = null ; return ; } if ( temp . right != null ) { if ( temp . right == delNode ) { temp . right = null ; return ; } else q . add ( temp . right ) ; } if ( temp . left != null ) { if ( temp . left == delNode ) { temp . left = null ; return ; } else q . add ( temp . left ) ; } } } static void delete ( Node root , int key ) { if ( root == null ) return ; if ( root . left == null && root . right == null ) { if ( root . key == key ) { root = null ; return ; } else return ; } Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; Node temp = null , keyNode = null ; while ( ! q . isEmpty ( ) ) { temp = q . peek ( ) ; q . remove ( ) ; if ( temp . key == key ) keyNode = temp ; if ( temp . left != null ) q . add ( temp . left ) ; if ( temp . right != null ) q . add ( temp . right ) ; } if ( keyNode != null ) { int x = temp . key ; deleteDeepest ( root , temp ) ; keyNode . key = x ; } } public static void main ( String args [ ] ) { root = new Node ( 10 ) ; root . left = new Node ( 11 ) ; root . left . left = new Node ( 7 ) ; root . left . right = new Node ( 12 ) ; root . right = new Node ( 9 ) ; root . right . left = new Node ( 15 ) ; root . right . right = new Node ( 8 ) ; System . out . print ( " Inorder ▁ traversal ▁ " + " before ▁ deletion : " ) ; inorder ( root ) ; int key = 11 ; delete ( root , key ) ; System . out . print ( " Inorder traversal " ▁ + ▁ " after deletion : "); inorder ( root ) ; } }
Number of sink nodes in a graph | Java program to count number if sink nodes ; Return the number of Sink NOdes . ; Array for marking the non - sink node . ; Marking the non - sink node . ; Counting the sink nodes . ; Driver Code
import java . util . * ; class GFG { static int countSink ( int n , int m , int edgeFrom [ ] , int edgeTo [ ] ) { int [ ] mark = new int [ n + 1 ] ; for ( int i = 0 ; i < m ; i ++ ) mark [ edgeFrom [ i ] ] = 1 ; int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( mark [ i ] == 0 ) count ++ ; return count ; } public static void main ( String [ ] args ) { int n = 4 , m = 2 ; int edgeFrom [ ] = { 2 , 4 } ; int edgeTo [ ] = { 3 , 3 } ; System . out . println ( countSink ( n , m , edgeFrom , edgeTo ) ) ; } }
Largest subset of Graph vertices with edges of 2 or more colors | Java program to find size of subset of graph vertex such that each vertex has more than 1 color edges ; Number of vertices ; function to calculate max subset size ; set for number of vertices ; loop for deletion of vertex from set ; if subset has only 1 vertex return 0 ; for each vertex iterate and keep removing a vertix while we find a vertex with all edges of same color . ; note down different color values for each vertex ; if only one color is found erase that vertex ( bad vertex ) ; If no vertex was removed in the above loop . ; Driver code
import java . util . * ; class GFG { static int N = 6 ; static int subsetGraph ( int C [ ] [ ] ) { HashSet < Integer > vertices = new HashSet < > ( ) ; for ( int i = 0 ; i < N ; ++ i ) { vertices . add ( i ) ; } while ( ! vertices . isEmpty ( ) ) { if ( vertices . size ( ) == 1 ) { return 1 ; } boolean someone_removed = false ; for ( int x : vertices ) { HashSet < Integer > values = new HashSet < > ( ) ; for ( int y : vertices ) { if ( y != x ) { values . add ( C [ x ] [ y ] ) ; } } if ( values . size ( ) == 1 ) { vertices . remove ( x ) ; someone_removed = true ; break ; } } if ( ! someone_removed ) { break ; } } return ( vertices . size ( ) ) ; } public static void main ( String [ ] args ) { int C [ ] [ ] = { { 0 , 9 , 2 , 4 , 7 , 8 } , { 9 , 0 , 9 , 9 , 7 , 9 } , { 2 , 9 , 0 , 3 , 7 , 6 } , { 4 , 9 , 3 , 0 , 7 , 1 } , { 7 , 7 , 7 , 7 , 0 , 7 } , { 8 , 9 , 6 , 1 , 7 , 0 } } ; System . out . println ( subsetGraph ( C ) ) ; } }