codeparrot/xlcost-text-to-code huggingface dataset:
Series summation if T ( n ) is given and n is very large | Java implementation of the approach ; Function to return the sum of the given series ; Driver code
class GFG { public static final int MOD = 1000000007 ; static int sumOfSeries ( int n ) { int ans = ( int ) Math . pow ( n % MOD , 2 ) ; return ( ans % MOD ) ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( sumOfSeries ( n ) ) ; } }
Kth odd number in an array | Java implementation of the approach ; Function to return the kth odd element from the array ; Traverse the array ; If current element is odd ; If kth odd element is found ; Total odd elements in the array are < k ; Driver code
public class GFG { static int kthOdd ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) k -- ; if ( k == 0 ) return arr [ i ] ; } return - 1 ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; int k = 2 ; System . out . print ( kthOdd ( arr , n , k ) ) ; } }
Find last five digits of a given five digit number raised to power five | Java program to find last five digits of a five digit number raised to power five ; Function to find the last five digits of a five digit number raised to power five ; Driver code
class GfG { static void lastFiveDigits ( int n ) { n = ( n / 10000 ) * 10000 + ( ( n / 100 ) % 10 ) * 1000 + ( n % 10 ) * 100 + ( ( n / 10 ) % 10 ) * 10 + ( n / 1000 ) % 10 ; int ans = 1 ; for ( int i = 0 ; i < 5 ; i ++ ) { ans *= n ; ans %= 100000 ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int n = 12345 ; lastFiveDigits ( n ) ; } }
Sum of ( maximum element | Java implementation of the above approach ; Function to return a ^ n % mod ; Compute sum of max ( A ) - min ( A ) for all subsets ; Sort the array . ; Maxs = 2 ^ i - 1 ; Mins = 2 ^ ( n - 1 - i ) - 1 ; Driver code
import java . util . * ; class GFG { static int mod = 1000000007 ; static long power ( long a , long n ) { if ( n == 0 ) { return 1 ; } long p = power ( a , n / 2 ) % mod ; p = ( p * p ) % mod ; if ( n == 1 ) { p = ( p * a ) % mod ; } return p ; } static long computeSum ( int [ ] arr , int n ) { Arrays . sort ( arr ) ; long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { long maxs = ( power ( 2 , i ) - 1 + mod ) % mod ; maxs = ( maxs * arr [ i ] ) % mod ; long mins = ( power ( 2 , n - 1 - i ) - 1 + mod ) % mod ; mins = ( mins * arr [ i ] ) % mod ; long V = ( maxs - mins + mod ) % mod ; sum = ( sum + V ) % mod ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 3 , 1 } ; int n = arr . length ; System . out . println ( computeSum ( arr , n ) ) ; } }
Count of all N digit numbers such that num + Rev ( num ) = 10 ^ N | Java implementation of the approach ; Function to return the count of such numbers ; If n is odd ; Driver code
class GFG { static int countNumbers ( int n ) { if ( n % 2 == 1 ) return 0 ; return ( 9 * ( int ) Math . pow ( 10 , n / 2 - 1 ) ) ; } public static void main ( String args [ ] ) { int n = 2 ; System . out . print ( countNumbers ( n ) ) ; } }
Count of numbers having only 1 set bit in the range [ 0 , n ] | Java implementation of the approach ; Function to return the required count ; To store the count of numbers ; Every power of 2 contains only 1 set bit ; Driver code
class GFG { static int count ( int n ) { int cnt = 0 ; int p = 1 ; while ( p <= n ) { cnt ++ ; p *= 2 ; } return cnt ; } public static void main ( String args [ ] ) { int n = 7 ; System . out . print ( count ( n ) ) ; } }
Find the K | Java programme to find the K 'th minimum element from an array concatenated M times ; Function to find the K - th minimum element from an array concatenated M times ; Sort the elements in ascending order ; Return the K 'th Min element  present at ( (K-1) / M ) index ; Driver Code
import java . util . * ; class GFG1 { static int KthMinValAfterMconcatenate ( int [ ] A , int N , int M , int K ) { Arrays . sort ( A ) ; return ( A [ ( ( K - 1 ) / M ) ] ) ; } public static void main ( String [ ] args ) { int [ ] A = { 3 , 1 , 2 } ; int M = 3 , K = 4 ; int N = A . length ; System . out . println ( KthMinValAfterMconcatenate ( A , N , M , K ) ) ; } }
Sum of all i such that ( 2 ^ i + 1 ) % 3 = 0 where i is in range [ 1 , n ] | Java implementation of the approach ; Function to return the required sum ; Total odd numbers from 1 to n ; Sum of first n odd numbers ; Driver code
class GFG { static int sum ( int n ) { n = ( n + 1 ) / 2 ; return ( n * n ) ; } public static void main ( String args [ ] ) { int n = 3 ; System . out . println ( sum ( n ) ) ; } }
Numbers that are not divisible by any number in the range [ 2 , 10 ] | Java implementation of the approach ; Function to return the count of numbers from 1 to N which are not divisible by any number in the range [ 2 , 10 ] ; Driver code
class GFG { static int countNumbers ( int n ) { return n - n / 2 - n / 3 - n / 5 - n / 7 + n / 6 + n / 10 + n / 14 + n / 15 + n / 21 + n / 35 - n / 30 - n / 42 - n / 70 - n / 105 + n / 210 ; } public static void main ( String [ ] args ) { int n = 20 ; System . out . println ( countNumbers ( n ) ) ; } }
Maximum Primes whose sum is equal to given N | Java program for above approach ; Function to find max count of primes ; if n is even n / 2 is required answer if n is odd floor ( n / 2 ) = ( int ) ( n / 2 ) is required answer ; Driver Code
class GFG { static int maxPrimes ( int n ) { return n / 2 ; } public static void main ( String [ ] args ) { int n = 17 ; System . out . println ( maxPrimes ( n ) ) ; } }
Sum of the series ( 1 * 2 ) + ( 2 * 3 ) + ( 3 * 4 ) + ... ... upto n terms | Java implementation of the approach ; Function to return sum ; Driver code
class GFG { static int sum ( int n ) { return n * ( n + 1 ) * ( n + 2 ) / 3 ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . println ( sum ( n ) ) ; } }
Find the shortest distance between any pair of two different good nodes | Java program to find the shortest pairwise distance between any two different good nodes . ; Function to add edges ; Function to find the shortest distance between any pair of two different good nodes ; Keeps minimum element on top ; To keep required answer ; If it is not good vertex ; Keep all vertices not visited and distance as MAXI ; Distance from ith vertex to ith is zero ; Make queue empty ; Push the ith vertex ; Count the good vertices ; Take the top element ; Remove it ; If it is already visited ; Count good vertices ; If distance from vth vertex is greater than ans ; If two good vertices are found ; Go to all adjacent vertices ; If distance is less ; Return the required answer ; Driver code ; Number of vertices and edges ; Function call to add edges ; Number of good nodes ; To keep good vertices
import java . util . ArrayList ; import java . util . Comparator ; import java . util . PriorityQueue ; class GFG { static class Pair { int first , second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } public Pair ( ) { } } static final int N = 100005 ; static final int MAXI = 99999999 ; static void add_edge ( ArrayList < Pair > gr [ ] , int x , int y , int weight ) { gr [ x ] . add ( new Pair ( y , weight ) ) ; gr [ y ] . add ( new Pair ( x , weight ) ) ; } static int minDistance ( ArrayList < Pair > gr [ ] , int n , int dist [ ] , int vis [ ] , int a [ ] , int k ) { PriorityQueue < Pair > q = new PriorityQueue < > ( new Comparator < Pair > ( ) { public int compare ( Pair p1 , Pair p2 ) { if ( p1 . first == p2 . first ) { return p1 . second - p2 . second ; } return p1 . first - p2 . first ; } } ) ; int ans = MAXI ; for ( int i = 1 ; i <= n ; i ++ ) { if ( a [ i ] == 0 ) continue ; for ( int j = 1 ; j <= n ; j ++ ) { dist [ j ] = MAXI ; vis [ j ] = 0 ; } dist [ i ] = 0 ; while ( ! q . isEmpty ( ) ) q . poll ( ) ; q . add ( new Pair ( 0 , i ) ) ; int good = 0 ; while ( ! q . isEmpty ( ) ) { int v = q . peek ( ) . second ; q . poll ( ) ; if ( vis [ v ] != 0 ) continue ; vis [ v ] = 1 ; good += a [ v ] ; if ( dist [ v ] > ans ) break ; if ( good == 2 && a [ v ] != 0 ) { ans = Math . min ( ans , dist [ v ] ) ; break ; } for ( int j = 0 ; j < gr [ v ] . size ( ) ; j ++ ) { int to = gr [ v ] . get ( j ) . first ; int weight = gr [ v ] . get ( j ) . second ; if ( dist [ v ] + weight < dist [ to ] ) { dist [ to ] = dist [ v ] + weight ; q . add ( new Pair ( dist [ to ] , to ) ) ; } } } } return ans ; } public static void main ( String [ ] args ) { int n = 5 , m = 5 ; @ SuppressWarnings ( " unchecked " ) ArrayList < Pair > [ ] gr = new ArrayList [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { gr [ i ] = new ArrayList < Pair > ( ) ; } add_edge ( gr , 1 , 2 , 3 ) ; add_edge ( gr , 1 , 2 , 3 ) ; add_edge ( gr , 2 , 3 , 4 ) ; add_edge ( gr , 3 , 4 , 1 ) ; add_edge ( gr , 4 , 5 , 8 ) ; int k = 3 ; int [ ] a = new int [ N ] , vis = new int [ N ] , dist = new int [ N ] ; a [ 1 ] = a [ 3 ] = a [ 5 ] = 1 ; System . out . println ( minDistance ( gr , n , dist , vis , a , k ) ) ; } }
Smallest divisor D of N such that gcd ( D , M ) is greater than 1 | Java implementation of the above approach ; Function to find the minimum divisor ; Iterate for all factors of N ; Check for gcd > 1 ; Check for gcd > 1 ; If gcd is m itself ; Driver code
class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static int findMinimum ( int n , int m ) { int mini = m ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { int sec = n / i ; if ( __gcd ( m , i ) > 1 ) { return i ; } else if ( __gcd ( sec , m ) > 1 ) { mini = Math . min ( sec , mini ) ; } } } if ( mini == m ) return - 1 ; else return mini ; } public static void main ( String [ ] args ) { int n = 8 , m = 10 ; System . out . println ( findMinimum ( n , m ) ) ; } }
Find Nth term of the series 1 , 5 , 32 , 288 ... | Java code to generate ' Nth ' terms of this sequence ; Function to generate a fixed \ number ; Finding nth term ; Driver Method
import java . lang . Math ; class GFG { public static int nthTerm ( int N ) { int nth = 0 , i ; for ( i = N ; i > 0 ; i -- ) { nth += Math . pow ( i , i ) ; } return nth ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . println ( nthTerm ( N ) ) ; } }
Find kth smallest number in range [ 1 , n ] when all the odd numbers are deleted | Java implementation of the approach ; Function to return the kth smallest element from the range [ 1 , n ] after removing all the odd elements ; Driver code
class GFG { static int kthSmallest ( int n , int k ) { return ( 2 * k ) ; } public static void main ( String args [ ] ) { int n = 8 , k = 4 ; System . out . print ( kthSmallest ( n , k ) ) ; } }
Check if a number can be represented as sum of non zero powers of 2 | Java implementation of the approach ; Function that return true if n can be represented as the sum of powers of 2 without using 2 ^ 0 ; Driver code
class GFG { static boolean isSumOfPowersOfTwo ( int n ) { if ( n % 2 == 1 ) return false ; else return true ; } public static void main ( String args [ ] ) { int n = 10 ; if ( isSumOfPowersOfTwo ( n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Minimize sum of adjacent difference with removal of one element from array | Java implementation of the approach ; Function to find the element ; Value variable for storing the total value ; Declaring maximum value as zero ; If array contains on element ; Storing the maximum value in temp variable ; Adding the adjacent difference modulus values of removed element . Removing adjacent difference modulus value after removing element ; Returning total value - maximum value ; Drivers code
class GFG { static int findMinRemoval ( int arr [ ] , int n ) { int temp , value = 0 ; int maximum = 0 ; if ( n == 1 ) return 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i != 0 && i != n - 1 ) { value = value + Math . abs ( arr [ i ] - arr [ i + 1 ] ) ; temp = Math . abs ( arr [ i ] - arr [ i + 1 ] ) + Math . abs ( arr [ i ] - arr [ i - 1 ] ) - Math . abs ( arr [ i - 1 ] - arr [ i + 1 ] ) ; } else if ( i == 0 ) { value = value + Math . abs ( arr [ i ] - arr [ i + 1 ] ) ; temp = Math . abs ( arr [ i ] - arr [ i + 1 ] ) ; } else temp = Math . abs ( arr [ i ] - arr [ i - 1 ] ) ; maximum = Math . max ( maximum , temp ) ; } return ( value - maximum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 3 , 2 , 10 } ; int n = arr . length ; System . out . print ( findMinRemoval ( arr , n ) + "NEW_LINE"); } }
Time until distance gets equal to X between two objects moving in opposite direction | Java implementation of the approach ; Function to return the time for which the two policemen can communicate ; time = distance / speed ; Driver code
class GFG { static double getTime ( int u , int v , int x ) { double speed = u + v ; double time = x / speed ; return time ; } public static void main ( String [ ] args ) { int u = 3 , v = 3 , x = 3 ; System . out . println ( getTime ( u , v , x ) ) ; } }
Given number of matches played , find number of teams in tournament | Java implementation of the approach ; Function to return the number of teams ; To store both roots of the equation ; sqrt ( b ^ 2 - 4 ac ) ; First root ( - b + sqrt ( b ^ 2 - 4 ac ) ) / 2 a ; Second root ( - b - sqrt ( b ^ 2 - 4 ac ) ) / 2 a ; Return the positive root ; Driver code
import java . io . * ; class GFG { static int number_of_teams ( int M ) { int N1 , N2 , sqr ; sqr = ( int ) Math . sqrt ( 1 + ( 8 * M ) ) ; N1 = ( 1 + sqr ) / 2 ; N2 = ( 1 - sqr ) / 2 ; if ( N1 > 0 ) return N1 ; return N2 ; } public static void main ( String [ ] args ) { int M = 45 ; System . out . println ( number_of_teams ( M ) ) ; } }
Sum of numbers from 1 to N which are in Lucas Sequence | java program to find sum of numbers from 1 to N which are in Lucas Sequence ; Function to return the required sum ; Generate lucas number and keep on adding them ; Driver code
class GFG { static int LucasSum ( int N ) { int sum = 0 ; int a = 2 , b = 1 , c ; sum += a ; while ( b <= N ) { sum += b ; c = a + b ; a = b ; b = c ; } return sum ; } public static void main ( String [ ] args ) { int N = 20 ; System . out . println ( LucasSum ( N ) ) ; } }
Count of all even numbers in the range [ L , R ] whose sum of digits is divisible by 3 | Java implementation of the approach ; Function to return the count of required numbers ; Count of numbers in range which are divisible by 6 ; Driver code
class GFG { static int countNumbers ( int l , int r ) { return ( ( r / 6 ) - ( l - 1 ) / 6 ) ; } public static void main ( String [ ] args ) { int l = 1000 , r = 6000 ; System . out . println ( countNumbers ( l , r ) ) ; } }
Sum of minimum element of all sub | Java implementation of the above approach ; Function to find the sum of minimum of all subsequence ; Driver code
class GfG { static int findMinSum ( int arr [ ] , int n ) { int occ = n - 1 , sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] * ( int ) Math . pow ( 2 , occ ) ; occ -- ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 5 } ; int n = arr . length ; System . out . println ( findMinSum ( arr , n ) ) ; } }
Number of trailing zeroes in base B representation of N ! | Java program to find the number of trailing zeroes in base B representation of N ! ; To find the power of a prime p in factorial N ; calculating floor ( n / r ) and adding to the count ; increasing the power of p from 1 to 2 to 3 and so on ; returns all the prime factors of k ; vector to store all the prime factors along with their number of occurrence in factorization of B ; Returns largest power of B that divides N ! ; calculating minimum power of all the prime factors of B ; Driver code
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int findPowerOfP ( int N , int p ) { int count = 0 ; int r = p ; while ( r <= N ) { count += ( N / r ) ; r = r * p ; } return count ; } static Vector < pair > primeFactorsofB ( int B ) { Vector < pair > ans = new Vector < pair > ( ) ; for ( int i = 2 ; B != 1 ; i ++ ) { if ( B % i == 0 ) { int count = 0 ; while ( B % i == 0 ) { B = B / i ; count ++ ; } ans . add ( new pair ( i , count ) ) ; } } return ans ; } static int largestPowerOfB ( int N , int B ) { Vector < pair > vec = new Vector < pair > ( ) ; vec = primeFactorsofB ( B ) ; int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < vec . size ( ) ; i ++ ) ans = Math . min ( ans , findPowerOfP ( N , vec . get ( i ) . first ) / vec . get ( i ) . second ) ; return ans ; } public static void main ( String [ ] args ) { System . out . println ( largestPowerOfB ( 5 , 2 ) ) ; System . out . println ( largestPowerOfB ( 6 , 9 ) ) ; } }
Count numbers in range 1 to N which are divisible by X but not by Y | Java implementation of above approach ; Function to count total numbers divisible by x but not y in range 1 to N ; Check if Number is divisible by x but not Y if yes , Increment count ; Driver Code
class GFG { static int countNumbers ( int X , int Y , int N ) { int count = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( ( i % X == 0 ) && ( i % Y != 0 ) ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int X = 2 , Y = 3 , N = 10 ; System . out . println ( countNumbers ( X , Y , N ) ) ; } }
Position of a person diametrically opposite on a circle | Java implementation of the approach ; Function to return the required position ; Driver code
class Sol { static int getPosition ( int n , int m ) { if ( m > ( n / 2 ) ) return ( m - ( n / 2 ) ) ; return ( m + ( n / 2 ) ) ; } public static void main ( String args [ ] ) { int n = 8 , m = 5 ; System . out . println ( getPosition ( n , m ) ) ; } }
Minimum operations required to modify the array such that parity of adjacent elements is different | Java implementation of the approach ; Function to return the parity of a number ; Function to return the minimum number of operations required ; Operation needs to be performed ; Parity of previous element ; Parity of next element ; Update parity of current element to be other than the parities of the previous and the next number ; Driver Code
class GFG { static int parity ( int a ) { return a % 3 ; } static int solve ( int [ ] array , int size ) { int operations = 0 ; for ( int i = 0 ; i < size - 1 ; i ++ ) { if ( parity ( array [ i ] ) == parity ( array [ i + 1 ] ) ) { operations ++ ; if ( i + 2 < size ) { int pari1 = parity ( array [ i ] ) ; int pari2 = parity ( array [ i + 2 ] ) ; if ( pari1 == pari2 ) { if ( pari1 == 0 ) array [ i + 1 ] = 1 ; else if ( pari1 == 1 ) array [ i + 1 ] = 0 ; else array [ i + 1 ] = 1 ; } else { if ( ( pari1 == 0 && pari2 == 1 ) || ( pari1 == 1 && pari2 == 0 ) ) array [ i + 1 ] = 2 ; if ( ( pari1 == 1 && pari2 == 2 ) || ( pari1 == 2 && pari2 == 1 ) ) array [ i + 1 ] = 0 ; if ( ( pari1 == 2 && pari2 == 0 ) || ( pari1 == 0 && pari2 == 2 ) ) array [ i + 1 ] = 1 ; } } } } return operations ; } public static void main ( String arg [ ] ) { int [ ] array = { 2 , 1 , 3 , 0 } ; int size = array . length ; System . out . println ( solve ( array , size ) ) ; } }
Number of submatrices with OR value 1 | Java program to count number of submatrices with OR value 1 ; Function to find required prefix - count for each row from right to left ; Function to find the count of submatrices with OR value 1 ; Array to store prefix count of zeros from right to left for boolean array ; Variable to store the count of submatrices with OR value 0 ; Loop to evaluate each column of the prefix matrix uniquely . For each index of a column we will try to determine the number of sub - matrices starting from that index and has all 1 s ; First part of pair will be the value of inserted element . Second part will be the count of the number of elements pushed before with a greater value ; Variable to store the number of submatrices with all 0 s ; Return the final answer ; Driver Code
import java . util . * ; class GFG { static int n = 3 ; static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void findPrefixCount ( int p_arr [ ] [ ] , boolean arr [ ] [ ] ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = n - 1 ; j >= 0 ; j -- ) { if ( arr [ i ] [ j ] ) continue ; if ( j != n - 1 ) p_arr [ i ] [ j ] += p_arr [ i ] [ j + 1 ] ; p_arr [ i ] [ j ] += ( arr [ i ] [ j ] == false ? 1 : 0 ) ; } } static int matrixOrValueOne ( boolean arr [ ] [ ] ) { int [ ] [ ] p_arr = new int [ n ] [ n ] ; findPrefixCount ( p_arr , arr ) ; int count_zero_submatrices = 0 ; for ( int j = 0 ; j < n ; j ++ ) { int i = n - 1 ; Stack < pair > q = new Stack < pair > ( ) ; int to_sum = 0 ; while ( i >= 0 ) { int c = 0 ; while ( q . size ( ) != 0 && q . peek ( ) . first > p_arr [ i ] [ j ] ) { to_sum -= ( q . peek ( ) . second + 1 ) * ( q . peek ( ) . first - p_arr [ i ] [ j ] ) ; c += q . peek ( ) . second + 1 ; q . pop ( ) ; } to_sum += p_arr [ i ] [ j ] ; count_zero_submatrices += to_sum ; q . add ( new pair ( p_arr [ i ] [ j ] , c ) ) ; i -- ; } } return ( n * ( n + 1 ) * n * ( n + 1 ) ) / 4 - count_zero_submatrices ; } public static void main ( String [ ] args ) { boolean arr [ ] [ ] = { { false , false , false } , { false , true , false } , { false , false , false } } ; System . out . println ( matrixOrValueOne ( arr ) ) ; } }
XOR of XORs of all sub | Java program to find the XOR of XOR 's of all submatrices ; Function to find to required XOR value ; Nested loop to find the number of sub - matrix each index belongs to ; Number of ways to choose from top - left elements ; Number of ways to choose from bottom - right elements ; Driver Code
class GFG { static int submatrixXor ( int [ ] [ ] arr ) { int n = 3 ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { int top_left = ( i + 1 ) * ( j + 1 ) ; int bottom_right = ( n - i ) * ( n - j ) ; if ( ( top_left % 2 == 1 ) && ( bottom_right % 2 == 1 ) ) ans = ( ans ^ arr [ i ] [ j ] ) ; } } return ans ; } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 6 , 7 , 13 } , { 8 , 3 , 4 } , { 9 , 7 , 6 } } ; System . out . println ( submatrixXor ( arr ) ) ; } }
Find Nth positive number whose digital root is X | Java program to find the N - th number whose digital root is X ; Function to find the digital root of a number ; Function to find the Nth number with digital root as X ; Counter variable to keep the count of valid numbers ; Find digital root ; Check if is required answer or not ; Print the answer if you have found it and breakout of the loop ; Driver Code
class GFG { static int findDigitalRoot ( int num ) { int sum = Integer . MAX_VALUE , tempNum = num ; while ( sum >= 10 ) { sum = 0 ; while ( tempNum > 0 ) { sum += tempNum % 10 ; tempNum /= 10 ; } tempNum = sum ; } return sum ; } static void findAnswer ( int X , int N ) { int counter = 0 ; for ( int i = 1 ; counter < N ; ++ i ) { int digitalRoot = findDigitalRoot ( i ) ; if ( digitalRoot == X ) { ++ counter ; } if ( counter == N ) { System . out . print ( i ) ; break ; } } } public static void main ( String args [ ] ) { int X = 1 , N = 3 ; findAnswer ( X , N ) ; } }
Find Nth positive number whose digital root is X | Java program to find the N - th number with digital root as X ; Function to find the N - th number with digital root as X ; Driver Code
class GfG { static int findAnswer ( int X , int N ) { return ( N - 1 ) * 9 + X ; } public static void main ( String [ ] args ) { int X = 7 , N = 43 ; System . out . println ( findAnswer ( X , N ) ) ; } }
Sum of the natural numbers ( up to N ) whose modulo with K yield R | Java implementation of the approach ; Function to return the sum ; If current number gives R as the remainder on dividing by K ; Update the sum ; Return the sum ; Driver code
class GfG { static long count ( int N , int K , int R ) { long sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( i % K == R ) sum += i ; } return sum ; } public static void main ( String [ ] args ) { int N = 20 , K = 4 , R = 3 ; System . out . println ( count ( N , K , R ) ) ; } }