codeparrot/xlcost-text-to-code huggingface dataset:
Find Kth number from sorted array formed by multiplying any two numbers in the array | Java implementation to find the Kth number in the list formed from product of any two numbers in the array and sorting them ; Function to find number of pairs ; Negative and Negative ; Add Possible Pairs ; Positive and Positive ; Add Possible pairs ; Negative and Positive ; Add Possible pairs ; Function to find the kth element in the list ; Separate Positive and Negative elements ; Sort the Elements ; Binary search ; Return the required answer ; Driver code ; Function call
import java . util . * ; class GFG { static boolean check ( int x , Vector pos , Vector neg , int k ) { int pairs = 0 ; int p = neg . size ( ) - 1 ; int nn = neg . size ( ) - 1 ; int pp = pos . size ( ) - 1 ; for ( int i = 0 ; i < neg . size ( ) ; i ++ ) { while ( ( p >= 0 ) && ( ( int ) neg . get ( i ) * ( int ) neg . get ( p ) <= x ) ) p -- ; pairs += Math . min ( nn - p , nn - i ) ; } p = 0 ; for ( int i = pos . size ( ) - 1 ; i >= 0 ; i -- ) { while ( ( p < pos . size ( ) ) && ( ( int ) pos . get ( i ) * ( int ) pos . get ( p ) <= x ) ) p ++ ; pairs += Math . min ( p , i ) ; } p = pos . size ( ) - 1 ; for ( int i = neg . size ( ) - 1 ; i >= 0 ; i -- ) { while ( ( p >= 0 ) && ( ( int ) neg . get ( i ) * ( int ) pos . get ( p ) <= x ) ) p -- ; pairs += pp - p ; } return ( pairs >= k ) ; } static int kth_element ( int a [ ] , int n , int k ) { Vector pos = new Vector ( ) ; Vector neg = new Vector ( ) ; ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= 0 ) pos . add ( a [ i ] ) ; else neg . add ( a [ i ] ) ; } sort ( pos . begin ( ) , pos . end ( ) ) ; sort ( neg . begin ( ) , neg . end ( ) ) ; Collections . sort ( pos ) ; Collections . sort ( neg ) ; int l = ( int ) - 1e8 , ans = 0 , r = ( int ) 1e8 ; while ( l <= r ) { int mid = ( l + r ) >> 1 ; if ( check ( mid , pos , neg , k ) ) { ans = mid ; r = mid - 1 ; } else l = mid + 1 ; } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { - 4 , - 2 , 3 , 3 } , k = 3 ; int n = a . length ; System . out . println ( kth_element ( a , n , k ) ) ; } }
Find root of a number using Newton 's method | Java implementation of the approach ; Function to return the square root of a number using Newtons method ; Assuming the sqrt of n as n only ; The closed guess will be stored in the root ; To count the number of iterations ; Calculate more closed x ; Check for closeness ; Update root ; Driver code
class GFG { static double squareRoot ( double n , double l ) { double x = n ; double root ; int count = 0 ; while ( true ) { count ++ ; root = 0.5 * ( x + ( n / x ) ) ; if ( Math . abs ( root - x ) < l ) break ; x = root ; } return root ; } public static void main ( String [ ] args ) { double n = 327 ; double l = 0.00001 ; System . out . println ( squareRoot ( n , l ) ) ; } }
Count the occurrence of Nth term in first N terms of Van Eck 's sequence | Java program to count the occurrence of nth term in first n terms of Van Eck 's sequence ; Utility function to compute Van Eck 's sequence ; Initialize sequence array ; Loop to generate sequence ; Check if sequence [ i ] has occured previously or is new to sequence ; If occurrence found then the next term will be how far back this last term occured previously ; Utility function to count the occurrence of nth term in first n terms of the sequence ; Get nth term of the sequence ; Count the occurrence of nth term in first n terms of the sequence ; Return count ; Driver code ; Pre - compute Van Eck 's sequence ; Print count of the occurrence of nth term in first n terms of the sequence ; Print count of the occurrence of nth term in first n terms of the sequence
class GFG { static int MAX = 100000 ; static int sequence [ ] = new int [ MAX + 1 ] ; static void vanEckSequence ( ) { for ( int i = 0 ; i < MAX ; i ++ ) { sequence [ i ] = 0 ; } for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = i - 1 ; j >= 0 ; j -- ) { if ( sequence [ j ] == sequence [ i ] ) { sequence [ i + 1 ] = i - j ; break ; } } } } static int getCount ( int n ) { int nthTerm = sequence [ n - 1 ] ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( sequence [ i ] == nthTerm ) count ++ ; } return count ; } public static void main ( String [ ] args ) { vanEckSequence ( ) ; int n = 5 ; System . out . println ( getCount ( n ) ) ; n = 11 ; System . out . println ( getCount ( n ) ) ; } }
Count the occurrence of Nth term in first N terms of Van Eck 's sequence | Java program to count the occurrence of nth term in first n terms of Van Eck 's sequence ; Utility function to compute Van Eck 's sequence ; Initialize sequence array ; Loop to generate sequence ; Check if sequence [ i ] has occured previously or is new to sequence ; If occurrence found then the next term will be how far back this last term occured previously ; Utility function to count the occurrence of nth term in first n terms of the sequence ; Initialize count as 1 ; Increment count if ( i + 1 ) th term is non - zero ; Previous occurrence of sequence [ i ] will be it ( i - sequence [ i + 1 ] ) th position ; Return the count of occurrence ; Driver code ; Pre - compute Van Eck 's sequence ; Print count of the occurrence of nth term in first n terms of the sequence ; Print count of the occurrence of nth term in first n terms of the sequence
class GFG { static int MAX = 100000 ; static int sequence [ ] = new int [ MAX + 1 ] ; static void vanEckSequence ( ) { for ( int i = 0 ; i < MAX ; i ++ ) { sequence [ i ] = 0 ; } for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = i - 1 ; j >= 0 ; j -- ) { if ( sequence [ j ] == sequence [ i ] ) { sequence [ i + 1 ] = i - j ; break ; } } } } static int getCount ( int n ) { int count = 1 ; int i = n - 1 ; while ( sequence [ i + 1 ] != 0 ) { count ++ ; i = i - sequence [ i + 1 ] ; } return count ; } public static void main ( String [ ] args ) { vanEckSequence ( ) ; int n = 5 ; System . out . println ( getCount ( n ) ) ; n = 11 ; System . out . println ( getCount ( n ) ) ; } }
Program to find the time remaining for the day to complete | Java program to Number of pairs of lines having integer intersection points ; Function to find the remaining time ; Formula for total remaining minutes = 1440 - 60 h - m ; Remaining hours ; Remaining minutes ; Driver code ; Current time ; Get the remaining time
class GFG { static void remainingTime ( int h , int m ) { int totalMin , hoursRemaining , minRemaining ; totalMin = 1440 - 60 * h - m ; hoursRemaining = totalMin / 60 ; minRemaining = totalMin % 60 ; System . out . print ( hoursRemaining + " : : " + minRemaining + "NEW_LINE"); } public static void main ( String [ ] args ) { int h = 0 , m = 1 ; remainingTime ( h , m ) ; } }
Goldbach 's Weak Conjecture for Odd numbers | Function to check if a number can be represent as as a sum of 3 prime ; Driver code
class GFG { static void check ( int n ) { if ( n % 2 == 1 && n > 5 ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } public static void main ( String [ ] args ) { int a = 3 ; int b = 7 ; check ( a ) ; check ( b ) ; } }
Number of ways to reach ( X , Y ) in a matrix starting from the origin | Java implementation of the approach ; To store the factorial and factorial mod inverse of the numbers ; Function to find ( a ^ m1 ) % mod ; Function to find the factorial of all the numbers ; Function to find the factorial modinverse of all the numbers ; Function to return nCr ; Function to return the number of ways to reach ( X , Y ) in a matrix with the given moves starting from the origin ; Driver code
import java . util . * ; class GFG { static long [ ] factorial = new long [ 1000005 ] ; static long [ ] modinverse = new long [ 1000005 ] ; static long mod = 1000000007 ; static int N = 1000005 ; static long power ( long a , long m1 ) { if ( m1 == 0 ) return 1 ; else if ( m1 == 1 ) return a ; else if ( m1 == 2 ) return ( a * a ) % mod ; else if ( ( m1 & 1 ) != 0 ) return ( a * power ( power ( a , m1 / 2 ) , 2 ) ) % mod ; else return power ( power ( a , m1 / 2 ) , 2 ) % mod ; } static void factorialfun ( ) { factorial [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) factorial [ i ] = ( factorial [ i - 1 ] * i ) % mod ; } static void modinversefun ( ) { modinverse [ N - 1 ] = power ( factorial [ N - 1 ] , mod - 2 ) % mod ; for ( int i = N - 2 ; i >= 0 ; i -- ) modinverse [ i ] = ( modinverse [ i + 1 ] * ( i + 1 ) ) % mod ; } static long binomial ( int n , int r ) { if ( r > n ) return 0 ; long a = ( factorial [ n ] * modinverse [ n - r ] ) % mod ; a = ( a * modinverse [ r ] ) % mod ; return a ; } static long ways ( long x , long y ) { factorialfun ( ) ; modinversefun ( ) ; if ( ( 2 * x - y ) % 3 == 0 && ( 2 * y - x ) % 3 == 0 ) { long m = ( 2 * x - y ) / 3 ; long n = ( 2 * y - x ) / 3 ; System . out . println ( n + m + " ▁ " + n ) ; return binomial ( ( int ) ( n + m ) , ( int ) n ) ; } return 0 ; } public static void main ( String [ ] args ) { long x = 3 , y = 3 ; System . out . println ( ways ( x , y ) ) ; } }
Find Nth even length palindromic number formed using digits X and Y | Java program to find nth even palindromic number of only even length composing of 4 ' s ▁ and ▁ 5' s . ; Utility function to compute n 'th palindrome number  ; Calculate the length from above formula as discussed above ; Calculate rank for length L ; Mask to check if i 't bit   is set or not  ; If bit is set append '5' else append '4' ; Driver Code
import java . util . * ; class GFG { static String solve ( int n , char x , char y ) { int length = ( int ) Math . ceil ( Math . log ( n + 2 ) / Math . log ( 2 ) ) - 1 ; int rank = n - ( 1 << length ) + 1 ; String left = " " , right = " " ; for ( int i = length - 1 ; i >= 0 ; i -- ) { int mask = ( 1 << i ) ; int bit = mask & rank ; if ( bit > 0 ) { left += y ; right += y ; } else { left += x ; right += x ; } } StringBuilder sb = new StringBuilder ( right ) ; sb . reverse ( ) ; right = sb . toString ( ) ; String res = left + right ; return res ; } public static void main ( String [ ] args ) { int n = 23 ; char x = '4' , y = '5' ; String ans = solve ( n , x , y ) ; System . out . println ( ans ) ; } }
Maximum of all the integers in the given level of Pascal triangle | Java implementation of the approach ; Function for the binomial coefficient ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Function to return the maximum value in the nth level of the Pascal 's triangle ; Driver code
class GFG { static int binomialCoeff ( int n , int k ) { int [ ] [ ] C = new int [ n + 1 ] [ k + 1 ] ; int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } return C [ n ] [ k ] ; } static int findMax ( int n ) { return binomialCoeff ( n , n / 2 ) ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( findMax ( n ) ) ; } }
Find two co | Java implementation of the approach ;  ; Function to find the required numbers ; GCD of the given numbers ; Printing the required numbers ; Driver code
import java . math . * ; class GFG { public static int findGCD ( int a , int b ) { if ( b == 0 ) return a ; else return findGCD ( b , a % b ) ; } static void findNumbers ( int a , int b ) { int gcd = findGCD ( a , b ) ; System . out . println ( ( a / gcd ) + " ▁ " + ( b / gcd ) ) ; } public static void main ( String [ ] args ) { int a = 12 , b = 16 ; findNumbers ( a , b ) ; } }
Find the remaining balance after the transaction | Java program to find the remaining balance ; Function to find the balance ; Check if the transaction can be successful or not ; Transaction is successful ; Transaction is unsuccessful ; Driver Code
import java . util . * ; class GFG { static void findBalance ( int x , float bal ) { if ( x % 10 == 0 && ( ( float ) x + 1.50 ) <= bal ) { System . out . printf ( "%.2fNEW_LINE", bal - x - 1.50); } else { System . out . printf ( "%.2fNEW_LINE", bal); } } public static void main ( String [ ] args ) { int x = 50 ; float bal = ( float ) 100.50 ; findBalance ( x , bal ) ; } }
Count number of pairs in array having sum divisible by K | SET 2 | JAVA Program to count pairs whose sum divisible by ' K ' ; Program to count pairs whose sum divisible by ' K ' ; Create a frequency array to count occurrences of all remainders when divided by K ; To store count of pairs . ; Traverse the array , compute the remainder and add k - remainder value hash count to ans ; Count number of ( A [ i ] , ( K - rem ) % K ) pairs ; Increment count of remainder in hash map ; Driver code
class GFG { static int countKdivPairs ( int A [ ] , int n , int K ) { int [ ] freq = new int [ K ] ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int rem = A [ i ] % K ; ans += freq [ ( K - rem ) % K ] ; freq [ rem ] ++ ; } return ans ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 2 , 1 , 7 , 5 , 3 } ; int n = A . length ; int K = 4 ; System . out . println ( countKdivPairs ( A , n , K ) ) ; } }
Minimum number of Bottles visible when a bottle can be enclosed inside another Bottle | Java code for the above approach ; Driver code ; Find the solution
import java . util . * ; class GFG { static void min_visible_bottles ( int [ ] arr , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } ans = Math . max ( ans , mp . get ( arr [ i ] ) ) ; } System . out . print ( " Minimum ▁ number ▁ of ▁ " + " Visible ▁ Bottles ▁ are : ▁ " + ans + "NEW_LINE"); } public static void main ( String [ ] args ) { int n = 8 ; int arr [ ] = { 1 , 1 , 2 , 3 , 4 , 5 , 5 , 4 } ; min_visible_bottles ( arr , n ) ; } }
Number of even substrings in a string of digits | Java program to count number of substring which are even integer in a string of digits . ; Return the even number substrings . ; If current digit is even , add count of substrings ending with it . The count is ( i + 1 ) ; Driver code
public class GFG { static int evenNumSubstring ( String str ) { int len = str . length ( ) ; int count = 0 ; for ( int i = 0 ; i < len ; i ++ ) { int temp = str . charAt ( i ) - '0' ; if ( temp % 2 == 0 ) count += ( i + 1 ) ; } return count ; } public static void main ( String args [ ] ) { String str = "1234" ; System . out . println ( evenNumSubstring ( str ) ) ; } }
Remove Nth node from end of the Linked List | Java program to delete nth node from last ; Structure of node ; Function to insert node in a linked list ; Function to remove nth node from last ; To store length of the linked list ; B > length , then we can 't remove node ; We need to remove head node ; Return head . next ; Remove len - B th node from starting ; This function prints contents of linked list starting from the given node ; Driver code
class GFG { static class Node { int data ; Node next ; } ; static Node create ( Node head , int x ) { Node temp , ptr = head ; temp = new Node ( ) ; temp . data = x ; temp . next = null ; if ( head == null ) head = temp ; else { while ( ptr . next != null ) { ptr = ptr . next ; } ptr . next = temp ; } return head ; } static Node removeNthFromEnd ( Node head , int B ) { int len = 0 ; Node tmp = head ; while ( tmp != null ) { len ++ ; tmp = tmp . next ; } if ( B > len ) { System . out . print ( " Length ▁ of ▁ the ▁ linked ▁ list ▁ is ▁ " + len ) ; System . out . print ( " ▁ we ▁ can ' t ▁ remove ▁ " + B + " th ▁ node ▁ from ▁ the " ) ; System . out . print ( " linked listNEW_LINE"); return head ; } else if ( B == len ) { return head . next ; } else { int diff = len - B ; Node prev = null ; Node curr = head ; for ( int i = 0 ; i < diff ; i ++ ) { prev = curr ; curr = curr . next ; } prev . next = curr . next ; return head ; } } static void display ( Node head ) { Node temp = head ; while ( temp != null ) { System . out . print ( temp . data + " ▁ " ) ; temp = temp . next ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { Node head = null ; head = create ( head , 1 ) ; head = create ( head , 2 ) ; head = create ( head , 3 ) ; head = create ( head , 4 ) ; head = create ( head , 5 ) ; int n = 2 ; System . out . print ( "Linked list before modification: NEW_LINE"); display ( head ) ; head = removeNthFromEnd ( head , 2 ) ; System . out . print ( "Linked list after modification: NEW_LINE"); display ( head ) ; } }
Find the final sequence of the array after performing given operations | Java implementation of the approach ; Function that generates the array b [ ] when n is even ; Fill the first half of the final array with reversed sequence ; Fill the second half ; Function that generates the array b [ ] when n is odd ; Fill the first half of the final array with reversed sequence ; Fill the second half ; Function to find the final array b [ ] after n operations of given type ; Create the array b ; If the array size is even ; Print the final array elements ; Driver code
import java . io . * ; class GFG { static void solveEven ( int n , int arr [ ] , int b [ ] ) { int left = n - 1 ; for ( int i = 0 ; i < ( n / 2 ) ; ++ i ) { b [ i ] = arr [ left ] ; left = left - 2 ; if ( left < 0 ) break ; } int right = 0 ; for ( int i = n / 2 ; i <= n - 1 ; ++ i ) { b [ i ] = arr [ right ] ; right = right + 2 ; if ( right > n - 2 ) break ; } } static void solveOdd ( int n , int arr [ ] , int b [ ] ) { int left = n - 1 ; for ( int i = 0 ; i < ( n / 2 ) + 1 ; ++ i ) { b [ i ] = arr [ left ] ; left = left - 2 ; if ( left < 0 ) break ; } int right = 1 ; for ( int i = ( n / 2 ) + 1 ; i <= n - 1 ; ++ i ) { b [ i ] = arr [ right ] ; right = right + 2 ; if ( right > n - 2 ) break ; } } static void solve ( int n , int arr [ ] ) { int b [ ] = new int [ n ] ; if ( n % 2 == 0 ) solveEven ( n , arr , b ) ; else solveOdd ( n , arr , b ) ; for ( int i = 0 ; i <= n - 1 ; ++ i ) { System . out . print ( b [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . length ; solve ( n , arr ) ; } }
Find the sum of the first half and second half elements of an array | Java program to count pairs whose sum divisible by ' K ' ; Function to find the sum of the first half elements and second half elements of an array ; Add elements in first half sum ; Add elements in the second half sum ; Driver code ; Function call
import java . util . * ; class GFG { public static void sum_of_elements ( int [ ] arr , int n ) { int sumfirst = 0 , sumsecond = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < n / 2 ) { sumfirst += arr [ i ] ; } else { sumsecond += arr [ i ] ; } } System . out . println ( " Sum ▁ of ▁ first ▁ half ▁ elements ▁ is ▁ " + sumfirst ) ; System . out . println ( " Sum ▁ of ▁ second ▁ half ▁ elements ▁ is ▁ " + sumsecond ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 20 , 30 , 60 , 10 , 25 , 15 , 40 } ; int n = arr . length ; sum_of_elements ( arr , n ) ; } }
Sequence with sum K and minimum sum of absolute differences between consecutive elements | Java implementation of the approach ; Function to return the minimized sum ; If k is divisible by n then the answer will be 0 ; Else the answer will be 1 ; Driver code
import java . io . * ; class GFG { static int minimum_sum ( int n , int k ) { if ( k % n == 0 ) return 0 ; return 1 ; } public static void main ( String [ ] args ) { int n = 3 , k = 56 ; System . out . println ( minimum_sum ( n , k ) ) ; } }
Print first N terms of Lower Wythoff sequence | Java implementation of the approach ; Function to print the first n terms of the lower Wythoff sequence ; Calculate value of phi ; Find the numbers ; a ( n ) = floor ( n * phi ) ; Print the nth numbers ; Driver code
class GFG { static void lowerWythoff ( int n ) { double phi = ( 1 + Math . sqrt ( 5 ) ) / 2.0 ; for ( int i = 1 ; i <= n ; i ++ ) { double ans = Math . floor ( i * phi ) ; System . out . print ( ( int ) ans ) ; if ( i != n ) System . out . print ( " ▁ , ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 5 ; lowerWythoff ( n ) ; } }
Create new linked list from two given linked list with greater element at each node | Java program to create a new linked list from two given linked list of the same size with the greater element among the two at each node ; Representation of node ; Function to insert node in a linked list ; Function which returns new linked list ; Compare for greater node ; Driver code ; First linked list ; Second linked list
import java . util . * ; class GFG { static class Node { int data ; Node next ; } ; static Node insert ( Node root , int item ) { Node ptr , temp ; temp = new Node ( ) ; temp . data = item ; temp . next = null ; if ( root == null ) root = temp ; else { ptr = root ; while ( ptr . next != null ) ptr = ptr . next ; ptr . next = temp ; } return root ; } static Node newList ( Node root1 , Node root2 ) { Node ptr1 = root1 , ptr2 = root2 , ptr ; Node root = null , temp ; while ( ptr1 != null ) { temp = new Node ( ) ; temp . next = null ; if ( ptr1 . data < ptr2 . data ) temp . data = ptr2 . data ; else temp . data = ptr1 . data ; if ( root == null ) root = temp ; else { ptr = root ; while ( ptr . next != null ) ptr = ptr . next ; ptr . next = temp ; } ptr1 = ptr1 . next ; ptr2 = ptr2 . next ; } return root ; } static void display ( Node root ) { while ( root != null ) { System . out . print ( root . data + " - > " ) ; root = root . next ; } System . out . println ( ) ; } public static void main ( String args [ ] ) { Node root1 = null , root2 = null , root = null ; root1 = insert ( root1 , 5 ) ; root1 = insert ( root1 , 2 ) ; root1 = insert ( root1 , 3 ) ; root1 = insert ( root1 , 8 ) ; System . out . print ( " First ▁ List : ▁ " ) ; display ( root1 ) ; root2 = insert ( root2 , 1 ) ; root2 = insert ( root2 , 7 ) ; root2 = insert ( root2 , 4 ) ; root2 = insert ( root2 , 5 ) ; System . out . print ( " Second ▁ List : ▁ " ) ; display ( root2 ) ; root = newList ( root1 , root2 ) ; System . out . print ( " New ▁ List : ▁ " ) ; display ( root ) ; } }
Count of quadruplets from range [ L , R ] having GCD equal to K | Java implementation of the approach ; Function to return the gcd of a and b ; Function to return the count of quadruplets having gcd = k ; To store the required count ; Check every quadruplet pair whether its gcd is k ; Return the required count ; Driver code
import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int countQuadruplets ( int l , int r , int k ) { int count = 0 ; for ( int u = l ; u <= r ; u ++ ) { for ( int v = l ; v <= r ; v ++ ) { for ( int w = l ; w <= r ; w ++ ) { for ( int x = l ; x <= r ; x ++ ) { if ( gcd ( gcd ( u , v ) , gcd ( w , x ) ) == k ) count ++ ; } } } } return count ; } public static void main ( String [ ] args ) { int l = 1 , r = 10 , k = 2 ; System . out . println ( countQuadruplets ( l , r , k ) ) ; } }
Find an index such that difference between product of elements before and after it is minimum | Java implementation of the approach ; Function to return the index i such that the absolute difference between product of elements up to that index and the product of rest of the elements of the array is minimum ; To store the required index ; Prefix product array ; Compute the product array ; Iterate the product array to find the index ; Driver code
class GFG { static int findIndex ( int a [ ] , int n ) { int res = 0 ; long min_diff = Long . MAX_VALUE ; long prod [ ] = new long [ n ] ; prod [ 0 ] = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prod [ i ] = prod [ i - 1 ] * a [ i ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { long curr_diff = Math . abs ( ( prod [ n - 1 ] / prod [ i ] ) - prod [ i ] ) ; if ( curr_diff < min_diff ) { min_diff = curr_diff ; res = i ; } } return res ; } public static void main ( String arg [ ] ) { int arr [ ] = { 3 , 2 , 5 , 7 , 2 , 9 } ; int N = arr . length ; System . out . println ( findIndex ( arr , N ) ) ; } }