codeparrot/xlcost-text-to-code huggingface dataset:
Check if a number can be represented as sum of two positive perfect cubes | Java program for the above approach ; Function to check if N can be represented as sum of two perfect cubes or not ; Stores the perfect cubes of first N natural numbers ; Traverse the map ; Stores first number ; Stores second number ; Search the pair for the first number to obtain sum N from the Map ; If N cannot be represented as sum of two positive perfect cubes ; Driver code ; Function call to check if N can be represented as sum of two perfect cubes or not
import java . util . * ; class GFG { public static void sumOfTwoPerfectCubes ( int N ) { HashMap < Integer , Integer > cubes = new HashMap < > ( ) ; for ( int i = 1 ; i * i * i <= N ; i ++ ) cubes . put ( ( i * i * i ) , i ) ; Iterator < Map . Entry < Integer , Integer > > itr = cubes . entrySet ( ) . iterator ( ) ; while ( itr . hasNext ( ) ) { Map . Entry < Integer , Integer > entry = itr . next ( ) ; int firstNumber = entry . getKey ( ) ; int secondNumber = N - entry . getKey ( ) ; if ( cubes . containsKey ( secondNumber ) ) { System . out . println ( " True " ) ; return ; } } System . out . println ( " False " ) ; } public static void main ( String [ ] args ) { int N = 28 ; sumOfTwoPerfectCubes ( N ) ; } }
Count subsets consisting of each element as a factor of the next element in that subset | Java Program to implement the above approach ; Function to find number of subsets satisfying the given condition ; Stores number of required sets ; Stores maximum element of arr [ ] that defines the size of sieve ; Iterate through the arr [ ] ; If current element > maxE , then update maxE ; Declare an array sieve of size N + 1 ; Mark all elements corresponding in the array , by one as there will always exists a singleton set ; Iterate from range [ 1 , N ] ; If element is present in array ; Traverse through all its multiples <= n ; Update them if they are present in array ; Iterate from the range [ 1 , N ] ; Update the value of cnt ; Print the result ; Driver Code ; Function Call
import java . io . * ; import java . util . * ; class GFG { static int mod = 1000000007 ; static void countSets ( int arr [ ] , int n ) { int cnt = 0 ; int maxE = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( maxE < arr [ i ] ) maxE = arr [ i ] ; } int sieve [ ] = new int [ maxE + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) sieve [ arr [ i ] ] = 1 ; for ( int i = 1 ; i <= maxE ; i ++ ) { if ( sieve [ i ] != 0 ) { for ( int j = i * 2 ; j <= maxE ; j += i ) { if ( sieve [ j ] != 0 ) sieve [ j ] = ( sieve [ j ] + sieve [ i ] ) % mod ; } } } for ( int i = 0 ; i <= maxE ; i ++ ) cnt = ( cnt % mod + sieve [ i ] % mod ) % mod ; System . out . println ( cnt % mod ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 16 , 18 , 6 , 7 , 2 , 19 , 20 , 9 } ; int N = arr . length ; countSets ( arr , N ) ; } }
Count distinct prime factors for each element of an array | Java program for the above approach ; Stores smallest prime factor for every number ; Stores distinct prime factors ; Function to find the smallest prime factor of every number ; Mark the smallest prime factor of every number to itself ; Separately mark all the smallest prime factor of every even number to be 2 ; If i is prime ; Mark spf for all numbers divisible by i ; Mark spf [ j ] if it is not previously marked ; Function to find the distinct prime factors ; Push all distinct of x prime factor in v [ x ] ; Pushback into v [ i ] ; Increment the idx ; Update x = ( x / spf [ x ] ) ; Function to get the distinct factor count of arr [ ] ; initialization ; Precompute the smallest Prime Factors ; For distinct prime factors Fill the v [ ] vector ; Count of Distinct Prime Factors of each array element ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int MAX = 100001 ; static int spf [ ] ; static ArrayList < Integer > v [ ] ; static void sieve ( ) { for ( int i = 1 ; i < MAX ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < MAX ; i = i + 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i < MAX ; i ++ ) if ( spf [ i ] == i ) { for ( int j = i * i ; j < MAX ; j = j + i ) { if ( spf [ j ] == j ) spf [ j ] = i ; } } } static void DistPrime ( ) { for ( int i = 1 ; i < MAX ; i ++ ) { int idx = 1 ; int x = i ; if ( x != 1 ) v [ i ] . add ( spf [ x ] ) ; x = x / spf [ x ] ; while ( x != 1 ) { if ( v [ i ] . get ( idx - 1 ) != spf [ x ] ) { v [ i ] . add ( spf [ x ] ) ; idx += 1 ; } x = x / spf [ x ] ; } } } static void getFactorCount ( int arr [ ] , int N ) { spf = new int [ MAX ] ; v = new ArrayList [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) v [ i ] = new ArrayList < > ( ) ; sieve ( ) ; DistPrime ( ) ; for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( ( int ) v [ arr [ i ] ] . size ( ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 9 , 12 } ; int N = arr . length ; getFactorCount ( arr , N ) ; } }
Find the first day of a given year from a base year having first day as Monday | Java program to implement the above approach ; Function to find the day of 1 st January of Y year ; Count years between years Y and B ; Count leap years ; Non leap years ; Total number of days in the years lying between the years Y and B ; Actual day ; Driver code
import java . util . * ; class GFG { static void findDay ( int Y , int B ) { int lyear , rest , totaldays , day ; Y = ( Y - 1 ) - B ; lyear = Y / 4 ; rest = Y - lyear ; totaldays = ( rest * 365 ) + ( lyear * 366 ) + 1 ; day = ( totaldays % 7 ) ; if ( day == 0 ) System . out . println ( " Monday " ) ; else if ( day == 1 ) System . out . println ( " Tuesday " ) ; else if ( day == 2 ) System . out . println ( " Wednesday " ) ; else if ( day == 3 ) System . out . println ( " Thursday " ) ; else if ( day == 4 ) System . out . println ( " Friday " ) ; else if ( day == 5 ) System . out . println ( " Saturday " ) ; else if ( day == 6 ) System . out . println ( " Sunday " ) ; else System . out . println ( " INPUT ▁ YEAR ▁ IS ▁ WRONG ! " ) ; } public static void main ( String [ ] args ) { int Y = 2020 , B = 1900 ; findDay ( Y , B ) ; } }
Queries to update array elements in a range [ L , R ] to satisfy given conditions | Java program to implement the above approach ; Function to print the array ; Function to perform the query in range [ L , R ] such that arr [ i ] += i - L + 1 ; Initialize array ; Traverse the query array ; Stores range in 1 - based index ; Update arr1 [ L ] ; Update arr1 [ R + 1 ] ; Update arr2 [ R + 1 ] ; Calculate prefix sum ; Traverse the array , arr2 [ ] ; Copy arr2 [ ] into arr [ ] ; Driver Code ; Given array ; Size of the array ; Stores count of query ; Function Call
class GFG { static void printArray ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } static void modifyArray ( int arr [ ] , int N , int Q [ ] [ ] , int cntQuery ) { int arr1 [ ] = new int [ N + 2 ] ; int arr2 [ ] = new int [ N + 2 ] ; for ( int i = 0 ; i < cntQuery ; i ++ ) { int L = Q [ i ] [ 0 ] + 1 , R = Q [ i ] [ 1 ] + 1 ; arr1 [ L ] ++ ; arr1 [ R + 1 ] -- ; arr2 [ R + 1 ] -= R - L + 1 ; } for ( int i = 1 ; i <= N ; i ++ ) arr1 [ i ] += arr1 [ i - 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) arr2 [ i ] += arr2 [ i - 1 ] + arr1 [ i ] ; for ( int i = 1 ; i <= N ; i ++ ) arr [ i - 1 ] = arr2 [ i ] ; printArray ( arr , N ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 0 , 0 , 0 } ; int N = arr . length ; int Q [ ] [ ] = { { 1 , 3 } , { 0 , 1 } } ; int cntQuery = Q . length ; modifyArray ( arr , N , Q , cntQuery ) ; } }
Maximize product obtained by taking one element from each array of a given list | Java program for the above approach ; Function to return the product of 2 numbers ; If any of the two numbers is None ; Otherwise , return the product ; Function to calculate maximum product by taking only one element from each array present in the list ; Find the maximum and minimum present in the current array ; If last index is reached , then return the highest ( positive ) and lowest ( negative ) values ; Store the positive and negative products returned by calculating for the remaining arrays ; Store highest positive product ; Store product of highest with negative ; Store product of lowest with positive ; Store lowest negative product ; Return the maximum positive and minimum negative product ; Driver Code ; Count of given arrays ; Given list of N arrays ; Store the maximum positive and minimum negative product possible ; Print the maximum product
import java . io . * ; import java . util . * ; class GFG { static int findProduct ( int number_1 , int number_2 ) { if ( number_1 == Integer . MIN_VALUE number_2 == Integer . MIN_VALUE ) { return 0 ; } else return number_1 * number_2 ; } static ArrayList < Integer > calculateProduct ( ArrayList < ArrayList < Integer > > List , int index ) { int highest = Collections . max ( List . get ( index ) ) ; int lowest = Collections . min ( List . get ( index ) ) ; if ( index + 1 == List . size ( ) ) { if ( lowest < 0 && highest >= 0 ) { return ( new ArrayList < Integer > ( Arrays . asList ( highest , lowest ) ) ) ; } else if ( lowest <= 0 && highest <= 0 ) { return ( new ArrayList < Integer > ( Arrays . asList ( Integer . MIN_VALUE , lowest ) ) ) ; } else if ( lowest >= 0 && highest >= 0 ) { return ( new ArrayList < Integer > ( Arrays . asList ( highest , Integer . MIN_VALUE ) ) ) ; } } ArrayList < Integer > temp = calculateProduct ( List , index + 1 ) ; int positive = temp . get ( 0 ) ; int negative = temp . get ( 1 ) ; int highPos = findProduct ( highest , positive ) ; int highNeg = findProduct ( highest , negative ) ; int lowPos = findProduct ( lowest , positive ) ; int lowNeg = findProduct ( lowest , negative ) ; if ( lowest < 0 && highest >= 0 ) { return ( new ArrayList < Integer > ( Arrays . asList ( Math . max ( highPos , lowNeg ) , Math . min ( highNeg , lowPos ) ) ) ) ; } else if ( lowest <= 0 && highest <= 0 ) { return ( new ArrayList < Integer > ( Arrays . asList ( lowNeg , lowPos ) ) ) ; } else if ( lowest >= 0 && highest >= 0 ) { return ( new ArrayList < Integer > ( Arrays . asList ( Math . max ( lowPos , highPos ) , Math . min ( lowNeg , highNeg ) ) ) ) ; } return ( new ArrayList < Integer > ( Arrays . asList ( 0 , 0 ) ) ) ; } public static void main ( String [ ] args ) { int N = 2 ; ArrayList < ArrayList < Integer > > arr = new ArrayList < ArrayList < Integer > > ( ) ; arr . add ( new ArrayList < Integer > ( Arrays . asList ( - 3 , - 4 ) ) ) ; arr . add ( new ArrayList < Integer > ( Arrays . asList ( 1 , 2 , - 3 ) ) ) ; ArrayList < Integer > ans = calculateProduct ( arr , 0 ) ; System . out . println ( ans . get ( 0 ) ) ; } }
Reduce a number N by at most D to maximize count of trailing nines | Java program for the above approach ; Function to find a number with maximum count of trailing nine ; Stores count of digits in n ; Stores power of 10 ; If last i digits greater than or equal to d ; Update res ; Update p10 ; Driver Code ; Function Call
class GFG { static void maxNumTrailNine ( int n , int d ) { int res = n ; int cntDigits = ( int ) Math . log10 ( n ) + 1 ; int p10 = 10 ; for ( int i = 1 ; i <= cntDigits ; i ++ ) { if ( n % p10 >= d ) { break ; } else { res = n - n % p10 - 1 ; } p10 = p10 * 10 ; } System . out . println ( res ) ; } public static void main ( String [ ] args ) { int n = 1025 , d = 6 ; maxNumTrailNine ( n , d ) ; } }
Split array into minimum number of subsets such that elements of all pairs are present in different subsets at least once | Java program to implement the above approach ; Function to find minimum count of ways to split the array into two subset such that elements of each pair occurs in two different subset ; Stores minimum count of ways to split array into two subset such that elements of each pair occurs in two different subset ; If N is odd ; Driver code
import java . util . * ; class GFG { static int MinimumNoOfWays ( int arr [ ] , int n ) { int mini_no_of_ways ; if ( n % 2 == 0 ) { mini_no_of_ways = n / 2 ; } else { mini_no_of_ways = n / 2 + 1 ; } return mini_no_of_ways ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 4 , 2 , 1 , 5 } ; int N = arr . length ; System . out . print ( MinimumNoOfWays ( arr , N ) ) ; } }
Chocolate Distribution Problem | Set 2 | Java program for above approach ; Function to return minimum number of chocolates ; decreasing sequence ; add the chocolates received by that person ; end point of decreasing sequence ; val = 1 ; reset value at that index ; increasing sequence ; flat sequence ; add value of chocolates at position n - 1 ; helper function to get sum of decreasing sequence ; value obtained from decreasing sequence also the count of values in the sequence ; assigning max of values obtained from increasing and decreasing sequences ; sum of count - 1 values & peak value sum of natural numbers : ( n * ( n + 1 ) ) / 2 ; Driver code
import java . io . * ; class GFG { public static int minChocolates ( int [ ] a , int n ) { int i = 0 , j = 0 ; int res = 0 , val = 1 ; while ( j < n - 1 ) { if ( a [ j ] > a [ j + 1 ] ) { j += 1 ; continue ; } if ( i == j ) res += val ; else { res += get_sum ( val , i , j ) ; } if ( a [ j ] < a [ j + 1 ] ) val += 1 ; else val = 1 ; j += 1 ; i = j ; } if ( i == j ) res += val ; else res += get_sum ( val , i , j ) ; return res ; } public static int get_sum ( int peak , int start , int end ) { int count = end - start + 1 ; peak = ( peak > count ) ? peak : count ; int s = peak + ( ( ( count - 1 ) * count ) >> 1 ) ; return s ; } } public static void main ( String [ ] args ) { int [ ] a = { 5 , 5 , 4 , 3 , 2 , 1 } ; int n = a . length ; System . out . print ( " Minimum ▁ number ▁ of ▁ chocolates ▁ = ▁ " + minChocolates ( a , n ) ) ; }
Remove array elements to reduce frequency of each array element to at most K | Java program for the above approach ; Function to remove array elements such that frequency of each distinct array element is at most K ; Base Case ; Stores index of array element by removing the array element such that the frequency of array elements is at most K ; Traverse the array ; If j < k or arr [ i ] > arr [ j - k ] ; Update arr [ j ] ; Remove array elements ; Function to print the array ; Traverse the array ; Utility Function to remove array elements such that frequency of each distinct array element is at most K ; Print updated array ; Driver Code
import java . util . * ; class GFG { static int [ ] RemoveElemArr ( int [ ] arr , int n , int k ) { if ( n == 0 n == 1 ) return arr ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( j < k arr [ i ] > arr [ j - k ] ) { arr [ j ++ ] = arr [ i ] ; } } while ( arr . length > j ) { arr = Arrays . copyOfRange ( arr , 0 , arr . length - 1 ) ; } return arr ; } static void printArray ( int [ ] arr ) { for ( int i = 0 ; i < arr . length ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } static void UtilRemov ( int [ ] arr , int n , int k ) { arr = RemoveElemArr ( arr , n , k ) ; printArray ( arr ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 2 , 3 , 4 , 4 , 4 , 5 , 5 } ; int k = 2 ; int n = arr . length ; UtilRemov ( arr , n , k ) ; } }
Count pairs of equal array elements remaining after every removal | Java program to implement the above approach ; Function to count pairs of equal elements by removing arr [ i ] from the array ; Stores total count of pairs of equal elements ; Store frequency of each distinct array element ; Traverse the array ; Update frequency of arr [ i ] ; Traverse the map ; Stores key of an element ; Traverse the array ; Stores count of pairs of equal element by removing arr [ i ] ; Driver code ; Given Array
import java . util . * ; class GFG { static void pairs_after_removing ( int arr [ ] , int N ) { int cntPairs = 0 ; Map < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { mp . put ( arr [ i ] , mp . getOrDefault ( arr [ i ] , 0 ) + 1 ) ; } for ( Map . Entry < Integer , Integer > element : mp . entrySet ( ) ) { int i = element . getKey ( ) ; cntPairs += mp . get ( i ) * ( mp . get ( i ) - 1 ) / 2 ; } for ( int i = 0 ; i < N ; i ++ ) { int pairs_after_arr_i_removed = cntPairs + 1 - mp . get ( arr [ i ] ) ; System . out . print ( pairs_after_arr_i_removed + " ▁ " ) ; } return ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 3 , 2 } ; int N = arr . length ; pairs_after_removing ( arr , N ) ; } }
Modify N by adding its smallest positive divisor exactly K times | Java program to implement the above approach ; Function to find the smallest divisor of N greater than 1 ; If i is a divisor of N ; If N is a prime number ; Function to find the value of N by performing the operations K times ; If N is an even number ; Update N ; If N is an odd number ; Update N ; Driver Code
class GFG { static int smallestDivisorGr1 ( int N ) { for ( int i = 2 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { return i ; } } return N ; } static int findValOfNWithOperat ( int N , int K ) { if ( N % 2 == 0 ) { N += K * 2 ; } else { N += smallestDivisorGr1 ( N ) + ( K - 1 ) * 2 ; } return N ; } public static void main ( String [ ] args ) { int N = 6 , K = 4 ; System . out . print ( findValOfNWithOperat ( N , K ) ) ; } }
Partition array into minimum number of equal length subsets consisting of a single distinct value | Java program to implement the above approach ; Function to find the minimum count of subsets by partitioning the array with given conditions ; Store frequency of each distinct element of the array ; Traverse the array ; Update frequency of arr [ i ] ; Stores GCD of frequency of each distinct element of the array ; Update freqGCD ; Recursive function to return gcd of a and b ; Driver Code
import java . util . * ; class GFG { static int CntOfSubsetsByPartitioning ( int arr [ ] , int N ) { HashMap < Integer , Integer > freq = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( freq . containsKey ( arr [ i ] ) ) { freq . put ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) ; } else { freq . put ( arr [ i ] , 1 ) ; } } int freqGCD = 0 ; for ( Map . Entry < Integer , Integer > i : freq . entrySet ( ) ) { freqGCD = __gcd ( freqGCD , i . getValue ( ) ) ; } return ( N ) / freqGCD ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 4 , 3 , 2 , 1 } ; int N = arr . length ; System . out . print ( CntOfSubsetsByPartitioning ( arr , N ) ) ; } }
Construct a Matrix such that each cell consists of sum of adjacent elements of respective cells in given Matrix | Java program for the above approach ; Initialize rows and columns ; Store all 8 directions ; Function to check if a cell ( i , j ) is valid or not ; Function to find sum of adjacent cells for cell ( i , j ) ; Initialize sum ; Visit all 8 directions ; Check if cell is valid ; Return sum ; Function to print sum of adjacent elements ; Stores the resultant matrix ; Iterate each elements of matrix ; Find adjacent sum ; Driver code ; Given matrix ; Size of matrix ; Function call
import java . util . * ; import java . lang . * ; public class GFG { private static int r , c ; static int [ ] [ ] dir = { { 1 , 0 } , { - 1 , 0 } , { 0 , 1 } , { 0 , - 1 } , { - 1 , - 1 } , { - 1 , 1 } , { 1 , 1 } , { 1 , - 1 } } ; public static boolean valid ( int i , int j ) { if ( i >= 0 && j >= 0 && i < r && j < c ) return true ; return false ; } static int find ( int i , int j , int [ ] [ ] v ) { int s = 0 ; for ( int k = 0 ; k < 8 ; k ++ ) { int ni = i + dir [ k ] [ 0 ] , nj = j + dir [ k ] [ 1 ] ; if ( valid ( ni , nj ) ) s += v [ ni ] [ nj ] ; } return s ; } static void findsumofneighbors ( int [ ] [ ] M ) { int [ ] [ ] v = new int [ r ] ; for ( int i = 0 ; i < r ; i ++ ) { for ( int j = 0 ; j < c ; j ++ ) { v [ i ] [ j ] = find ( i , j , M ) ; System . out . print ( v [ i ] [ j ] + " ▁ " ) ; } System . out . println ( " " ) ; } } public static void main ( String [ ] args ) { int [ ] [ ] M = { { 1 , 4 , 1 } , { 2 , 4 , 5 } , { 3 , 1 , 2 } } ; r = M . length ; c = M [ 0 ] . length ; findsumofneighbors ( M ) ; } }
Nth term of a recurrence relation generated by two given arrays | Java program for the above approach ; Declare T [ ] [ ] as global matrix ; Result matrix ; Function to multiply two matrices ; Create an auxiliary matrix to store elements of the multiplication matrix ; Iterate over range [ 0 , K ] ; Update temp [ i ] [ j ] ; Update the final matrix ; Function to multiply two matrices ; Create an auxiliary matrix to store elements of the multiplication matrix ; Iterate over range [ 0 , K ] ; Update temp [ i ] [ j ] ; Update the final matrix ; Function to calculate matrix ^ n using binary exponentaion ; Initialize result matrix and unity matrix ; Function to calculate nth term of general recurrence ; Fill T [ ] [ ] with appropriate value ; Function Call to calculate T ^ n ; Calculate nth term as first element of F * ( T ^ n ) ; Print the result ; Driver Code ; Given Initial terms ; Given coefficients ; Given K ; Given N ; Function Call
import java . util . * ; class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static int [ ] [ ] T = new int [ 2000 ] [ 2000 ] ; static int [ ] [ ] result = new int [ 2000 ] [ 2000 ] ; static void mul_2 ( int K ) { int [ ] [ ] temp = new int [ K + 1 ] [ K + 1 ] ; for ( int i = 1 ; i <= K ; i ++ ) { for ( int j = 1 ; j <= K ; j ++ ) { for ( int k = 1 ; k <= K ; k ++ ) { temp [ i ] [ j ] = ( temp [ i ] [ j ] + ( T [ i ] [ k ] * T [ k ] [ j ] ) % mod ) % mod ; } } } for ( int i = 1 ; i <= K ; i ++ ) { for ( int j = 1 ; j <= K ; j ++ ) { T [ i ] [ j ] = temp [ i ] [ j ] ; } } } static void mul_1 ( int K ) { int [ ] [ ] temp = new int [ K + 1 ] [ K + 1 ] ; for ( int i = 1 ; i <= K ; i ++ ) { for ( int j = 1 ; j <= K ; j ++ ) { for ( int k = 1 ; k <= K ; k ++ ) { temp [ i ] [ j ] = ( temp [ i ] [ j ] + ( result [ i ] [ k ] * T [ k ] [ j ] ) % mod ) % mod ; } } } for ( int i = 1 ; i <= K ; i ++ ) { for ( int j = 1 ; j <= K ; j ++ ) { result [ i ] [ j ] = temp [ i ] [ j ] ; } } } static void matrix_pow ( int K , int n ) { for ( int i = 1 ; i <= K ; i ++ ) { for ( int j = 1 ; j <= K ; j ++ ) { if ( i == j ) result [ i ] [ j ] = 1 ; } } while ( n > 0 ) { if ( n % 2 == 1 ) mul_1 ( K ) ; mul_2 ( K ) ; n /= 2 ; } } static int NthTerm ( int F [ ] , int C [ ] , int K , int n ) { for ( int i = 1 ; i <= K ; i ++ ) T [ i ] [ K ] = C [ K - i ] ; for ( int i = 1 ; i <= K ; i ++ ) T [ i + 1 ] [ i ] = 1 ; matrix_pow ( K , n ) ; int answer = 0 ; for ( int i = 1 ; i <= K ; i ++ ) { answer += F [ i - 1 ] * result [ i ] [ 1 ] ; } System . out . print ( answer + "NEW_LINE"); return 0 ; } public static void main ( String [ ] args ) { int F [ ] = { 1 , 2 , 3 } ; int C [ ] = { 1 , 1 , 1 } ; int K = 3 ; int N = 10 ; NthTerm ( F , C , K , N ) ; } }
Calculate Root Mean Kth power of all array elements | Java program for the above approach ; Function to find the Nth root ; Initially guessing random numberbetween 0 and 9 ; Smaller eps for more accuracy ; Initialize difference between the two roots by Integer . MAX_VALUE ; xK denotes current value of x ; Iterate until desired accuracy is reached ; Find the current value from previous value by newton 's method ; Function to calculate the Root Mean kth power of array elements ; Calculate sum of kth power ; Calculate Mean ; Calculate kth Root of mean ; Driver Code ; Function Call
class GFG { static double nthRoot ( int A , int N ) { double xPre = ( Math . random ( ) * 10 ) % 10 ; double eps = 1e-3 ; double delX = Integer . MAX_VALUE ; double xK = 0 ; while ( delX > eps ) { xK = ( ( N - 1.0 ) * xPre + ( double ) A / Math . pow ( xPre , N - 1 ) ) / ( double ) N ; delX = Math . abs ( xK - xPre ) ; xPre = xK ; } return xK ; } static float RMNValue ( int arr [ ] , int n , int k ) { int Nth = 0 ; float mean = 0 , root = 0 ; for ( int i = 0 ; i < n ; i ++ ) { Nth += Math . pow ( arr [ i ] , k ) ; } mean = ( Nth / ( float ) ( n ) ) ; root = ( float ) nthRoot ( ( int ) mean , k ) ; return root ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 4 , 6 , 8 } ; int N = arr . length ; int K = 3 ; System . out . print ( RMNValue ( arr , N , K ) ) ; } }