codeparrot/xlcost-text-to-code huggingface dataset:
Minimum replacement of pairs by their LCM required to reduce given array to its LCM | Java program for the above approach ; Boolean array to set or unset prime non - prime indices ; Stores the prefix sum of the count of prime numbers ; Function to check if a number is prime or not from 0 to N ; If p is a prime ; Set its multiples as non - prime ; Function to store the count of prime numbers ; Function to count the operations to reduce the array to one element by replacing each pair with its LCM ; Generating Prime Number ; Corner Case ; Driver code ; Stores the prefix sum of the count of prime numbers ; Given array arr [ ] ; Function call
import java . util . * ; class GFG { static final int maxm = 10001 ; static boolean prime [ ] ; static int prime_number [ ] ; static void SieveOfEratosthenes ( ) { Arrays . fill ( prime , true ) ; for ( int p = 2 ; p * p < maxm ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i < maxm ; i += p ) prime [ i ] = false ; } } prime [ 0 ] = false ; prime [ 1 ] = false ; } static void num_prime ( ) { prime_number [ 0 ] = 0 ; for ( int i = 1 ; i <= maxm ; i ++ ) { int tmp ; if ( prime [ i ] == true ) { tmp = 1 ; } else { tmp = 0 ; } prime_number [ i ] = prime_number [ i - 1 ] + tmp ; } } static void min_steps ( int arr [ ] , int n ) { SieveOfEratosthenes ( ) ; num_prime ( ) ; if ( n == 1 ) { System . out . println ( "0" ) ; } else if ( n == 2 ) { System . out . println ( "1" ) ; } else { System . out . println ( prime_number [ n ] - 1 + ( n - 2 ) ) ; } } public static void main ( String [ ] args ) { prime = new boolean [ maxm + 1 ] ; prime_number = new int [ maxm + 1 ] ; int arr [ ] = { 5 , 4 , 3 , 2 , 1 } ; int N = arr . length ; min_steps ( arr , N ) ; } }
Find the winner of a game of removing any number of stones from the least indexed non | Java program for the above approach ; Function to find the winner of game between A and B ; win = 1 means B is winner win = 0 means A is winner ; If size is even , winner is B ; If size is odd , winner is A ; Stone will be removed by B ; B will take n - 1 stones from current pile having n stones and force A to pick 1 stone ; Stone will be removed by A ; A will take n - 1 stones from current pile having n stones and force B to pick 1 stone ; Print the winner accordingly ; Driver Code ; Given piles of stone ; Function call
import java . util . * ; class GFG { static void findWinner ( int a [ ] , int n ) { int win = 0 ; if ( n % 2 == 0 ) win = 1 ; else win = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( i % 2 == 1 ) { if ( win == 0 && a [ i ] > 1 ) win = 1 ; } else { if ( win == 1 && a [ i ] > 1 ) win = 0 ; } } if ( win == 0 ) System . out . print ( " A " ) ; else System . out . print ( " B " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 , 2 } ; int N = arr . length ; findWinner ( arr , N ) ; } }
Check if a destination is reachable from source with two movements allowed | Set 2 | Java program to implement the above approach ; Check if ( x2 , y2 ) can be reached from ( x1 , y1 ) ; Reduce x2 by y2 until it is less than or equal to x1 ; Reduce y2 by x2 until it is less than or equal to y1 ; If x2 is reduced to x1 ; Check if y2 can be reduced to y1 or not ; If y2 is reduced to y1 ; Check if x2 can be reduced to x1 or not ; Driver Code
class GFG { static boolean isReachable ( long x1 , long y1 , long x2 , long y2 ) { while ( x2 > x1 && y2 > y1 ) { if ( x2 > y2 ) x2 %= y2 ; else y2 %= x2 ; } if ( x2 == x1 ) return ( y2 - y1 ) >= 0 && ( y2 - y1 ) % x1 == 0 ; else if ( y2 == y1 ) return ( x2 - x1 ) >= 0 && ( x2 - x1 ) % y1 == 0 ; else return false ; } public static void main ( String [ ] args ) { long source_x = 2 , source_y = 10 ; long dest_x = 26 , dest_y = 12 ; if ( isReachable ( source_x , source_y , dest_x , dest_y ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Find the player who wins the game by removing the last of given N cards | Java program to implement the above approach ; Function to check which player can win the game ; Driver code
import java . util . * ; class GFG { static void checkWinner ( int N , int K ) { if ( N % ( K + 1 ) > 0 ) { System . out . print ( " A " ) ; } else { System . out . print ( " B " ) ; } } public static void main ( String [ ] args ) { int N = 50 ; int K = 10 ; checkWinner ( N , K ) ; } }
Length of longest subarray having only K distinct Prime Numbers | Java program for the above approach ; Function to precalculate all the prime up to 10 ^ 6 ; Initialize prime to true ; Iterate [ 2 , sqrt ( N ) ] ; If p is prime ; Mark all multiple of p as true ; Function that finds the length of longest subarray K distinct primes ; Precompute all prime up to 2 * 10 ^ 6 ; Keep track occurrence of prime ; Initialize result to - 1 ; If number is prime then increment its count and decrease k ; Decrement K ; Remove required elements till k become non - negative ; Decrease count so that it may appear in another subarray appearing after this present subarray ; Increment K ; Take the max value as length of subarray ; Return the final length ; Driver Code ; Given array arr [ ] ; Function call
import java . util . * ; import java . lang . * ; class GFG { static boolean [ ] isprime = new boolean [ 2000010 ] ; static void SieveOfEratosthenes ( int n ) { Arrays . fill ( isprime , true ) ; isprime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( isprime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) isprime [ i ] = false ; } } } static int KDistinctPrime ( int arr [ ] , int n , int k ) { SieveOfEratosthenes ( 2000000 ) ; Map < Integer , Integer > cnt = new HashMap < > ( ) ; int result = - 1 ; for ( int i = 0 , j = - 1 ; i < n ; ++ i ) { int x = arr [ i ] ; if ( isprime [ x ] ) { cnt . put ( x , cnt . getOrDefault ( x , 0 ) + 1 ) ; if ( cnt . get ( x ) == 1 ) { -- k ; } } while ( k < 0 ) { x = arr [ ++ j ] ; if ( isprime [ x ] ) { cnt . put ( x , cnt . getOrDefault ( x , 0 ) - 1 ) ; if ( cnt . get ( x ) == 0 ) { ++ k ; } } } if ( k == 0 ) result = Math . max ( result , i - j ) ; } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int K = 3 ; int N = arr . length ; System . out . println ( KDistinctPrime ( arr , N , K ) ) ; } }
Smallest composite number not divisible by first N prime numbers | Java program to implement the above approach ; Initializing the max value ; Function to generate N prime numbers using Sieve of Eratosthenes ; Stores the primes ; Setting all numbers to be prime initially ; If a prime number is encountered ; Set all its multiples as composites ; Store all the prime numbers ; Function to find the square of the ( N + 1 ) - th prime number ; Driver Code ; Stores all prime numbers
import java . util . Arrays ; import java . util . Vector ; class GFG { static final int MAX_SIZE = 1000005 ; static void SieveOfEratosthenes ( Vector < Integer > StorePrimes ) { boolean [ ] IsPrime = new boolean [ MAX_SIZE ] ; Arrays . fill ( IsPrime , true ) ; for ( int p = 2 ; p * p < MAX_SIZE ; p ++ ) { if ( IsPrime [ p ] == true ) { for ( int i = p * p ; i < MAX_SIZE ; i += p ) IsPrime [ i ] = false ; } } for ( int p = 2 ; p < MAX_SIZE ; p ++ ) if ( IsPrime [ p ] ) StorePrimes . add ( p ) ; } static int Smallest_non_Prime ( Vector < Integer > StorePrimes , int N ) { int x = StorePrimes . get ( N ) ; return x * x ; } public static void main ( String [ ] args ) { int N = 3 ; Vector < Integer > StorePrimes = new Vector < Integer > ( ) ; SieveOfEratosthenes ( StorePrimes ) ; System . out . print ( Smallest_non_Prime ( StorePrimes , N ) ) ; } }
Nth Subset of the Sequence consisting of powers of K in increasing order of their Sum | Java program for above approach ; Function to print the required N - th subset ; Nearest power of 2 <= N ; Now insert k ^ p in the answer ; update n ; Print the ans in sorted order ; Driver function
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static void printSubset ( long n , int k ) { ArrayList < Long > answer = new ArrayList < > ( ) ; while ( n > 0 ) { long p = ( long ) ( Math . log ( n ) / Math . log ( 2 ) ) ; ; answer . add ( ( long ) ( Math . pow ( k , p ) ) ) ; n %= ( int ) Math . pow ( 2 , p ) ; } Collections . sort ( answer ) ; for ( Long x : answer ) { System . out . print ( x + " ▁ " ) ; } } public static void main ( String [ ] args ) { long n = 5 ; int k = 4 ; printSubset ( n , k ) ; } }
Minimum time taken by each job to be completed given by a Directed Acyclic Graph | Java program for the above approach ; Adjacency List to store the graph ; Array to store the in - degree of node ; Array to store the time in which the job i can be done ; Function to add directed edge between two vertices ; Insert edge from u to v ; Increasing the indegree of vertex v ; Function to find the minimum time needed by each node to get the task ; Queue to store the nodes while processing ; Update the time of the jobs who don 't require any job to  be completed before this job ; Iterate until queue is empty ; Get front element of queue ; Pop the front element ; Decrease in - degree of the current node ; Push its adjacent elements ; Print the time to complete the job ; Driver Code ; Given Nodes N and edges M ; Given directed edges of graph ; Function call
import java . util . * ; class GFG { static final int maxN = 100000 ; @ SuppressWarnings ( " unchecked " ) static Vector < Integer > [ ] graph = new Vector [ maxN ] ; static int [ ] indegree = new int [ maxN ] ; static int [ ] job = new int [ maxN ] ; static void addEdge ( int u , int v ) { graph [ u ] . add ( v ) ; indegree [ v ] ++ ; } static void printOrder ( int n , int m ) { Queue < Integer > q = new LinkedList < > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { if ( indegree [ i ] == 0 ) { q . add ( i ) ; job [ i ] = 1 ; } } while ( ! q . isEmpty ( ) ) { int cur = q . peek ( ) ; q . remove ( ) ; for ( int adj : graph [ cur ] ) { indegree [ adj ] -- ; if ( indegree [ adj ] == 0 ) { job [ adj ] = 1 + job [ cur ] ; q . add ( adj ) ; } } } for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( job [ i ] + " ▁ " ) ; System . out . print ( "NEW_LINE"); } public static void main ( String [ ] args ) { int n , m ; n = 10 ; m = 13 ; for ( int i = 0 ; i < graph . length ; i ++ ) graph [ i ] = new Vector < Integer > ( ) ; addEdge ( 1 , 3 ) ; addEdge ( 1 , 4 ) ; addEdge ( 1 , 5 ) ; addEdge ( 2 , 3 ) ; addEdge ( 2 , 8 ) ; addEdge ( 2 , 9 ) ; addEdge ( 3 , 6 ) ; addEdge ( 4 , 6 ) ; addEdge ( 4 , 8 ) ; addEdge ( 5 , 8 ) ; addEdge ( 6 , 7 ) ; addEdge ( 7 , 8 ) ; addEdge ( 8 , 10 ) ; printOrder ( n , m ) ; } }
Count total set bits in all numbers from range L to R | Java program for the above approach ; Returns position of leftmost set bit The rightmost position is taken as 0 ; Function that gives the position of previous leftmost set bit in n ; Function that returns count of set bits present in all numbers from 1 to n ; Get the position of leftmost set bit in n ; Use the position ; Function to count the set bits between the two numbers N and M ; Base Case ; Get position of next leftmost set bit ; If n is of the form 2 ^ x - 1 ; Update n for next recursive call ; Function that counts the set bits between L and R ; Driver Code ; Given L and R ; Function Call
import java . util . * ; class GFG { static int getLeftmostBit ( int n ) { int m = 0 ; while ( n > 1 ) { n = n >> 1 ; m ++ ; } return m ; } static int getNextLeftmostBit ( int n , int m ) { int temp = 1 << m ; while ( n < temp ) { temp = temp >> 1 ; m -- ; } return m ; } static int countSetBit ( int n ) { int m = getLeftmostBit ( n ) ; return _countSetBit ( n , m ) ; } static int _countSetBit ( int n , int m ) { if ( n == 0 ) return 0 ; m = getNextLeftmostBit ( n , m ) ; if ( n == ( ( int ) 1 << ( m + 1 ) ) - 1 ) return ( int ) ( m + 1 ) * ( 1 << m ) ; n = n - ( 1 << m ) ; return ( ( n + 1 ) + countSetBit ( n ) + m * ( 1 << ( m - 1 ) ) ) ; } static int countSetBits ( int L , int R ) { return Math . abs ( countSetBit ( R ) - countSetBit ( L - 1 ) ) ; } public static void main ( String [ ] args ) { int L = 3 , R = 5 ; System . out . print ( " Total ▁ set ▁ bit ▁ count ▁ is ▁ " + countSetBits ( L , R ) ) ; } }
Permutation of Array such that products of all adjacent elements are even | Java program to permutation of array such that product of all adjacent elements is even ; Function to print the required permutation ; push odd elements in ' odd ' and even elements in ' even ' ; Check if it possible to arrange the elements ; Else print the permutation ; Print remaining odds are even . and even elements ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void printPermutation ( int arr [ ] , int n ) { ArrayList < Integer > odd = new ArrayList < Integer > ( ) ; ArrayList < Integer > even = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even . add ( arr [ i ] ) ; else odd . add ( arr [ i ] ) ; } int size_odd = odd . size ( ) ; int size_even = even . size ( ) ; if ( size_odd > size_even + 1 ) System . out . println ( " - 1" ) ; else { int i = 0 ; int j = 0 ; while ( i < size_odd && j < size_even ) { System . out . print ( odd . get ( i ) + " ▁ " ) ; ++ i ; System . out . print ( even . get ( j ) + " ▁ " ) ; ++ j ; } while ( i < size_odd ) { System . out . print ( odd . get ( i ) + " ▁ " ) ; ++ i ; } while ( j < size_even ) { System . out . print ( even . get ( j ) + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 7 , 9 , 8 , 10 , 11 } ; int N = arr . length ; printPermutation ( arr , N ) ; } }
Maximize GCD of all possible pairs from 1 to N | Java Program to implement the approach ; Function to obtain the maximum gcd of all pairs from 1 to n ; Print the answer ; Driver code ; Function call
class GFG { static void find ( int n ) { System . out . println ( n / 2 ) ; } public static void main ( String [ ] args ) { int n = 5 ; find ( n ) ; } }
Count of pairs of Array elements which are divisible by K when concatenated | Java program to count pairs of array elements which are divisible by K when concatenated ; Function to calculate and return the count of pairs ; Compute power of 10 modulo k ; Calculate length of a [ i ] ; Increase count of remainder ; Calculate ( a [ i ] * 10 ^ lenj ) % k ; Calculate ( k - ( a [ i ] * 10 ^ lenj ) % k ) % k ; Increase answer by count ; If a pair ( a [ i ] , a [ i ] ) is counted ; Return the count of pairs ; Driver code
import java . util . * ; import java . lang . * ; class GFG { static int [ ] [ ] rem = new int [ 11 ] [ 11 ] ; static int countPairs ( int [ ] a , int n , int k ) { int [ ] len = new int [ n ] ; int [ ] p = new int [ 11 ] ; p [ 0 ] = 1 ; for ( int i = 1 ; i <= 10 ; i ++ ) { p [ i ] = ( p [ i - 1 ] * 10 ) % k ; } for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; while ( x > 0 ) { len [ i ] ++ ; x /= 10 ; } rem [ len [ i ] ] [ a [ i ] % k ] ++ ; } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 1 ; j <= 10 ; j ++ ) { int r = ( a [ i ] * p [ j ] ) % k ; int xr = ( k - r ) % k ; ans += rem [ j ] [ xr ] ; if ( len [ i ] == j && ( r + a [ i ] % k ) % k == 0 ) ans -- ; } } return ans ; } public static void main ( String [ ] args ) { int [ ] a = { 4 , 5 , 2 } ; int n = a . length , k = 2 ; System . out . println ( countPairs ( a , n , k ) ) ; } }
Count of N digit Numbers whose sum of every K consecutive digits is equal | Java program for the above approach ; Function to count the number of N - digit numbers such that sum of every k consecutive digits are equal ; Range of numbers ; Extract digits of the number ; Store the sum of first K digits ; Check for every k - consecutive digits using sliding window ; Driver Code ; Given integer N and K
class GFG { static int countDigitSum ( int N , int K ) { int l = ( int ) Math . pow ( 10 , N - 1 ) , r = ( int ) Math . pow ( 10 , N ) - 1 ; int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { int num = i ; int digits [ ] = new int [ N ] ; for ( int j = N - 1 ; j >= 0 ; j -- ) { digits [ j ] = num % 10 ; num /= 10 ; } int sum = 0 , flag = 0 ; for ( int j = 0 ; j < K ; j ++ ) sum += digits [ j ] ; for ( int j = K ; j < N ; j ++ ) { if ( sum - digits [ j - K ] + digits [ j ] != sum ) { flag = 1 ; break ; } } if ( flag == 0 ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { int N = 2 , K = 1 ; System . out . print ( countDigitSum ( N , K ) ) ; } }
Array formed using sum of absolute differences of that element with all other elements | Java program for the above approach ; Function to return the new array ; Length of the arraylist ; Initialize the Arraylist ; Sum of absolute differences of element with all elements ; Initialize int sum to 0 ; Add the value of sum to ans ; Return the final ans ; Driver Code ; Given array arr [ ] ; Function Call
import java . util . * ; class GFG { private static List < Integer > calculate ( int [ ] arr ) { int n = arr . length ; List < Integer > ans = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { int sum = 0 ; for ( int j = 0 ; j < arr . length ; j ++ ) { sum += Math . abs ( arr [ i ] - arr [ j ] ) ; } ans . add ( sum ) ; } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 6 } ; System . out . println ( calculate ( arr ) ) ; } }
Number of pair of positions in matrix which are not accessible | Java program to count number of pair of positions in matrix which are not accessible ; Counts number of vertices connected in a component containing x . Stores the count in k . ; Incrementing the number of node in a connected component . ; Return the number of count of non - accessible cells . ; Initialize count of connected vertices found by DFS starting from i . ; Update result ; Inserting the edge between edge . ; Mapping the cell coordinate into node number . ; Inserting the edge . ; Driver Code
import java . util . * ; class GFG { static int k ; static void dfs ( Vector < Integer > graph [ ] , boolean visited [ ] , int x ) { for ( int i = 0 ; i < graph [ x ] . size ( ) ; i ++ ) { if ( ! visited [ graph [ x ] . get ( i ) ] ) { ( k ) ++ ; visited [ graph [ x ] . get ( i ) ] = true ; dfs ( graph , visited , graph [ x ] . get ( i ) ) ; } } } static int countNonAccessible ( Vector < Integer > graph [ ] , int N ) { boolean [ ] visited = new boolean [ N * N + N ] ; int ans = 0 ; for ( int i = 1 ; i <= N * N ; i ++ ) { if ( ! visited [ i ] ) { visited [ i ] = true ; int k = 1 ; dfs ( graph , visited , i ) ; ans += k * ( N * N - k ) ; } } return ans ; } static void insertpath ( Vector < Integer > graph [ ] , int N , int x1 , int y1 , int x2 , int y2 ) { int a = ( x1 - 1 ) * N + y1 ; int b = ( x2 - 1 ) * N + y2 ; graph [ a ] . add ( b ) ; graph [ b ] . add ( a ) ; } public static void main ( String args [ ] ) { int N = 2 ; Vector [ ] graph = new Vector [ N * N + 1 ] ; for ( int i = 1 ; i <= N * N ; i ++ ) graph [ i ] = new Vector < Integer > ( ) ; insertpath ( graph , N , 1 , 1 , 1 , 2 ) ; insertpath ( graph , N , 1 , 2 , 2 , 2 ) ; System . out . println ( countNonAccessible ( graph , N ) ) ; } }
Minimum number of distinct powers of 2 required to express a given binary number | Java program to implement the above approach ; Function to return the minimum distinct powers of 2 required to express s ; Reverse the string to start from lower powers ; Check if the character is 1 ; Add in range provided range ; Initialize the counter ; Check if the character is not 0 ; Increment the counter ; Print the result ; Driver code
import java . util . * ; class GFG { static void findMinimum ( String s ) { int n = s . length ( ) ; int [ ] x = new int [ n + 1 ] ; StringBuilder s2 = new StringBuilder ( s ) ; s2 . reverse ( ) ; String s3 = s2 . toString ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s3 . charAt ( i ) == '1' ) { if ( x [ i ] == 1 ) { x [ i + 1 ] = 1 ; x [ i ] = 0 ; } else if ( 1 <= i && ( i & x [ i - 1 ] ) == 1 ) { x [ i + 1 ] = 1 ; x [ i - 1 ] = - 1 ; } else x [ i ] = 1 ; } } int c = 0 ; for ( int i = 0 ; i <= n ; i ++ ) { if ( x [ i ] != 0 ) c ++ ; } System . out . println ( c ) ; } public static void main ( String [ ] args ) { String str = "111" ; findMinimum ( str ) ; } }
Split a Numeric String into Fibonacci Sequence | Java program of the above approach ; Function that returns true if Fibonacci sequence is found ; Base condition : If pos is equal to length of S and seq length is greater than 3 ; Return true ; Stores current number ; Add current digit to num ; Avoid integer overflow ; Avoid leading zeros ; If current number is greater than last two number of seq ; If seq length is less 2 or current number is is equal to the last two of the seq ; Add to the seq ; Recur for i + 1 ; Remove last added number ; If no sequence is found ; Function that prints the Fibonacci sequence from the split of string S ; Initialize a vector to store the sequence ; Call helper function ; If sequence length is greater than 3 ; Print the sequence ; If no sequence is found ; Print - 1 ; Driver code ; Given String ; Function Call
import java . util . * ; class GFG { static boolean splitIntoFibonacciHelper ( int pos , String S , ArrayList < Long > seq ) { if ( pos == S . length ( ) && ( seq . size ( ) >= 3 ) ) { return true ; } long num = 0 ; for ( int i = pos ; i < S . length ( ) ; i ++ ) { num = num * 10 + ( S . charAt ( i ) - '0' ) ; if ( num > Integer . MAX_VALUE ) break ; if ( S . charAt ( pos ) == '0' && i > pos ) break ; if ( seq . size ( ) > 2 && ( num > ( ( long ) seq . get ( seq . size ( ) - 1 ) + ( long ) seq . get ( seq . size ( ) - 2 ) ) ) ) break ; if ( seq . size ( ) < 2 || ( num == ( ( long ) seq . get ( seq . size ( ) - 1 ) + ( long ) seq . get ( seq . size ( ) - 2 ) ) ) ) { seq . add ( num ) ; if ( splitIntoFibonacciHelper ( i + 1 , S , seq ) ) return true ; seq . remove ( seq . size ( ) - 1 ) ; } } return false ; } static void splitIntoFibonacci ( String S ) { ArrayList < Long > seq = new ArrayList < > ( ) ; splitIntoFibonacciHelper ( 0 , S , seq ) ; if ( seq . size ( ) >= 3 ) { for ( int i = 0 ; i < seq . size ( ) ; i ++ ) System . out . print ( seq . get ( i ) + " ▁ " ) ; } else { System . out . print ( " - 1" ) ; } } public static void main ( String [ ] args ) { String S = "11235813" ; splitIntoFibonacci ( S ) ; } }
Count pair of strings whose concatenation has every vowel | Java program for the above approach ; Function to return the count of all concatenated string with each vowel at least once ; Creating a hash array with initial value as 0 ; Traversing through each string and getting hash value for each of them ; Initializing the weight of each string ; Find the hash value for each string ; Increasing the count of the hash value ; Getting all possible pairs of indexes in hash array ; Check if the pair which has hash value 31 and multiplying the count of string and add it strCount ; Corner case , for strings which independently has all the vowels ; Return thre final count ; Driver code ; Given array of strings ; Function call
class GFG { public static int good_pairs ( String [ ] str , int N ) { int arr [ ] = new int [ 32 ] ; int strCount = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int Weight = 0 ; for ( int j = 0 ; j < str [ i ] . length ( ) ; j ++ ) { switch ( str [ i ] . charAt ( j ) ) { case ' a ' : Weight = Weight | 1 ; break ; case ' e ' : Weight = Weight | 2 ; break ; case ' i ' : Weight = Weight | 4 ; break ; case ' o ' : Weight = Weight | 8 ; break ; case ' u ' : Weight = Weight | 16 ; break ; } } arr [ Weight ] ++ ; } for ( int i = 0 ; i < 32 ; i ++ ) { for ( int j = i + 1 ; j < 32 ; j ++ ) { if ( ( i j ) == 31 ) strCount += arr [ i ] * arr [ j ] ; } } strCount += ( arr [ 31 ] * ( arr [ 31 ] - 1 ) ) / 2 ; return strCount ; } public static void main ( String [ ] args ) { String str [ ] = { " aaweiolkju " , " oxdfgujkmi " } ; int N = str . length ; System . out . println ( good_pairs ( str , N ) ) ; } }