codeparrot/xlcost-text-to-code huggingface dataset:
Count the subarray with sum strictly greater than the sum of remaining elements | Java implementation of the above approach ; Function to count the number of sub - arrays with sum strictly greater than the remaining elements of array ; For loop for beginning point of a subarray ; For loop for ending point of the subarray ; Initialise subarray_sum and remaining_sum to 0 ; For loop to calculate the sum of generated subarray ; For loop to calculate the sum remaining array element ; Checking for condition when subarray sum is strictly greater than remaining sum of array element ; Driver code
import java . util . * ; class GFG { static int Count_subarray ( int arr [ ] , int n ) { int subarray_sum , remaining_sum , count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { subarray_sum = 0 ; remaining_sum = 0 ; for ( int k = i ; k <= j ; k ++ ) { subarray_sum += arr [ k ] ; } for ( int l = 0 ; l < i ; l ++ ) { remaining_sum += arr [ l ] ; } for ( int l = j + 1 ; l < n ; l ++ ) { remaining_sum += arr [ l ] ; } if ( subarray_sum > remaining_sum ) { count += 1 ; } } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 9 , 12 , 6 } ; int n = arr . length ; System . out . print ( Count_subarray ( arr , n ) ) ; } }
Count the subarray with sum strictly greater than the sum of remaining elements | Java implementation of the above approach ; Calculating total sum of given array ; For loop for beginning point of a subarray ; initialise subarray_sum to 0 ; For loop for calculating subarray_sum and remaining_sum ; Calculating subarray_sum and corresponding remaining_sum ; Checking for the condition when subarray sum is strictly greater than the remaining sum of the array element ; Driver code
class GFG { static int Count_subarray ( int arr [ ] , int n ) { int total_sum = 0 , subarray_sum , remaining_sum , count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { total_sum += arr [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { subarray_sum = 0 ; for ( int j = i ; j < n ; j ++ ) { subarray_sum += arr [ j ] ; remaining_sum = total_sum - subarray_sum ; if ( subarray_sum > remaining_sum ) { count += 1 ; } } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 9 , 12 , 6 } ; int n = arr . length ; System . out . print ( Count_subarray ( arr , n ) ) ; } }
Remove one element to get maximum XOR | Java implementation of the approach ; Function to return the maximized XOR after removing an element from the array ; Find XOR of the complete array ; To store the final answer ; Iterating through the array to find the final answer ; Return the final answer ; Driver code
class GFG { static int maxXOR ( int arr [ ] , int n ) { int xorArr = 0 ; for ( int i = 0 ; i < n ; i ++ ) xorArr ^= arr [ i ] ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans = Math . max ( ans , ( xorArr ^ arr [ i ] ) ) ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 3 } ; int n = arr . length ; System . out . println ( maxXOR ( arr , n ) ) ; } }
Count of numbers from the range [ L , R ] which contains at least one digit that divides K | Java implementation of the approach ; Function that returns true if num contains at least one digit that divides k ; Get the last digit ; If the digit is non - zero and it divides k ; Remove the last digit ; There is no digit in num that divides k ; Function to return the required count of elements from the given range which contain at least one digit that divides k ; To store the result ; For every number from the range ; If any digit of the current number divides k ; Driver code
class GFG { static boolean digitDividesK ( int num , int k ) { while ( num != 0 ) { int d = num % 10 ; if ( d != 0 && k % d == 0 ) return true ; num = num / 10 ; } return false ; } static int findCount ( int l , int r , int k ) { int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { if ( digitDividesK ( i , k ) ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int l = 20 , r = 35 ; int k = 45 ; System . out . println ( findCount ( l , r , k ) ) ; } }
Check if a given number is factorial of any number | Java implementation for the above approach ; Function to check if the given number is a factorial of any number ; Driver Code
class GFG { static boolean isFactorial ( int n ) { for ( int i = 1 ; ; i ++ ) { if ( n % i == 0 ) { n /= i ; } else { break ; } } if ( n == 1 ) { return true ; } else { return false ; } } public static void main ( String [ ] args ) { int n = 24 ; boolean ans = isFactorial ( n ) ; if ( ans == true ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Remove an element to minimize the LCM of the given array | Java implementation of the above approach ; Function to return the LCM of two numbers ; Function to return the minimum LCM after removing a single element from the given array ; Prefix and Suffix arrays ; Single state dynamic programming relation for storing LCM of first i elements from the left in Prefix [ i ] ; Initializing Suffix array ; Single state dynamic programming relation for storing LCM of all the elements having index greater than or equal to i in Suffix [ i ] ; If first or last element of the array has to be removed ; If any other element is replaced ; Return the minimum LCM ; Driver code
class GFG { static int lcm ( int a , int b ) { int GCD = __gcd ( a , b ) ; return ( a * b ) / GCD ; } static int MinLCM ( int a [ ] , int n ) { int [ ] Prefix = new int [ n + 2 ] ; int [ ] Suffix = new int [ n + 2 ] ; Prefix [ 1 ] = a [ 0 ] ; for ( int i = 2 ; i <= n ; i += 1 ) { Prefix [ i ] = lcm ( Prefix [ i - 1 ] , a [ i - 1 ] ) ; } Suffix [ n ] = a [ n - 1 ] ; for ( int i = n - 1 ; i >= 1 ; i -= 1 ) { Suffix [ i ] = lcm ( Suffix [ i + 1 ] , a [ i - 1 ] ) ; } int ans = Math . min ( Suffix [ 2 ] , Prefix [ n - 1 ] ) ; for ( int i = 2 ; i < n ; i += 1 ) { ans = Math . min ( ans , lcm ( Prefix [ i - 1 ] , Suffix [ i + 1 ] ) ) ; } return ans ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int a [ ] = { 5 , 15 , 9 , 36 } ; int n = a . length ; System . out . println ( MinLCM ( a , n ) ) ; } }
Number of coloured 0 's in an N | Java implementation of the approach ; Function to return the count of coloured 0 s in an n - level hexagon ; Driver code
class GFG { static int count ( int n ) { return n * ( 3 * n - 1 ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( count ( n ) ) ; } }
Minimum value to be assigned to the elements so that sum becomes greater than initial sum | Java implementation of the approach ; Function to return the minimum required value ; Find the sum of the array elements ; Return the required value ; Driver code
class GFG { static int findMinValue ( int arr [ ] , int n ) { long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return ( ( int ) ( sum / n ) + 1 ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 4 , 2 , 1 , 10 , 6 } ; int n = arr . length ; System . out . print ( findMinValue ( arr , n ) ) ; } }
Color all boxes in line such that every M consecutive boxes are unique | Java implementation of the above approach ; Function to return ( m ! % MOD ) ; Driver code
class GFG { static final int MOD = 1000000007 ; static int modFact ( int n , int m ) { int result = 1 ; for ( int i = 1 ; i <= m ; i ++ ) result = ( result * i ) % MOD ; return result ; } public static void main ( String [ ] args ) { int n = 3 , m = 2 ; System . out . println ( modFact ( n , m ) ) ; } }
Sum of squares of all Subsets of given Array | Java implementation of the approach ; Function to return ( 2 ^ P % mod ) ; Function to return the sum of squares of subsets ; Sqauaring the elements and adding it to ans ; Driver code
class GFG { static final int mod = ( int ) ( 1e9 + 7 ) ; static long power ( int p ) { long res = 1 ; for ( int i = 1 ; i <= p ; ++ i ) { res *= 2 ; res %= mod ; } return res % mod ; } static long subset_square_sum ( int A [ ] ) { int n = A . length ; long ans = 0 ; for ( int i : A ) { ans += ( 1 * i * i ) % mod ; ans %= mod ; } return ( 1 * ans * power ( n - 1 ) ) % mod ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 7 } ; System . out . println ( subset_square_sum ( A ) ) ; } }
Find the number of pairs such that their gcd is equals to 1 | Java program to find the number of pairs such that gcd equals to 1 ; Function to calculate least prime factor of each number ; If it is a prime number ; For all multiples which are not visited yet . ; Function to find the value of Mobius function for all the numbers from 1 to n ; If number is one ; If number has a squared prime factor ; Multiply - 1 with the previous number ; Function to find the number of pairs such that gcd equals to 1 ; To store maximum number ; To store frequency of each number ; Find frequency and maximum number ; To store number of pairs with gcd equals to 1 ; Traverse through the all possible elements ; Return the number of pairs ; Driver code ; Function call
class GFG { static int N = 100050 ; static int [ ] lpf = new int [ N ] ; static int [ ] mobius = new int [ N ] ; static void least_prime_factor ( ) { for ( int i = 2 ; i < N ; i ++ ) if ( lpf [ i ] == 0 ) for ( int j = i ; j < N ; j += i ) if ( lpf [ j ] == 0 ) lpf [ j ] = i ; } static void Mobius ( ) { for ( int i = 1 ; i < N ; i ++ ) { if ( i == 1 ) mobius [ i ] = 1 ; else { if ( lpf [ i / lpf [ i ] ] == lpf [ i ] ) mobius [ i ] = 0 ; else mobius [ i ] = - 1 * mobius [ i / lpf [ i ] ] ; } } } static int gcd_pairs ( int a [ ] , int n ) { int maxi = 0 ; int [ ] fre = new int [ N ] ; for ( int i = 0 ; i < n ; i ++ ) { fre [ a [ i ] ] ++ ; maxi = Math . max ( a [ i ] , maxi ) ; } least_prime_factor ( ) ; Mobius ( ) ; int ans = 0 ; for ( int i = 1 ; i <= maxi ; i ++ ) { if ( mobius [ i ] == 0 ) continue ; int temp = 0 ; for ( int j = i ; j <= maxi ; j += i ) temp += fre [ j ] ; ans += temp * ( temp - 1 ) / 2 * mobius [ i ] ; } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = a . length ; System . out . print ( gcd_pairs ( a , n ) ) ; } }
Comparing X ^ Y and Y ^ X for very large values of X and Y | Java implementation of the approach ; Function to compare x ^ y and y ^ x ; Storing values OF x ^ y AND y ^ x ; Comparing values ; Driver code
import java . util . * ; class GFG { static void compareVal ( int x , int y ) { double a = y * Math . log ( x ) ; double b = x * Math . log ( y ) ; if ( a > b ) System . out . print ( x + " ^ " + y + " ▁ > ▁ " + y + " ^ " + x ) ; else if ( a < b ) System . out . print ( x + " ^ " + y + " ▁ < ▁ " + y + " ^ " + x ) ; else if ( a == b ) System . out . print ( x + " ^ " + y + " ▁ = ▁ " + y + " ^ " + x ) ; } public static void main ( String [ ] args ) { int x = 4 , y = 5 ; compareVal ( x , y ) ; } }
Euler zigzag numbers ( Alternating Permutation ) | Java program to find zigzag sequence ; Function to print first n zigzag numbers ; To store factorial and n 'th zig zag number ; Initialize factorial upto n ; Set first two zig zag numbers ; Print first two zig zag number ; Print the rest zig zag numbers ; Binomial ( n , k ) * a ( k ) * a ( n - k ) ; Store the value ; Print the number ; Driver code ; Function call
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static void ZigZag ( int n ) { long [ ] fact = new long [ n + 1 ] ; long [ ] zig = new long [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) zig [ i ] = 0 ; fact [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) fact [ i ] = fact [ i - 1 ] * i ; zig [ 0 ] = 1 ; zig [ 1 ] = 1 ; System . out . print ( " zig ▁ zag ▁ numbers : ▁ " ) ; System . out . print ( zig [ 0 ] + " ▁ " + zig [ 1 ] + " ▁ " ) ; for ( int i = 2 ; i < n ; i ++ ) { long sum = 0 ; for ( int k = 0 ; k <= i - 1 ; k ++ ) { sum += ( fact [ i - 1 ] / ( fact [ i - 1 - k ] * fact [ k ] ) ) * zig [ k ] * zig [ i - 1 - k ] ; } zig [ i ] = sum / 2 ; System . out . print ( sum / 2 + " ▁ " ) ; } } public static void main ( String [ ] args ) throws java . lang . Exception { int n = 10 ; ZigZag ( n ) ; } }
Gijswijt 's Sequence | Java program to demonstrate Gijswijt 's sequence ; if the sequence is a ( 1 ) a ( 2 ) a ( 3 ) . . a ( n - 1 ) check if the sequence can be represented as x * ( y ^ k ) find the largest value of k ; count ; pattern of elements of size i from the end of sequence ; count ; extract the pattern in a reverse order ; check how many times the pattern is repeated ; if the element dosent match ; if the end of pattern is reached set value of k = 0 and increase the count ; return the max count ; print first n terms of Gijswijt 's sequence ; set the count ; stoes the element ; print the first n terms of the sequence ; push the element ; find the count for next number ; Driver code
import java . util . * ; class GFG { static int find_count ( Vector < Integer > ele ) { int count = 0 ; for ( int i = 0 ; i < ele . size ( ) ; i ++ ) { Vector < Integer > p = new Vector < Integer > ( ) ; int c = 0 ; for ( int j = ele . size ( ) - 1 ; j >= ( ele . size ( ) - 1 - i ) && j >= 0 ; j -- ) { p . add ( ele . get ( j ) ) ; } int j = ele . size ( ) - 1 , k = 0 ; while ( j >= 0 ) { if ( ele . get ( j ) != p . get ( k ) ) { break ; } j -- ; k ++ ; if ( k == p . size ( ) ) { c ++ ; k = 0 ; } } count = Math . max ( count , c ) ; } return count ; } static void solve ( int n ) { int count = 1 ; Vector < Integer > ele = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( count + " , ▁ " ) ; ele . add ( count ) ; count = find_count ( ele ) ; } } public static void main ( String [ ] args ) { int n = 10 ; solve ( n ) ; } }
Wedderburn – Etherington number | Java program to find N terms of the sequence ; Stores the Wedderburn Etherington numbers ; Function to return the nth Wedderburn Etherington numbers ; Base case ; If n is even n = 2 x ; get x ; a ( 2 x ) = a ( 1 ) a ( 2 x - 1 ) + a ( 2 ) a ( 2 x - 2 ) + ... + a ( x - 1 ) a ( x + 1 ) ; a ( x ) ( a ( x ) + 1 ) / 2 ; Store the ans ; Return the required answer ; If n is odd ; a ( 2 x - 1 ) = a ( 1 ) a ( 2 x - 2 ) + a ( 2 ) a ( 2 x - 3 ) + ... + a ( x - 1 ) a ( x ) , ; Store the ans ; Return the required answer ; Function to print first N Wedderburn Etherington numbers ; Store first 3 numbers ; Print N terms ; Driver code ; function call
import java . util . * ; class GFG { static HashMap < Integer , Integer > store = new HashMap < Integer , Integer > ( ) ; static int Wedderburn ( int n ) { if ( n <= 2 ) return store . get ( n ) ; else if ( n % 2 == 0 ) { int x = n / 2 , ans = 0 ; for ( int i = 1 ; i < x ; i ++ ) { ans += store . get ( i ) * store . get ( n - i ) ; } ans += ( store . get ( x ) * ( store . get ( x ) + 1 ) ) / 2 ; store . put ( n , ans ) ; return ans ; } else { int x = ( n + 1 ) / 2 , ans = 0 ; for ( int i = 1 ; i < x ; i ++ ) { ans += store . get ( i ) * store . get ( n - i ) ; } store . put ( n , ans ) ; return ans ; } } static void Wedderburn_Etherington ( int n ) { store . put ( 0 , 0 ) ; store . put ( 1 , 1 ) ; store . put ( 2 , 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( Wedderburn ( i ) ) ; if ( i != n - 1 ) System . out . print ( " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 10 ; Wedderburn_Etherington ( n ) ; } }
Maximum value after merging all elements in the array | Java program to maximum value after merging all elements in the array ; Function to maximum value after merging all elements in the array ; To check if positive and negative elements present or not ; Check for positive integer ; Check for negative integer ; If both positive and negative elements are present ; To store maximum value possible ; To find minimum value ; Remove minimum element ; Replace with absolute values ; To find minimum value ; Remove minimum element ; Return the required sum ; Driver code ; Function call
import java . io . * ; class GFG { static int Max_sum ( int a [ ] , int n ) { int pos = 0 , neg = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > 0 ) pos = 1 ; else if ( a [ i ] < 0 ) neg = 1 ; if ( ( pos == 1 ) && ( neg == 1 ) ) break ; } int sum = 0 ; if ( ( pos == 1 ) && ( neg == 1 ) ) { for ( int i = 0 ; i < n ; i ++ ) sum += Math . abs ( a [ i ] ) ; } else if ( pos == 1 ) { int mini = a [ 0 ] ; sum = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { mini = Math . min ( mini , a [ i ] ) ; sum += a [ i ] ; } sum -= 2 * mini ; } else if ( neg == 1 ) { for ( int i = 0 ; i < n ; i ++ ) a [ i ] = Math . abs ( a [ i ] ) ; int mini = a [ 0 ] ; sum = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { mini = Math . min ( mini , a [ i ] ) ; sum += a [ i ] ; } sum -= 2 * mini ; } return sum ; } public static void main ( String [ ] args ) { int [ ] a = { 1 , 3 , 5 , - 2 , - 6 } ; int n = a . length ; System . out . println ( Max_sum ( a , n ) ) ; } }
Decimal to Binary using recursion and without using power operator | Java implementation of the approach ; Recursive function to convert n to its binary equivalent ; Base case ; Recursive call ; Driver code
import java . io . * ; class GFG { static void decimalToBinary ( int n ) { if ( n == 0 ) { System . out . print ( "0" ) ; return ; } decimalToBinary ( n / 2 ) ; System . out . print ( n % 2 ) ; } public static void main ( String [ ] args ) { int n = 13 ; decimalToBinary ( n ) ; } }
Find minimum possible values of A , B and C when two of the ( A + B ) , ( A + C ) and ( B + C ) are given | Java implementation of the approach ; Function to find A , B and C ; Keep minimum number in x ; Find the numbers ; Driver code ; Function call
import java . io . * ; class GFG { static void MinimumValue ( int x , int y ) { if ( x > y ) { int temp = x ; x = y ; y = temp ; } int a = 1 ; int b = x - 1 ; int c = y - b ; System . out . print ( a + " ▁ " + b + " ▁ " + c ) ; } public static void main ( String [ ] args ) { int x = 123 , y = 13 ; MinimumValue ( x , y ) ; } }
Check whether it is possible to convert A into B | Java implementation of the approach ; Function that returns true if A can be converted to B with the given operations ; If the current number ends with 1 ; If the current number is divisible by 2 ; If above two conditions fail ; If it is possible to convert A to B ; Driver code
class GFG { static boolean canConvert ( int a , int b ) { while ( b > a ) { if ( b % 10 == 1 ) { b /= 10 ; continue ; } if ( b % 2 == 0 ) { b /= 2 ; continue ; } return false ; } if ( b == a ) return true ; return false ; } public static void main ( String [ ] args ) { int A = 2 , B = 82 ; if ( canConvert ( A , B ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Count Distinct Rectangles in N * N Chessboard | Java program to count unique rectangles in a chessboard ; Function to count distinct rectangles ; Driver Code
class Rectangle { static int count ( int N ) { int a = 0 ; a = ( N * ( N + 1 ) ) / 2 ; return a ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . print ( count ( n ) ) ; } }
Total number of days taken to complete the task if after certain days one person leaves | Java implementation of the approach ; Function to return the number of days required ; Driver code
import java . io . * ; class GFG { static int numberOfDays ( int a , int b , int n ) { int Days = b * ( n + a ) / ( a + b ) ; return Days ; } public static void main ( String [ ] args ) { int a = 10 , b = 20 , n = 5 ; System . out . println ( numberOfDays ( a , b , n ) ) ; } }
Find average of two numbers using bit operation | Java implementation of the approach ; Function to return the average of x and y using bit operations ; Calculate the average Floor value of ( x + y ) / 2 ; Driver code
class GFG { static int getAverage ( int x , int y ) { int avg = ( x & y ) + ( ( x ^ y ) >> 1 ) ; return avg ; } public static void main ( String [ ] args ) { int x = 10 , y = 9 ; System . out . print ( getAverage ( x , y ) ) ; } }
Smallest index such that there are no 0 or 1 to its right | Java program to implement the above approach ; Function to find the smallest index such that there are no 0 or 1 to its right ; Initially ; Traverse in the array ; Check if array element is 1 ; a [ i ] = 0 ; Return minimum of both ; Driver code
class GFG { static int smallestIndex ( int [ ] a , int n ) { int right1 = 0 , right0 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 1 ) right1 = i ; else right0 = i ; } return Math . min ( right1 , right0 ) ; } public static void main ( String [ ] args ) { int [ ] a = { 1 , 1 , 1 , 0 , 0 , 1 , 0 , 1 , 1 } ; int n = a . length ; System . out . println ( smallestIndex ( a , n ) ) ; } }
Total position where king can reach on a chessboard in exactly M moves | Set 2 | Java implementation of the approach ; Function to return the count of squares that can be visited by king in m moves ; To store the count of squares ; Check all squares of the chessboard ; Check if square ( i , j ) is at a distance <= m units from king 's current position ; Return count of squares ; Driver code
class GFG { static int countSquares ( int r , int c , int m ) { int squares = 0 ; for ( int i = 1 ; i <= 8 ; i ++ ) { for ( int j = 1 ; j <= 8 ; j ++ ) { if ( Math . max ( Math . abs ( i - r ) , Math . abs ( j - c ) ) <= m ) squares ++ ; } } return squares ; } public static void main ( String [ ] args ) { int r = 4 , c = 4 , m = 1 ; System . out . print ( countSquares ( r , c , m ) ) ; } }
Number of quadruples where the first three terms are in AP and last three terms are in GP | Java implementation of the approach ; Function to return the count of quadruples ; Hash table to count the number of occurrences ; Traverse and increment the count ; Run two nested loop for second and third element ; If they are same ; Initially decrease the count ; Find the first element using common difference ; Find the fourth element using GP y ^ 2 = x * z property ; If it is an integer ; If not equal ; Same elements ; Later increase the value for future calculations ; Driver code
import java . util . * ; class GFG { static int countQuadruples ( int a [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( mp . containsKey ( a [ i ] ) ) { mp . put ( a [ i ] , mp . get ( a [ i ] ) + 1 ) ; } else { mp . put ( a [ i ] , 1 ) ; } int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { for ( int k = 0 ; k < n ; k ++ ) { if ( j == k ) continue ; mp . put ( a [ j ] , mp . get ( a [ j ] ) - 1 ) ; mp . put ( a [ k ] , mp . get ( a [ k ] ) - 1 ) ; int first = a [ j ] - ( a [ k ] - a [ j ] ) ; int fourth = ( a [ k ] * a [ k ] ) / a [ j ] ; if ( ( a [ k ] * a [ k ] ) % a [ j ] == 0 ) { if ( a [ j ] != a [ k ] ) { if ( mp . containsKey ( first ) && mp . containsKey ( fourth ) ) count += mp . get ( first ) * mp . get ( fourth ) ; } else if ( mp . containsKey ( first ) && mp . containsKey ( fourth ) ) count += mp . get ( first ) * ( mp . get ( fourth ) - 1 ) ; } if ( mp . containsKey ( a [ j ] ) ) { mp . put ( a [ j ] , mp . get ( a [ j ] ) + 1 ) ; } else { mp . put ( a [ j ] , 1 ) ; } if ( mp . containsKey ( a [ k ] ) ) { mp . put ( a [ k ] , mp . get ( a [ k ] ) + 1 ) ; } else { mp . put ( a [ k ] , 1 ) ; } } } return count ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 6 , 4 , 9 , 2 } ; int n = a . length ; System . out . print ( countQuadruples ( a , n ) ) ; } }
Numbers in a Range with given Digital Root | Java implementation of the approach ; Function to return the count of required numbers ; Count of numbers present in given range ; Number of groups of 9 elements starting from L ; Left over elements not covered in factor 9 ; One Number in each group of 9 ; To check if any number in rem satisfy the property ; Driver code
class GFG { static int countNumbers ( int L , int R , int K ) { if ( K == 9 ) { K = 0 ; } int totalnumbers = R - L + 1 ; int factor9 = totalnumbers / 9 ; int rem = totalnumbers % 9 ; int ans = factor9 ; for ( int i = R ; i > R - rem ; i -- ) { int rem1 = i % 9 ; if ( rem1 == K ) { ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { int L = 10 ; int R = 22 ; int K = 3 ; System . out . println ( countNumbers ( L , R , K ) ) ; } }