codeparrot/xlcost-text-to-code huggingface dataset:
First N terms whose sum of digits is a multiple of 10 | Java implementation of the above approach ; Function to return the sum of digits of n ; Add last digit to the sum ; Remove last digit ; Function to return the nth term of the required series ; If sum of digit is already a multiple of 10 then append 0 ; To store the minimum digit that must be appended ; Return n after appending the required digit ; Function to print the first n terms of the required series ; Driver code
class GFG { final static int TEN = 10 ; static int digitSum ( int n ) { int sum = 0 ; while ( n > 0 ) { sum += n % TEN ; n /= TEN ; } return sum ; } static int getNthTerm ( int n ) { int sum = digitSum ( n ) ; if ( sum % TEN == 0 ) return ( n * TEN ) ; int extra = TEN - ( sum % TEN ) ; return ( ( n * TEN ) + extra ) ; } static void firstNTerms ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( getNthTerm ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 10 ; firstNTerms ( n ) ; } }
Nearest greater number by interchanging the digits | JAVA program to find nearest greater value ; Find all the possible permutation of Value A . ; Convert into Integer ; Find the minimum value of A by interchanging the digit of A and store min1 . ; Swap two string character ; Driver code ; Convert integer value into string to find all the permutation of the number ; count = 1 means number greater than B exists
import java . io . * ; import java . util . * ; class GFG { static int min1 = Integer . MAX_VALUE ; static int count = 0 ; public int permutation ( String str1 , int i , int n , int p ) { if ( i == n ) { int q = Integer . parseInt ( str1 ) ; if ( q - p > 0 && q < min1 ) { min1 = q ; count = 1 ; } } else { for ( int j = i ; j <= n ; j ++ ) { str1 = swap ( str1 , i , j ) ; permutation ( str1 , i + 1 , n , p ) ; str1 = swap ( str1 , i , j ) ; } } return min1 ; } public String swap ( String str , int i , int j ) { char ch [ ] = str . toCharArray ( ) ; char temp = ch [ i ] ; ch [ i ] = ch [ j ] ; ch [ j ] = temp ; return String . valueOf ( ch ) ; } public static void main ( String [ ] args ) { int A = 213 ; int B = 111 ; GFG gfg = new GFG ( ) ; String str1 = Integer . toString ( A ) ; int len = str1 . length ( ) ; int h = gfg . permutation ( str1 , 0 , len - 1 , B ) ; if ( count == 1 ) System . out . println ( h ) ; else System . out . println ( - 1 ) ; } }
Alcuin 's Sequence | Java program for Alcuin 's Sequence ; find the nth term of Alcuin 's sequence ; return the ans ; print first n terms of Alcuin number ; display the number ; Driver code
import java . util . * ; class GFG { static int Alcuin ( int n ) { double _n = n , ans ; ans = Math . round ( ( _n * _n ) / 12 ) - Math . floor ( _n / 4 ) * Math . floor ( ( _n + 2 ) / 4 ) ; return ( int ) ans ; } static void solve ( int n ) { int i = 0 ; for ( i = 1 ; i <= n ; i ++ ) { System . out . print ( Alcuin ( i ) + " , ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 15 ; solve ( n ) ; } }
Find the final sequence of the array after performing given operations | Java implementation of the approach ; Driver code
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int [ ] solve ( int [ ] arr , int n ) { int [ ] b = new int [ n ] ; int p = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { b [ p ] = arr [ i -- ] ; if ( i >= 0 ) b [ n - 1 - p ] = arr [ i ] ; p ++ ; } return b ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . length ; int [ ] b = solve ( arr , n ) ; System . out . println ( Arrays . toString ( b ) ) ; } }
Check if the product of every contiguous subsequence is different or not in a number | Java implementation of the approach ; Function that returns true if the product of every digit of a contiguous subsequence is distinct ; To store the given number as a string ; Append all the digits starting from the end ; Reverse the string to get the original number ; Store size of the string ; Set to store product of each contiguous subsequence ; Find product of every contiguous subsequence ; If current product already exists in the set ; Utility reverse funciton ; Driver code
import java . util . * ; class GFG { static boolean productsDistinct ( int N ) { String s = " " ; while ( N > 0 ) { s += ( char ) ( N % 10 + '0' ) ; N /= 10 ; } s = reverse ( s ) ; int sz = s . length ( ) ; HashSet < Integer > se = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < sz ; i ++ ) { int product = 1 ; for ( int j = i ; j < sz ; j ++ ) { product *= ( int ) ( s . charAt ( j ) - '0' ) ; if ( se . contains ( product ) ) return false ; else se . add ( product ) ; } } return true ; } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r ; r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { int N = 2345 ; if ( productsDistinct ( N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Number of ways to arrange 2 * N persons on the two sides of a table with X and Y persons on opposite sides | Java implementation for the above approach ; Function to returns factorial of n ; Function to find nCr ; Function to find the number of ways to arrange 2 * N persons ; Driver code ; Function call
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int factorial ( int n ) { if ( n <= 1 ) return 1 ; return n * factorial ( n - 1 ) ; } static int nCr ( int n , int r ) { return factorial ( n ) / ( factorial ( n - r ) * factorial ( r ) ) ; } static int NumberOfWays ( int n , int x , int y ) { return nCr ( 2 * n - x - y , n - x ) * factorial ( n ) * factorial ( n ) ; } public static void main ( String [ ] args ) throws java . lang . Exception { int n = 5 , x = 4 , y = 2 ; System . out . println ( NumberOfWays ( n , x , y ) ) ; } }
Find smallest positive number Y such that Bitwise AND of X and Y is Zero | Java program to find smallest number Y for a given value of X such that X AND Y is zero ; Method to find smallest number Y for a given value of X such that X AND Y is zero ; Convert the number into its binary form ; Case 1 : If all bits are ones , then return the next number ; Case 2 : find the first 0 - bit index and return the Y ; Driver code
import java . lang . * ; public class Main { static long findSmallestNonZeroY ( long A_num ) { String A_binary = Long . toBinaryString ( A_num ) ; long B = 1 ; int len = A_binary . length ( ) ; int no_ones = Long . bitCount ( A_num ) ; if ( len == no_ones ) { return A_num + 1 ; } for ( int i = 0 ; i < len ; i ++ ) { char ch = A_binary . charAt ( len - i - 1 ) ; if ( ch == '0' ) { B = ( long ) Math . pow ( 2.0 , ( double ) i ) ; break ; } } return B ; } public static void main ( String [ ] args ) { long X = findSmallestNonZeroY ( 10 ) ; System . out . println ( X ) ; } }
Maximum element in an array such that its previous and next element product is maximum | Java implementation of the approach ; Function to return the largest element such that its previous and next element product is maximum ; Calculate the product of the previous and the next element for the current element ; Update the maximum product ; If current product is equal to the current maximum product then choose the maximum element ; Driver code
class GFG { static int maxElement ( int a [ ] , int n ) { if ( n < 3 ) return - 1 ; int maxElement = a [ 0 ] ; int maxProd = a [ n - 1 ] * a [ 1 ] ; for ( int i = 1 ; i < n ; i ++ ) { int currProd = a [ i - 1 ] * a [ ( i + 1 ) % n ] ; if ( currProd > maxProd ) { maxProd = currProd ; maxElement = a [ i ] ; } else if ( currProd == maxProd ) { maxElement = Math . max ( maxElement , a [ i ] ) ; } } return maxElement ; } public static void main ( String [ ] args ) { int [ ] a = { 5 , 6 , 4 , 3 , 2 } ; int n = a . length ; System . out . println ( maxElement ( a , n ) ) ; } }
Total ways of choosing X men and Y women from a total of M men and W women | JAVA implementation of the approach ; Function to return the value of ncr effectively ; Initialize the answer ; Divide simultaneously by i to avoid overflow ; Function to return the count of required ways ; Driver code
import java . io . * ; class GFG { static int ncr ( int n , int r ) { int ans = 1 ; for ( int i = 1 ; i <= r ; i += 1 ) { ans *= ( n - r + i ) ; ans /= i ; } return ans ; } static int totalWays ( int X , int Y , int M , int W ) { return ( ncr ( M , X ) * ncr ( W , Y ) ) ; } public static void main ( String [ ] args ) { int X = 4 , Y = 3 , M = 6 , W = 5 ; System . out . println ( totalWays ( X , Y , M , W ) ) ; } }
Generate elements of the array following given conditions | Java implementation of the approach ; Function to generate the required array ; Initialize cnt variable for assigning unique value to prime and its multiples ; When we get a prime for the first time then assign a unique smallest value to it and all of its multiples ; Print the generated array ; Driver code
import java . io . * ; class GFG { static void specialSieve ( int n ) { int cnt = 0 ; int prime [ ] = new int [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) prime [ i ] = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( ! ( prime [ i ] > 0 ) ) { cnt ++ ; for ( int j = i ; j <= n ; j += i ) prime [ j ] = cnt ; } } for ( int i = 2 ; i <= n ; i ++ ) System . out . print ( prime [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 6 ; specialSieve ( n ) ; } }
Find the node whose sum with X has maximum set bits | Java implementation of the approach ; number of set bits ; Function to perform dfs to find the maximum value ; If current set bits value is greater than the current maximum ; If count is equal to the maximum then choose the node with minimum value ; Driver code ; Weights of the node ; Edges of the tree
import java . util . * ; class GFG { static int maximum = Integer . MIN_VALUE , x , ans = Integer . MAX_VALUE ; static Vector < Vector < Integer > > graph = new Vector < Vector < Integer > > ( ) ; static Vector < Integer > weight = new Vector < Integer > ( ) ; static int __builtin_popcount ( int x ) { int c = 0 ; for ( int i = 0 ; i < 60 ; i ++ ) if ( ( ( x >> i ) & 1 ) != 0 ) c ++ ; return c ; } static void dfs ( int node , int parent ) { int a = __builtin_popcount ( weight . get ( node ) + x ) ; if ( maximum < a ) { maximum = a ; ans = node ; } else if ( maximum == a ) ans = Math . min ( ans , node ) ; for ( int i = 0 ; i < graph . get ( node ) . size ( ) ; i ++ ) { if ( graph . get ( node ) . get ( i ) == parent ) continue ; dfs ( graph . get ( node ) . get ( i ) , node ) ; } } public static void main ( String args [ ] ) { x = 15 ; weight . add ( 0 ) ; weight . add ( 5 ) ; weight . add ( 10 ) ; ; weight . add ( 11 ) ; ; weight . add ( 8 ) ; weight . add ( 6 ) ; for ( int i = 0 ; i < 100 ; i ++ ) graph . add ( new Vector < Integer > ( ) ) ; graph . get ( 1 ) . add ( 2 ) ; graph . get ( 2 ) . add ( 3 ) ; graph . get ( 2 ) . add ( 4 ) ; graph . get ( 1 ) . add ( 5 ) ; dfs ( 1 , 1 ) ; System . out . println ( ans ) ; } }
Count Pairs from two arrays with even sum | Java implementation of the approach ; Function to return count of required pairs ; Count of odd and even numbers from both the arrays ; Find the count of odd and even elements in a [ ] ; Find the count of odd and even elements in b [ ] ; Count the number of pairs ; Return the number of pairs ; Driver code
import java . io . * ; class GFG { static int count_pairs ( int a [ ] , int b [ ] , int n , int m ) { int odd1 = 0 , even1 = 0 ; int odd2 = 0 , even2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) odd1 ++ ; else even1 ++ ; } for ( int i = 0 ; i < m ; i ++ ) { if ( b [ i ] % 2 == 1 ) odd2 ++ ; else even2 ++ ; } int pairs = Math . min ( odd1 , odd2 ) + Math . min ( even1 , even2 ) ; return pairs ; } public static void main ( String [ ] args ) { int a [ ] = { 9 , 14 , 6 , 2 , 11 } ; int b [ ] = { 8 , 4 , 7 , 20 } ; int n = a . length ; int m = b . length ; System . out . println ( count_pairs ( a , b , n , m ) ) ; } }
Find an index such that difference between product of elements before and after it is minimum |  ; Function to find index ; Array to store log values of elements ; Prefix Array to Maintain Sum of log values till index i ; Answer Index ; Find minimum absolute value ; Driver code
public class Main { public static void solve ( int Array [ ] , int N ) { double Arraynew [ ] = new double [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { Arraynew [ i ] = Math . log ( Array [ i ] ) ; } double prefixsum [ ] = new double [ N ] ; prefixsum [ 0 ] = Arraynew [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { prefixsum [ i ] = prefixsum [ i - 1 ] + Arraynew [ i ] ; } int answer = 0 ; double minabs = Math . abs ( prefixsum [ N - 1 ] - 2 * prefixsum [ 0 ] ) ; for ( int i = 1 ; i < N - 1 ; i ++ ) { double ans1 = Math . abs ( prefixsum [ N - 1 ] - 2 * prefixsum [ i ] ) ; if ( ans1 < minabs ) { minabs = ans1 ; answer = i ; } } System . out . println ( " Index ▁ is : ▁ " + answer ) ; } public static void main ( String [ ] args ) { int Array [ ] = { 1 , 4 , 12 , 2 , 6 } ; int N = 5 ; solve ( Array , N ) ; } }
Find the value of N when F ( N ) = f ( a ) + f ( b ) where a + b is the minimum possible and a * b = N | Java implementation of the approach ; Function to return the value of F ( N ) ; Base cases ; Count the number of times a number if divisible by 2 ; Return the summation ; Driver code
import java . io . * ; class GFG { static int getValueOfF ( int n ) { if ( n == 1 ) return 0 ; if ( n == 2 ) return 1 ; int cnt = 0 ; while ( n % 2 == 0 ) { cnt += 1 ; n /= 2 ; } return 2 * cnt ; } public static void main ( String [ ] args ) { int n = 20 ; System . out . println ( getValueOfF ( n ) ) ; } }
Find the sum of the number of divisors | Java code for above given approach ; To store the number of divisors ; Function to find the number of divisors of all numbers in the range 1 to n ; For every number 1 to n ; Increase divisors count for every number ; Function to find the sum of divisors ; To store sum ; Count the divisors ; Driver code ; Function call
class GFG { static int N = 100005 ; static int mod = 1000000007 ; static int cnt [ ] = new int [ N ] ; static void Divisors ( ) { for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j * i < N ; j ++ ) { cnt [ i * j ] ++ ; } } } static int Sumofdivisors ( int A , int B , int C ) { int sum = 0 ; Divisors ( ) ; for ( int i = 1 ; i <= A ; i ++ ) { for ( int j = 1 ; j <= B ; j ++ ) { for ( int k = 1 ; k <= C ; k ++ ) { int x = i * j * k ; sum += cnt [ x ] ; if ( sum >= mod ) { sum -= mod ; } } } } return sum ; } public static void main ( String [ ] args ) { int A = 5 , B = 6 , C = 7 ; System . out . println ( Sumofdivisors ( A , B , C ) ) ; } }
Find ( 1 ^ n + 2 ^ n + 3 ^ n + 4 ^ n ) mod 5 | Set 2 | Java implementation of the approach ; Function to return A mod B ; length of the string ; to store required answer ; Function to return ( 1 ^ n + 2 ^ n + 3 ^ n + 4 ^ n ) % 5 ; Calculate and return ans ; Driver code
class GFG { static int A_mod_B ( String N , int a ) { int len = N . length ( ) ; int ans = 0 ; for ( int i = 0 ; i < len ; i ++ ) ans = ( ans * 10 + ( int ) N . charAt ( i ) - '0' ) % a ; return ans % a ; } static int findMod ( String N ) { int mod = A_mod_B ( N , 4 ) ; int ans = ( 1 + ( int ) Math . pow ( 2 , mod ) + ( int ) Math . pow ( 3 , mod ) + ( int ) Math . pow ( 4 , mod ) ) ; return ( ans % 5 ) ; } public static void main ( String args [ ] ) { String N = "4" ; System . out . println ( findMod ( N ) ) ; } }
Elements greater than the previous and next element in an Array | Java program to print elements greater than the previous and next element in an Array ; Function to print elements greater than the previous and next element in an Array ; Traverse array from index 1 to n - 2 and check for the given condition ; Driver Code
class GfG { static void printElements ( int arr [ ] , int n ) { for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] && arr [ i ] > arr [ i + 1 ] ) System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 1 , 5 , 4 , 9 , 8 , 7 , 5 } ; int n = arr . length ; printElements ( arr , n ) ; } }
Sum of the series Kn + ( K ( n | Java program of above approach ; Recursive C program to compute modular power ; Base cases ; If B is even ; If B is odd ; Function to return sum ; Driver code
import java . lang . Math ; class GFG { static int exponent ( int A , int B ) { if ( A == 0 ) return 0 ; if ( B == 0 ) return 1 ; int y ; if ( B % 2 == 0 ) { y = exponent ( A , B / 2 ) ; y = ( y * y ) ; } else { y = A ; y = ( y * exponent ( A , B - 1 ) ) ; } return y ; } static int sum ( int k , int n ) { int sum = exponent ( k , n + 1 ) - exponent ( k - 1 , n + 1 ) ; return sum ; } public static void main ( String [ ] args ) { int n = 3 ; int K = 3 ; System . out . println ( sum ( K , n ) ) ; } }
Minimize the cost to split a number | Java implementation of the approach ; check if a number is prime or not ; run a loop upto square root of x ; Function to return the minimized cost ; If n is prime ; If n is odd and can be split into ( prime + 2 ) then cost will be 1 + 1 = 2 ; Every non - prime even number can be expressed as the sum of two primes ; n is odd so n can be split into ( 3 + even ) further even part can be split into ( prime + prime ) ( 3 + prime + prime ) will cost 3 ; Driver code
import java . util . * ; class GFG { static boolean isPrime ( int x ) { for ( int i = 2 ; i * i <= x ; i ++ ) { if ( x % i == 0 ) return false ; } return true ; } static int minimumCost ( int n ) { if ( isPrime ( n ) ) return 1 ; if ( n % 2 == 1 && isPrime ( n - 2 ) ) return 2 ; if ( n % 2 == 0 ) return 2 ; return 3 ; } public static void main ( String args [ ] ) { int n = 6 ; System . out . println ( minimumCost ( n ) ) ; } }
Find amount of water wasted after filling the tank | Java program to find the volume of water wasted ; Function to calculate amount of wasted water ; filled amount of water in one minute ; total time taken to fill the tank because of leakage ; wasted amount of water ; Driver code
class GFG { static double wastedWater ( double V , double M , double N ) { double wasted_amt , amt_per_min , time_to_fill ; amt_per_min = M - N ; time_to_fill = V / amt_per_min ; wasted_amt = N * time_to_fill ; return wasted_amt ; } public static void main ( String [ ] args ) { double V , M , N ; V = 700 ; M = 10 ; N = 3 ; System . out . println ( wastedWater ( V , M , N ) ) ; V = 1000 ; M = 100 ; N = 50 ; System . out . println ( wastedWater ( V , M , N ) ) ; } }
Smallest and Largest N | Java implementation of the approach ; Function to print the largest and the smallest n - digit perfect cube ; Smallest n - digit perfect cube ; Largest n - digit perfect cube ; Driver code
class GFG { static void nDigitPerfectCubes ( int n ) { int smallest = ( int ) Math . pow ( Math . ceil ( Math . cbrt ( Math . pow ( 10 , ( n - 1 ) ) ) ) , 3 ) ; System . out . print ( smallest + " ▁ " ) ; int largest = ( int ) Math . pow ( Math . ceil ( Math . cbrt ( Math . pow ( 10 , ( n ) ) ) ) - 1 , 3 ) ; System . out . print ( largest ) ; } public static void main ( String args [ ] ) { int n = 3 ; nDigitPerfectCubes ( n ) ; } }
Count numbers which are divisible by all the numbers from 2 to 10 | Java implementation of the approach ; Function to return the count of numbers from 1 to n which are divisible by all the numbers from 2 to 10 ; Driver code
class GFG { static int countNumbers ( int n ) { return ( n / 2520 ) ; } public static void main ( String args [ ] ) { int n = 3000 ; System . out . println ( countNumbers ( n ) ) ; } }
Sum of all odd factors of numbers in the range [ l , r ] | Java implementation of the approach ; Function to calculate the prefix sum of all the odd factors ; Add i to all the multiples of i ; Update the prefix sum ; Function to return the sum of all the odd factors of the numbers in the given range ; Driver code
import java . io . * ; class GFG { static int MAX = 100001 ; static int prefix [ ] = new int [ MAX ] ; static void sieve_modified ( ) { for ( int i = 1 ; i < MAX ; i += 2 ) { for ( int j = i ; j < MAX ; j += i ) prefix [ j ] += i ; } for ( int i = 1 ; i < MAX ; i ++ ) prefix [ i ] += prefix [ i - 1 ] ; } static int sumOddFactors ( int L , int R ) { return ( prefix [ R ] - prefix [ L - 1 ] ) ; } public static void main ( String [ ] args ) { sieve_modified ( ) ; int l = 6 , r = 10 ; System . out . println ( sumOddFactors ( l , r ) ) ; } }
Number of ways in which an item returns back to its initial position in N swaps in array of size K | Java program to find the number of ways in which an item returns back to its initial position in N swaps in an array of size K ; Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Function to return the number of ways ; Base case ; Recursive case F ( n ) = ( k - 1 ) ^ ( n - 1 ) - F ( n - 1 ) . ; Driver code ; Function calling
class GFG { static int mod = 1000000007 ; public static int power ( int x , int y ) { int p = mod ; int res = 1 ; x = x % p ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } public static int solve ( int n , int k ) { if ( n == 1 ) return 0 ; return ( power ( ( k - 1 ) , n - 1 ) % mod - solve ( n - 1 , k ) + mod ) % mod ; } public static void main ( String [ ] args ) { int n = 4 , k = 5 ; System . out . println ( solve ( n , k ) ) ; } }
XOR of a submatrix queries | Java implementation of the approach ; Function to pre - compute the xor ; Left to right prefix xor for each row ; Top to bottom prefix xor for each column ; Function to process the queries x1 , x2 , y1 , y2 represent the positions of the top - left and bottom right corners ; To store the xor values ; Finding the values we need to xor with value at ( x2 , y2 ) in prefix - xor matrix ; Return the required prefix xor ; Driver code ; To store pre - computed xor ; Pre - computing xor ; Queries
class GfG { static int n = 3 ; static void preComputeXor ( int arr [ ] [ ] , int prefix_xor [ ] [ ] ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) { if ( j == 0 ) prefix_xor [ i ] [ j ] = arr [ i ] [ j ] ; else prefix_xor [ i ] [ j ] = ( prefix_xor [ i ] [ j - 1 ] ^ arr [ i ] [ j ] ) ; } for ( int i = 0 ; i < n ; i ++ ) for ( int j = 1 ; j < n ; j ++ ) prefix_xor [ j ] [ i ] = ( prefix_xor [ j - 1 ] [ i ] ^ prefix_xor [ j ] [ i ] ) ; } static int ansQuerie ( int prefix_xor [ ] [ ] , int x1 , int y1 , int x2 , int y2 ) { int xor_1 = 0 , xor_2 = 0 , xor_3 = 0 ; if ( x1 != 0 ) xor_1 = prefix_xor [ x1 - 1 ] [ y2 ] ; if ( y1 != 0 ) xor_2 = prefix_xor [ x2 ] [ y1 - 1 ] ; if ( x1 != 0 && y1 != 0 ) xor_3 = prefix_xor [ x1 - 1 ] [ y1 - 1 ] ; return ( ( prefix_xor [ x2 ] [ y2 ] ^ xor_1 ) ^ ( xor_2 ^ xor_3 ) ) ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = new int [ ] [ ] { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int prefix_xor [ ] [ ] = new int [ n ] [ n ] ; preComputeXor ( arr , prefix_xor ) ; System . out . println ( ansQuerie ( prefix_xor , 1 , 1 , 2 , 2 ) ) ; System . out . println ( ansQuerie ( prefix_xor , 1 , 2 , 2 , 2 ) ) ; } }
Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1 | Java implementation of the approach ; Function that returns true if the eventual digit sum of number nm is 1 ; if reminder will 1 then eventual sum is 1 ; Function to print the required numbers less than n ; If it is the required perfect cube ; Driver code
class GFG { static boolean isDigitSumOne ( int nm ) { if ( nm % 9 == 1 ) return true ; else return false ; } static void printValidNums ( int n ) { int cbrt_n = ( int ) Math . cbrt ( n ) ; for ( int i = 1 ; i <= cbrt_n ; i ++ ) { int cube = ( int ) Math . pow ( i , 3 ) ; if ( cube >= 1 && cube <= n && isDigitSumOne ( cube ) ) System . out . print ( cube + " ▁ " ) ; } } public static void main ( String args [ ] ) { int n = 1000 ; printValidNums ( n ) ; } }