codeparrot/xlcost-text-to-code huggingface dataset:
Count the nodes in the given tree whose weight is a powerful number | Java implementation to Count the nodes in the given tree whose weight is a powerful number ; Function to check if the number is powerful ; First divide the number repeatedly by 2 ; Check if only 2 ^ 1 divides n , then return false ; Check if n is not a power of 2 then this loop will execute ; Find highest power of " factor " that divides n ; Check if only factor ^ 1 divides n , then return false ; n must be 1 now if it is not a prime number . Since prime numbers are not powerful , we return false if n is not 1. ; Function to perform dfs ; Check if weight of the current node is a powerful number ; Driver code ; Weights of the node ; Edges of the tree
import java . util . * ; class GFG { static int ans = 0 ; static Vector < Integer > [ ] graph = new Vector [ 100 ] ; static int [ ] weight = new int [ 100 ] ; static boolean isPowerful ( int n ) { while ( n % 2 == 0 ) { int power = 0 ; while ( n % 2 == 0 ) { n /= 2 ; power ++ ; } if ( power == 1 ) return false ; } for ( int factor = 3 ; factor <= Math . sqrt ( n ) ; factor += 2 ) { int power = 0 ; while ( n % factor == 0 ) { n = n / factor ; power ++ ; } if ( power == 1 ) return false ; } return ( n == 1 ) ; } static void dfs ( int node , int parent ) { if ( isPowerful ( weight [ node ] ) ) ans += 1 ; for ( int to : graph [ node ] ) { if ( to == parent ) continue ; dfs ( to , node ) ; } } public static void main ( String [ ] args ) { for ( int i = 0 ; i < graph . length ; i ++ ) graph [ i ] = new Vector < Integer > ( ) ; weight [ 1 ] = 5 ; weight [ 2 ] = 10 ; weight [ 3 ] = 11 ; weight [ 4 ] = 8 ; weight [ 5 ] = 6 ; graph [ 1 ] . add ( 2 ) ; graph [ 2 ] . add ( 3 ) ; graph [ 2 ] . add ( 4 ) ; graph [ 1 ] . add ( 5 ) ; dfs ( 1 , 1 ) ; System . out . print ( ans ) ; } }
Number of ways to color boundary of each block of M * N table | Java program to count the number of ways to color boundary of each block of M * N table . ; Function to compute all way to fill the boundary of all sides of the unit square ; Count possible ways to fill all upper and left side of the rectangle M * N ; Count possible ways to fill all side of the all squares unit size ; Driver code ; Number of rows ; Number of columns
import java . util . * ; class GFG { static int CountWays ( int N , int M ) { int count = 1 ; count = ( int ) Math . pow ( 3 , M + N ) ; count *= ( int ) Math . pow ( 2 , M * N ) ; return count ; } public static void main ( String args [ ] ) { int N = 3 ; int M = 2 ; System . out . println ( CountWays ( N , M ) ) ; } }
Nth positive number whose absolute difference of adjacent digits is at most 1 | Java program to find Nth number with absolute difference between all adjacent digits at most 1. ; Return Nth number with absolute difference between all adjacent digits at most 1. ; To store all such numbers ; Enqueue all integers from 1 to 9 in increasing order . ; Perform the operation N times so that we can get all such N numbers . ; Store the front element of queue , in array and pop it from queue . ; If the last digit of dequeued integer is not 0 , then enqueue the next such number . ; Enqueue the next such number ; If the last digit of dequeued integer is not 9 , then enqueue the next such number . ; Driver Code
import java . util . * ; class GFG { static void findNthNumber ( int N ) { int [ ] arr = new int [ N + 1 ] ; Queue < Integer > q = new LinkedList < > ( ) ; for ( int i = 1 ; i <= 9 ; i ++ ) q . add ( i ) ; for ( int i = 1 ; i <= N ; i ++ ) { arr [ i ] = q . peek ( ) ; q . remove ( ) ; if ( arr [ i ] % 10 != 0 ) q . add ( arr [ i ] * 10 + arr [ i ] % 10 - 1 ) ; q . add ( arr [ i ] * 10 + arr [ i ] % 10 ) ; if ( arr [ i ] % 10 != 9 ) q . add ( arr [ i ] * 10 + arr [ i ] % 10 + 1 ) ; } System . out . println ( arr [ N ] ) ; } public static void main ( String [ ] args ) { int N = 21 ; findNthNumber ( N ) ; } }
Unique element in an array where all elements occur K times except one | Set 2 | Java program for the above approach ; Function that find the unique element in the array arr [ ] ; Store all unique element in set ; Sum of all element of the array ; Sum of element in the set ; Print the unique element using formula ; Driver code ; Function call
import java . util . * ; class GFG { static void findUniqueElements ( int arr [ ] , int N , int K ) { Set < Integer > s = new HashSet < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) s . add ( arr [ i ] ) ; int arr_sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) arr_sum += arr [ i ] ; int set_sum = 0 ; Iterator it = s . iterator ( ) ; while ( it . hasNext ( ) ) { set_sum += ( int ) it . next ( ) ; } System . out . println ( ( K * set_sum - arr_sum ) / ( K - 1 ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 1 , 12 , 3 , 12 , 1 , 1 , 2 , 3 , 2 , 2 , 3 , 7 } ; int N = arr . length ; int K = 3 ; findUniqueElements ( arr , N , K ) ; } }
Form the Cubic equation from the given roots | Java program for the approach ; Function to find the cubic equation whose roots are a , b and c ; Find the value of coefficient ; Print the equation as per the above coefficients ; Driver Code ; Function Call
class GFG { static void findEquation ( int a , int b , int c ) { int X = ( a + b + c ) ; int Y = ( a * b ) + ( b * c ) + ( c * a ) ; int Z = a * b * c ; System . out . print ( " x ^ 3 ▁ - ▁ " + X + " x ^ 2 ▁ + ▁ " + Y + " x ▁ - ▁ " + Z + " ▁ = ▁ 0" ) ; } public static void main ( String [ ] args ) { int a = 5 , b = 2 , c = 3 ; findEquation ( a , b , c ) ; } }
Gill 's 4th Order Method to solve Differential Equations | Java program to implement Gill 's method ; A sample differential equation " dy / dx ▁ = ▁ ( x ▁ - ▁ y ) /2" ; Finds value of y for a given x using step size h and initial value y0 at x0 ; Count number of iterations using step size or height h ; Value of K_i ; Initial value of y ( 0 ) ; Iterate for number of iteration ; Value of K1 ; Value of K2 ; Value of K3 ; Value of K4 ; Find the next value of y ( n + 1 ) using y ( n ) and values of K in the above steps ; Update next value of x ; Return the final value of dy / dx ; Driver Code
class GFG { static double dydx ( double x , double y ) { return ( x - y ) / 2 ; } static double Gill ( double x0 , double y0 , double x , double h ) { int n = ( int ) ( ( x - x0 ) / h ) ; double k1 , k2 , k3 , k4 ; double y = y0 ; for ( int i = 1 ; i <= n ; i ++ ) { k1 = h * dydx ( x0 , y ) ; k2 = h * dydx ( x0 + 0.5 * h , y + 0.5 * k1 ) ; k3 = h * dydx ( x0 + 0.5 * h , y + 0.5 * ( - 1 + Math . sqrt ( 2 ) ) * k1 + k2 * ( 1 - 0.5 * Math . sqrt ( 2 ) ) ) ; k4 = h * dydx ( x0 + h , y - ( 0.5 * Math . sqrt ( 2 ) ) * k2 + k3 * ( 1 + 0.5 * Math . sqrt ( 2 ) ) ) ; y = y + ( 1.0 / 6 ) * ( k1 + ( 2 - Math . sqrt ( 2 ) ) * k2 + ( 2 + Math . sqrt ( 2 ) ) * k3 + k4 ) ; x0 = x0 + h ; } return y ; } public static void main ( String [ ] args ) { double x0 = 0 , y = 3.0 , x = 5.0 , h = 0.2 ; System . out . printf ( " y ( x ) ▁ = ▁ % .6f " , Gill ( x0 , y , x , h ) ) ; } }
Program to print numbers from N to 1 in reverse order | Java program to print all numbers between 1 to N in reverse order ; Recursive function to print from N to 1 ; Driver code
import java . util . * ; class GFG { static void PrintReverseOrder ( int N ) { for ( int i = N ; i > 0 ; i -- ) System . out . print ( + i + " ▁ " ) ; } public static void main ( String [ ] args ) { int N = 5 ; PrintReverseOrder ( N ) ; } }
Find count of numbers from 0 to n which satisfies the given equation for a value K | Java implementation to find the total count of all the numbers from 0 to n which satisfies the given equation for a value K ; Function to find the values ; Calculate the LCM ; Calculate the multiples of lcm ; Find the values which satisfies the given condition ; Subtract the extra values ; Return the final result ; Driver code
class GFG { static int findAns ( int a , int b , int n ) { int lcm = ( a * b ) / __gcd ( a , b ) ; int multiples = ( n / lcm ) + 1 ; int answer = Math . max ( a , b ) * multiples ; int lastvalue = lcm * ( n / lcm ) + Math . max ( a , b ) ; if ( lastvalue > n ) answer = answer - ( lastvalue - n - 1 ) ; return answer ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int a = 1 , b = 13 , n = 500 ; System . out . print ( findAns ( a , b , n ) + "NEW_LINE"); } }
Program to find if two numbers and their AM and HM are present in an array using STL | Java program to check if two numbers are present in an array then their AM and HM are also present . Finally , find the GM of the numbers ; Function to find the Arithmetic Mean of 2 numbers ; Function to find the Harmonic Mean of 2 numbers ; Following function checks and computes the desired results based on the means ; Calculate means ; Hash container ( HashMap ) to store elements ; Insertion of array elements in the Set ; Conditionals to check if numbers are present in array by Hashing ; Conditionals to check if the AM and HM of the numbers are present in array ; If all conditions are satisfied , the Geometric Mean is calculated ; If numbers are found but the respective AM and HM are not found in the array ; If none of the conditions are satisfied ; Driver code
import java . util . * ; class GFG { static Double ArithmeticMean ( Double A , Double B ) { return ( A + B ) / 2 ; } static Double HarmonicMean ( Double A , Double B ) { return ( 2 * A * B ) / ( A + B ) ; } static void CheckArithmeticHarmonic ( Double arr [ ] , Double A , Double B , int N ) { Double AM = ArithmeticMean ( A , B ) ; Double HM = HarmonicMean ( A , B ) ; HashMap < Double , Integer > Hash = new HashMap < Double , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { Hash . put ( arr [ i ] , 1 ) ; } if ( Hash . get ( A ) != 0 && Hash . get ( B ) != 0 ) { if ( Hash . get ( AM ) != 0 && Hash . get ( HM ) != 0 ) { System . out . print ( " GM ▁ = ▁ " ) ; System . out . format ( " % .2f " , Math . sqrt ( AM * HM ) ) ; } else { System . out . print ( " AM ▁ and ▁ HM ▁ not ▁ found " ) ; } } else { System . out . print ( " numbers ▁ not ▁ found " ) ; } } public static void main ( String args [ ] ) { Double arr [ ] = { 1.0 , 2.0 , 2.5 , 3.0 , 4.0 , 4.5 , 5.0 , 6.0 } ; int N = ( arr . length ) ; Double A = 3.0 ; Double B = 6.0 ; CheckArithmeticHarmonic ( arr , A , B , N ) ; } }
Minimum decrements to make integer A divisible by integer B | Java implementation to count total numbers moves to make integer A divisible by integer B ; Function that print number of moves required ; Calculate modulo ; Print the required answer ; Driver code ; Initialise A and B
import java . util . * ; class GFG { static void movesRequired ( int a , int b ) { int total_moves = a % b ; System . out . println ( total_moves ) ; } public static void main ( String [ ] args ) { int A = 10 , B = 3 ; movesRequired ( A , B ) ; } }
Pythagorean Triplet with given sum using single loop | Java program to find the Pythagorean Triplet with given sum ; Function to calculate the Pythagorean triplet in O ( n ) ; Iterate a from 1 to N - 1. ; Calculate value of b ; The value of c = n - a - b ; Driver Code ; Function call
class GFG { static void PythagoreanTriplet ( int n ) { int flag = 0 ; for ( int a = 1 ; a < n ; a ++ ) { int b = ( n * n - 2 * n * a ) / ( 2 * n - 2 * a ) ; int c = n - a - b ; if ( a * a + b * b == c * c && b > 0 && c > 0 ) { System . out . print ( a + " ▁ " + b + " ▁ " + c ) ; flag = 1 ; break ; } } if ( flag == 0 ) { System . out . print ( " - 1" ) ; } return ; } public static void main ( String [ ] args ) { int N = 12 ; PythagoreanTriplet ( N ) ; } }
Check if there exists a number with X factors out of which exactly K are prime | Java program to check if there exists a number with X factors out of which exactly K are prime ; Function to check if such number exists ; To store the sum of powers of prime factors of X which determines the maximum count of numbers whose product can form X ; Determining the prime factors of X ; To check if the number is prime ; If X is 1 , then we cannot form a number with 1 factor and K prime factor ( as K is atleast 1 ) ; If X itself is prime then it can be represented as a power of only 1 prime factor which is X itself so we return true ; If sum of the powers of prime factors of X is greater than or equal to K , which means X can be represented as a product of K numbers , we return true ; In any other case , we return false as we cannot form a number with X factors and K prime factors ; Driver code
import java . util . * ; class GFG { static boolean check ( int X , int K ) { int prime , temp , sqr , i ; prime = 0 ; temp = X ; sqr = ( int ) Math . sqrt ( X ) ; for ( i = 2 ; i <= sqr ; i ++ ) { while ( temp % i == 0 ) { temp = temp / i ; prime ++ ; } } if ( temp > 2 ) prime ++ ; if ( X == 1 ) return false ; if ( prime == 1 && K == 1 ) return true ; else if ( prime >= K ) return true ; else return false ; } public static void main ( String [ ] args ) { int X , K ; X = 4 ; K = 2 ; if ( check ( X , K ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Number of subsets with same AND , OR and XOR values in an Array | Java implementation to find the number of subsets with equal bitwise AND , OR and XOR values ; Function to find the number of subsets with equal bitwise AND , OR and XOR values ; Traverse through all the subsets ; Finding the subsets with the bits of ' i ' which are set ; Computing the bitwise AND ; Computing the bitwise OR ; Computing the bitwise XOR ; Comparing all the three values ; Driver Code
import java . io . * ; class GFG { static int mod = 1000000007 ; static int countSubsets ( int a [ ] , int n ) { int answer = 0 ; for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { int bitwiseAND = - 1 ; int bitwiseOR = 0 ; int bitwiseXOR = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) == 0 ) { if ( bitwiseAND == - 1 ) bitwiseAND = a [ j ] ; else bitwiseAND &= a [ j ] ; bitwiseOR |= a [ j ] ; bitwiseXOR ^= a [ j ] ; } } if ( bitwiseAND == bitwiseOR && bitwiseOR == bitwiseXOR ) answer = ( answer + 1 ) % mod ; } return answer ; } public static void main ( String [ ] args ) { int N = 6 ; int A [ ] = { 1 , 3 , 2 , 1 , 2 , 1 } ; System . out . print ( countSubsets ( A , N ) ) ; } }
Count of Subsets containing only the given value K | Java implementation to find the number of subsets formed by the given value K ; Function to find the number of subsets formed by the given value K ; Count is used to maintain the number of continuous K 's ; Iterating through the array ; If the element in the array is equal to K ; count * ( count + 1 ) / 2 is the total number of subsets with only K as their element ; Change count to 0 because other element apart from K has been found ; To handle the last set of K 's ; Driver code
class GFG { static int count ( int arr [ ] , int N , int K ) { int count = 0 , ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == K ) { count = count + 1 ; } else { ans += ( count * ( count + 1 ) ) / 2 ; count = 0 ; } } ans = ans + ( count * ( count + 1 ) ) / 2 ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 0 , 0 , 1 , 1 , 0 , 0 } ; int N = arr . length ; int K = 0 ; System . out . print ( count ( arr , N , K ) ) ; } }
Ternary number system or Base 3 numbers | Java program to convert decimal number to ternary number ; Function to convert a decimal number to a ternary number ; Base case ; Finding the remainder when N is divided by 3 ; Recursive function to call the function for the integer division of the value N / 3 ; Handling the negative cases ; Function to convert the decimal to ternary ; If the number is greater than 0 , compute the ternary representation of the number ; Driver Code
import java . io . * ; class GFG { static void convertToTernary ( int N ) { if ( N == 0 ) return ; int x = N % 3 ; N /= 3 ; if ( x < 0 ) N += 1 ; convertToTernary ( N ) ; if ( x < 0 ) System . out . print ( x + ( 3 * - 1 ) ) ; else System . out . print ( x ) ; } static void convert ( int Decimal ) { System . out . print ( " Ternary ▁ number ▁ of ▁ " + Decimal + " ▁ is : ▁ " ) ; if ( Decimal != 0 ) { convertToTernary ( Decimal ) ; } else System . out . println ( "0" ) ; } public static void main ( String [ ] args ) { int Decimal = 2747 ; convert ( Decimal ) ; } }
Largest number less than or equal to Z that leaves a remainder X when divided by Y | Java implementation to Find the largest non - negative number that is less than or equal to integer Z and leaves a remainder X when divided by Y ; Function to get the number ; remainder can ' t ▁ be ▁ larger ▁ ▁ than ▁ the ▁ largest ▁ number , ▁ ▁ if ▁ so ▁ then ▁ answer ▁ doesn ' t exist . ; reduce number by x ; finding the possible number that is divisible by y ; this number is always <= x as we calculated over z - x ; Driver Code ; initialise the three integers
class GFG { static int get ( int x , int y , int z ) { if ( x > z ) return - 1 ; int val = z - x ; int div = ( z - x ) / y ; int ans = div * y + x ; return ans ; } public static void main ( String [ ] args ) { int x = 1 , y = 5 , z = 8 ; System . out . print ( get ( x , y , z ) + "NEW_LINE"); } }
Find the conjugate of a Complex number | Java implementation to Find the conjugate of a complex number ; Function to find conjugate of a complex number ; store index of ' + ' ; store index of ' - ' ; print the result ; Driver code ; initialise the complex number
class GFG { static void solve ( String s ) { String z = s ; int l = s . length ( ) ; int i ; String str ; if ( s . indexOf ( ' + ' ) != - 1 ) { i = s . indexOf ( ' + ' ) ; str = s . replace ( ' + ' , ' - ' ) ; } else { i = s . indexOf ( ' - ' ) ; str = s . replace ( ' - ' , ' + ' ) ; } System . out . println ( " Conjugate ▁ of ▁ " + z + " ▁ = ▁ " + str ) ; } public static void main ( String [ ] args ) { String s = "3-4i " ; solve ( s ) ; } }
Count of greater elements for each element in the Array | Java implementation of the above approach ; Store the frequency of the array elements ; Store the sum of frequency of elements greater than the current element ; Driver code
import java . util . * ; class GfG { public static void countOfGreaterElements ( int arr [ ] ) { int n = arr . length ; TreeMap < Integer , Integer > mp = new TreeMap < Integer , Integer > ( Collections . reverseOrder ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) { mp . put ( arr [ i ] , mp . getOrDefault ( arr [ i ] , 0 ) + 1 ) ; } int x = 0 ; for ( Map . Entry < Integer , Integer > e : mp . entrySet ( ) ) { Integer temp = e . getValue ( ) ; mp . put ( e . getKey ( ) , x ) ; x += temp ; } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( mp . get ( arr [ i ] ) + " ▁ " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 9 , 5 , 2 , 1 , 3 , 4 , 8 , 6 } ; countOfGreaterElements ( arr ) ; } }
Minimum operations required to make two numbers equal | Java program to find minimum operations required to make two numbers equal ; Function to return the minimum operations required ; Keeping B always greater ; Reduce B such that gcd ( A , B ) becomes 1. ; Driver code
class GFG { static int minOperations ( int A , int B ) { if ( A > B ) { A = A + B ; B = A - B ; A = A - B ; } B = B / __gcd ( A , B ) ; return B - 1 ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int A = 7 , B = 15 ; System . out . print ( minOperations ( A , B ) + "NEW_LINE"); } }
Program to determine the Quadrant of a Complex number | Java program to determine the quadrant of a complex number ; Function to determine the quadrant of a complex number ; Storing the index of ' + ' ; Storing the index of ' - ' ; Finding the real part of the complex number ; Finding the imaginary part of the complex number ; Driver code
import java . util . * ; class GFG { static void quadrant ( String s ) { int l = s . length ( ) ; int i ; if ( s . contains ( " + " ) ) { i = s . indexOf ( ' + ' ) ; } else { i = s . indexOf ( ' - ' ) ; } String real = s . substring ( 0 , i ) ; String imaginary = s . substring ( i + 1 , l - 1 ) ; int x = Integer . valueOf ( real ) ; int y = Integer . valueOf ( imaginary ) ; if ( x > 0 && y > 0 ) System . out . print ( " Quadrant ▁ 1" ) ; else if ( x < 0 && y > 0 ) System . out . print ( " Quadrant ▁ 2" ) ; else if ( x < 0 && y < 0 ) System . out . print ( " Quadrant ▁ 3" ) ; else if ( x > 0 && y < 0 ) System . out . print ( " Quadrant ▁ 4" ) ; else if ( x == 0 && y > 0 ) System . out . print ( " Lies ▁ on ▁ positive " + " ▁ Imaginary ▁ axis " ) ; else if ( x == 0 && y < 0 ) System . out . print ( " Lies ▁ on ▁ negative " + " ▁ Imaginary ▁ axis " ) ; else if ( y == 0 && x < 0 ) System . out . print ( " Lies ▁ on ▁ negative " + " ▁ X - axis " ) ; else if ( y == 0 && x > 0 ) System . out . print ( " Lies ▁ on ▁ positive " + " ▁ X - axis " ) ; else System . out . print ( " Lies ▁ on ▁ the ▁ Origin " ) ; } public static void main ( String [ ] args ) { String s = "5 + 3i " ; quadrant ( s ) ; } }
Sum and Product of all Fibonacci Nodes of a Singly Linked List | Java implementation to find the sum and product of all of the Fibonacci nodes in a singly linked list ; Node of the singly linked list ; Function to insert a node at the beginning of the singly Linked List ; Allocate new node ; Insert the data ; Link the old list to the new node ; Move the head to point the new node ; Function that returns the largest element from the linked list . ; Declare a max variable and initialize with Integer . MIN_VALUE ; Check loop while head not equal to null ; If max is less then head . data then assign value of head . data to max otherwise node points to next node . ; Function to create a hash table to check Fibonacci numbers ; Inserting the first two numbers in the hash ; Loop to add Fibonacci numbers upto the maximum element present in the linked list ; Function to find the required sum and product ; Find the largest node value in Singly Linked List ; Creating a set containing all the fibonacci numbers upto the maximum data value in the Singly Linked List ; Traverse the linked list ; If current node is fibonacci ; Find the sum and the product ; Driver code ; Create the linked list 15.16 . 8.6 .13
import java . util . * ; class GFG { static class Node { int data ; Node next ; } ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static int largestElement ( Node head_ref ) { int max = Integer . MIN_VALUE ; Node head = head_ref ; while ( head != null ) { if ( max < head . data ) max = head . data ; head = head . next ; } return max ; } static void createHash ( HashSet < Integer > hash , int maxElement ) { int prev = 0 , curr = 1 ; hash . add ( prev ) ; hash . add ( curr ) ; while ( curr <= maxElement ) { int temp = curr + prev ; hash . add ( temp ) ; prev = curr ; curr = temp ; } } static void sumAndProduct ( Node head_ref ) { int maxEle = largestElement ( head_ref ) ; HashSet < Integer > hash = new HashSet < Integer > ( ) ; createHash ( hash , maxEle ) ; int prod = 1 ; int sum = 0 ; Node ptr = head_ref ; while ( ptr != null ) { if ( hash . contains ( ptr . data ) ) { prod *= ptr . data ; sum += ptr . data ; } ptr = ptr . next ; } System . out . print ( " Sum ▁ = ▁ " + sum + "NEW_LINE"); System . out . print ( " Product ▁ = ▁ " + prod ) ; } public static void main ( String [ ] args ) { Node head = null ; head = push ( head , 13 ) ; head = push ( head , 6 ) ; head = push ( head , 8 ) ; head = push ( head , 16 ) ; head = push ( head , 15 ) ; sumAndProduct ( head ) ; } }
Product of all Subarrays of an Array | Java program to find product of all subarray of an array ; Function to find product of all subarrays ; Variable to store the product ; Compute the product while traversing for subarrays ; Printing product of all subarray ; Driver code ; Function call
import java . util . * ; class GFG { static void product_subarrays ( int arr [ ] , int n ) { int product = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { for ( int k = i ; k <= j ; k ++ ) product *= arr [ k ] ; } } System . out . print ( product + "NEW_LINE"); } public static void main ( String args [ ] ) { int arr [ ] = { 10 , 3 , 7 } ; int n = arr . length ; product_subarrays ( arr , n ) ; } }