codeparrot/xlcost-text-to-code huggingface dataset:
Construct a Matrix with no element exceeding X and sum of two adjacent elements not exceeding Y | Java implementation of the above approach ; Function to print the required matrix ; For 1 * 1 matrix ; Greater number ; Smaller number ; Sets / Resets for alternate filling of the matrix ; Print the matrix ; If end of row is reached ; Driver Code
class GFG { static void FindMatrix ( int n , int m , int x , int y ) { int a , b , i , j ; if ( n * m == 1 ) { if ( x > y ) { System . out . print ( y + "NEW_LINE"); } else { System . out . print ( x + "NEW_LINE"); } return ; } a = Math . min ( x , y ) ; b = Math . min ( 2 * x , y ) - a ; boolean flag = true ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { if ( flag ) System . out . print ( a + " ▁ " ) ; else System . out . print ( b + " ▁ " ) ; flag = ! flag ; } if ( ( ( n % 2 != 0 && m % 2 == 0 ) || ( n % 2 == 0 && m % 2 == 0 ) ) ) flag = ! flag ; System . out . print ( "NEW_LINE"); } } public static void main ( String [ ] args ) { int N , M , X , Y ; N = 3 ; M = 3 ; X = 5 ; Y = 3 ; FindMatrix ( N , M , X , Y ) ; } }
Travelling Salesman Problem | Greedy Approach | Java program for the above approach ; Function to find the minimum cost path for all the paths ; Starting from the 0 th indexed city i . e . , the first city ; Traverse the adjacency matrix tsp [ ] [ ] ; Corner of the Matrix ; If this path is unvisited then and if the cost is less then update the cost ; Check all paths from the ith indexed city ; Update the ending city in array from city which was last visited ; Started from the node where we finished as well . ; Driver Code ; Input Matrix ; Function Call
import java . util . * ; public class TSPGreedy { static void findMinRoute ( int [ ] [ ] tsp ) { int sum = 0 ; int counter = 0 ; int j = 0 , i = 0 ; int min = Integer . MAX_VALUE ; List < Integer > visitedRouteList = new ArrayList < > ( ) ; visitedRouteList . add ( 0 ) ; int [ ] route = new int [ tsp . length ] ; while ( i < tsp . length && j < tsp [ i ] . length ) { if ( counter >= tsp [ i ] . length - 1 ) { break ; } if ( j != i && ! ( visitedRouteList . contains ( j ) ) ) { if ( tsp [ i ] [ j ] < min ) { min = tsp [ i ] [ j ] ; route [ counter ] = j + 1 ; } } j ++ ; if ( j == tsp [ i ] . length ) { sum += min ; min = Integer . MAX_VALUE ; visitedRouteList . add ( route [ counter ] - 1 ) ; j = 0 ; i = route [ counter ] - 1 ; counter ++ ; } } i = route [ counter - 1 ] - 1 ; for ( j = 0 ; j < tsp . length ; j ++ ) { if ( ( i != j ) && tsp [ i ] [ j ] < min ) { min = tsp [ i ] [ j ] ; route [ counter ] = j + 1 ; } } sum += min ; System . out . print ( " Minimum ▁ Cost ▁ is ▁ : ▁ " ) ; System . out . println ( sum ) ; } public static void main ( String [ ] args ) { int [ ] [ ] tsp = { { - 1 , 10 , 15 , 20 } , { 10 , - 1 , 35 , 25 } , { 15 , 35 , - 1 , 30 } , { 20 , 25 , 30 , - 1 } } ; findMinRoute ( tsp ) ; } }
Count distinct elements after adding each element of First Array with Second Array | Java program for the above approach ; Function to find Occurrence of each element from 1 to 2 * MAX ; Initialise MAX ; Count vector to store count of each element from 1 to 2 * MAX ; Size of Arr1 and Arr2 ; Find the elements of arr3 [ ] and increase count of element by 1 ; Print the result ; Driver Code ; Given arrays arr1 [ ] and arr2 [ ] ; Function call
import java . util . * ; class GFG { static void findCount ( int [ ] Arr1 , int [ ] Arr2 ) { int MAX = Math . max ( Arrays . stream ( Arr1 ) . max ( ) . getAsInt ( ) , Arrays . stream ( Arr2 ) . max ( ) . getAsInt ( ) ) ; int [ ] Count = new int [ 2 * MAX + 1 ] ; int n = Arr1 . length , m = Arr2 . length ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { int element = Arr1 [ i ] + Arr2 [ j ] ; Count [ element ] ++ ; } } for ( int i = 1 ; i <= 2 * MAX ; i ++ ) { if ( Count [ i ] > 0 ) { System . out . print ( i + " - > " + Count [ i ] + "NEW_LINE"); } } } public static void main ( String [ ] args ) { int [ ] arr1 = { 1 , 2 } ; int [ ] arr2 = { 1 , 2 , 1 } ; findCount ( arr1 , arr2 ) ; } }
Find two numbers with difference and division both same as N | Java program for the above approach ; Function to find two numbers with difference and division both as N ; Condition if the answer is not possible ; Calculate a and b ; Print the values ; Driver Code ; Given N ; Function call
class GFG { static void findAandB ( double N ) { if ( N == 1 ) { System . out . print ( " No " ) ; return ; } double a = N * N / ( N - 1 ) ; double b = a / N ; System . out . print ( " a ▁ = ▁ " + a + "NEW_LINE"); System . out . print ( " b ▁ = ▁ " + b + "NEW_LINE"); } public static void main ( String [ ] args ) { double N = 6 ; findAandB ( N ) ; } }
Maximize number of elements from Array with sum at most K | Java program for the above approach ; Function to select a maximum number of elements in array whose sum is at most K ; Sort the array ; Calculate the sum and count while iterating the sorted array ; Iterate for all the elements in the array ; Add the current element to sum ; Increment the count ; Return the answer ; Driver Code ; Given array ; Given sum k ; Function call
import java . util . * ; class GFG { static int maxSelections ( int A [ ] , int n , int k ) { Arrays . sort ( A ) ; int sum = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + A [ i ] ; if ( sum > k ) { break ; } count ++ ; } return count ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 7 , 2 , 9 , 4 } ; int k = 15 ; int n = A . length ; System . out . print ( maxSelections ( A , n , k ) ) ; } }
Pair of integers with difference K having an element as the K | Java program to implement the above problem ; Function to find the required pair ; No pair possible ; Driver Code
class GFG { static void computePair ( double K ) { if ( K == 1 ) { System . out . print ( " No " ) ; return ; } else { System . out . print ( K * K / ( K - 1 ) + " ▁ " ) ; System . out . print ( K / ( K - 1 ) + "NEW_LINE"); } } public static void main ( String [ ] args ) { double K = 6 ; computePair ( K ) ; } }
Generate a Binary String without any consecutive 0 ' s ▁ and ▁ at ▁ most ▁ K ▁ consecutive ▁ 1' s | Java implementation of the above approach ; Function to construct the binary string ; Conditions when string construction is not possible ; Stores maximum 1 's that  can be placed in between ; Place 0 's ; Place 1 's in between ; Count remaining M 's ; Place 1 's at the end ; Place 1 's at the beginning ; Return the final string ; Driver code
class GFG { static String ConstructBinaryString ( int N , int M , int K ) { if ( M < ( N - 1 ) || M > K * ( N + 1 ) ) return " - 1" ; String ans = " " ; int l = Math . min ( K , M / ( N - 1 ) ) ; int temp = N ; while ( temp != 0 ) { temp -- ; ans += '0' ; if ( temp == 0 ) break ; for ( int i = 0 ; i < l ; i ++ ) { ans += '1' ; } } M -= ( N - 1 ) * l ; if ( M == 0 ) return ans ; l = Math . min ( M , K ) ; for ( int i = 0 ; i < l ; i ++ ) ans += '1' ; M -= l ; while ( M > 0 ) { ans = '1' + ans ; M -- ; } return ans ; } public static void main ( String [ ] args ) { int N = 5 , M = 9 , K = 2 ; System . out . println ( ConstructBinaryString ( N , M , K ) ) ; } }
Find smallest number formed by inverting digits of given number N | Java program for the above approach ; Function to invert the digits of integer N to form minimum possible number ; Initialize the array ; Iterate till the number N exists ; Last digit of the number N ; Checking if the digit is smaller than 9 - digit ; Store the smaller digit in the array ; Reduce the number each time ; Check if the digit starts with 0 or not ; Print the answer ; Driver Code ; Given Number ; Function Call
class GFG { static void number ( int num ) { int a [ ] = new int [ 20 ] ; int r , i = 0 , j ; while ( num > 0 ) { r = num % 10 ; if ( 9 - r > r ) a [ i ] = r ; else a [ i ] = 9 - r ; i ++ ; num = num / 10 ; } if ( a [ i - 1 ] == 0 ) { System . out . print ( "9" ) ; i -- ; } for ( j = i - 1 ; j >= 0 ; j -- ) System . out . print ( a [ j ] ) ; } public static void main ( String [ ] args ) { int num = 4545 ; number ( num ) ; } }
Check if the concatenation of first N natural numbers is divisible by 3 | Java program for the above approach ; Function that returns True if concatenation of first N natural numbers is divisible by 3 ; Check using the formula ; Driver Code ; Given Number ; Function Call
class GFG { static boolean isDivisible ( int N ) { return ( N - 1 ) % 3 != 0 ; } public static void main ( String [ ] args ) { int N = 6 ; if ( isDivisible ( N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Count of N digit Numbers whose sum of every K consecutive digits is equal | Java program for the above approach ; Function to count the number of N - digit numbers such that sum of every k consecutive digits are equal ; Range of numbers ; Extract digits of the number ; Store the sum of first K digits ; Check for every k - consecutive digits ; If sum is not equal then break the loop ; Increment the count if it satisfy the given condition ; Driver Code ; Given N and K ; Function call
class GFG { static int countDigitSum ( int N , int K ) { int l = ( int ) Math . pow ( 10 , N - 1 ) , r = ( int ) Math . pow ( 10 , N ) - 1 ; int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { int num = i ; int digits [ ] = new int [ N ] ; for ( int j = N - 1 ; j >= 0 ; j -- ) { digits [ j ] = num % 10 ; num /= 10 ; } int sum = 0 , flag = 0 ; for ( int j = 0 ; j < K ; j ++ ) sum += digits [ j ] ; for ( int j = 1 ; j < N - K + 1 ; j ++ ) { int curr_sum = 0 ; for ( int m = j ; m < j + K ; m ++ ) { curr_sum += digits [ m ] ; } if ( sum != curr_sum ) { flag = 1 ; break ; } } if ( flag == 0 ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { int N = 2 , K = 1 ; System . out . print ( countDigitSum ( N , K ) ) ; } }
Perfect Square factors of a Number | Java program to implement the above approach ; Function that returns the count of factors that are perfect squares ; Stores the count of number of times a prime number divides N . ; Stores the number of factors that are perfect square ; Count number of 2 's  that divides N ; Calculate ans according to above formula ; Check for all the possible numbers that can divide it ; Check the number of times prime number i divides it ; Calculate ans according to above formula ; Return final count ; Driver Code
import java . util . * ; class GFG { static int noOfFactors ( int N ) { if ( N == 1 ) return 1 ; int count = 0 ; int ans = 1 ; while ( N % 2 == 0 ) { count ++ ; N = N / 2 ; } ans *= ( count / 2 + 1 ) ; for ( int i = 3 ; i * i <= N ; i = i + 2 ) { count = 0 ; while ( N % i == 0 ) { count ++ ; N = N / i ; } ans *= ( count / 2 + 1 ) ; } return ans ; } public static void main ( String [ ] args ) { int N = 100 ; System . out . print ( noOfFactors ( N ) ) ; } }
Nth angle of a Polygon whose initial angle and per angle increment is given | Java program for the above approach ; Function to check if the angle is possible or not ; Angular sum of a N - sided polygon ; Angular sum of N - sided given polygon ; Check if both sum are equal ; Function to find the nth angle ; Calculate nth angle ; Return the nth angle ; Driver Code ; Checks the possibility of the polygon exist or not ; Print nth angle of the polygon
class GFG { static boolean possible ( int N , int a , int b , int n ) { int sum_of_angle = 180 * ( N - 2 ) ; int Total_angle = ( N * ( ( 2 * a ) + ( N - 1 ) * b ) ) / 2 ; if ( sum_of_angle != Total_angle ) return false ; else return true ; } static int nth_angle ( int N , int a , int b , int n ) { int nth = 0 ; nth = a + ( n - 1 ) * b ; return nth ; } public static void main ( String [ ] args ) { int N = 3 , a = 30 , b = 30 , n = 3 ; if ( possible ( N , a , b , n ) ) System . out . print ( nth_angle ( N , a , b , n ) ) ; else System . out . print ( " Not ▁ Possible " ) ; } }
Check if the given string is linear or not | Java program for the above approach ; Function to check if the given string is linear or not ; Iterate over string ; If character is not same as the first character then return false ; Increment the tmp ; Driver code ; Given String str ; Function call
import java . util . * ; class GFG { static boolean is_linear ( String s ) { int tmp = 0 ; char first = s . charAt ( 0 ) ; for ( int pos = 0 ; pos < s . length ( ) ; pos += tmp ) { if ( s . charAt ( pos ) != first ) { return false ; } tmp ++ ; } return true ; } public static void main ( String [ ] args ) { String str = " aapaxyayziabcde " ; if ( is_linear ( str ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Minimize cost to color all the vertices of an Undirected Graph | Java program to implement the above approach ; Function to implement DFS Traversal to marks all the vertices visited from vertex U ; Mark U as visited ; Traverse the adjacency list of U ; Function to find the minimum cost to color all the vertices of graph ; To store adjacency list ; Loop through the edges to create adjacency list ; To check if a vertex of the graph is visited ; Mark visited to all the vertices that can be reached by colored vertices ; Perform DFS ; To store count of uncolored sub - graphs ; Loop through vertex to count uncolored sub - graphs ; If vertex not visited ; Increase count of uncolored sub - graphs ; Perform DFS to mark visited to all vertices of current sub - graphs ; Calculate minimum cost to color all vertices ; Print the result ; Driver code ; Given number of vertices and edges ; Given edges ; Given cost of coloring and adding an edge ; Given array of colored vertices
import java . util . * ; class GFG { static void DFS ( int U , int [ ] vis , ArrayList < ArrayList < Integer > > adj ) { vis [ U ] = 1 ; for ( Integer V : adj . get ( U ) ) { if ( vis [ V ] == 0 ) DFS ( V , vis , adj ) ; } } static void minCost ( int N , int M , int vCost , int eCost , int sorc [ ] , ArrayList < Integer > colored , int destination [ ] ) { ArrayList < ArrayList < Integer > > adj = new ArrayList < > ( ) ; for ( int i = 0 ; i < N + 1 ; i ++ ) adj . add ( new ArrayList < Integer > ( ) ) ; for ( int i = 0 ; i < M ; i ++ ) { adj . get ( sorc [ i ] ) . add ( destination [ i ] ) ; adj . get ( destination [ i ] ) . add ( sorc [ i ] ) ; } int [ ] vis = new int [ N + 1 ] ; for ( int i = 0 ; i < colored . size ( ) ; i ++ ) { DFS ( colored . get ( i ) , vis , adj ) ; } int X = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( vis [ i ] == 0 ) { X ++ ; DFS ( i , vis , adj ) ; } } int mincost = X * Math . min ( vCost , eCost ) ; System . out . println ( mincost ) ; } public static void main ( String [ ] args ) { int N = 3 , M = 1 ; int sorc [ ] = { 1 } ; int destination [ ] = { 2 } ; int vCost = 3 , eCost = 2 ; ArrayList < Integer > colored = new ArrayList < > ( ) ; colored . add ( 1 ) ; minCost ( N , M , vCost , eCost , sorc , colored , destination ) ; } }
Minimum cost to reduce the integer N to 1 as per given conditions | Java program for the above approach ; Function to find the minimum cost to reduce the integer N to 1 by the given operations ; Check if x is 1 ; Print the answer ; Prestore the answer ; Iterate till n exists ; Divide N by x ; Reduce n by x ; Add the cost ; Update the answer ; Return the answer ; Driver code ; Initialize the variables ; Function call
import java . util . * ; class GFG { static int min_cost ( int n , int x , int p , int q ) { if ( x == 1 ) { System . out . println ( ( n - 1 ) * p ) ; return 0 ; } int ans = ( n - 1 ) * p ; int pre = 0 ; while ( n > 1 ) { int tmp = n / x ; if ( tmp < 0 ) break ; pre += ( n - tmp * x ) * p ; n /= x ; pre += q ; ans = Math . min ( ans , pre + ( n - 1 ) * p ) ; } return ans ; } public static void main ( String [ ] args ) { int n = 5 , x = 2 , p = 2 , q = 3 ; System . out . println ( min_cost ( n , x , p , q ) ) ; } }
Number of ways to sum up a total of N from limited denominations | Java program for the above approach ; Function to find the number of ways to sum up a total of N from limited denominations ; Store the count of denominations ; Stores the final result ; As one of the denominations is rupee 1 , so we can reduce the computation by checking the equality for N - ( A * 1 ) = N - A ; Increment the count for number of ways ; Driver Code ; Given denominations ; Function call
import java . util . * ; class GFG { static int calculateWays ( int arr1 [ ] , int arr2 [ ] , int N ) { int A = arr2 [ 0 ] , B = arr2 [ 1 ] ; int C = arr2 [ 2 ] , D = arr2 [ 3 ] ; int ans = 0 ; for ( int b = 0 ; b <= B && b * 5 <= ( N ) ; b ++ ) for ( int c = 0 ; c <= C && b * 5 + c * 10 <= ( N ) ; c ++ ) for ( int d = 0 ; d <= D && b * 5 + c * 10 + d * 20 <= ( N ) ; d ++ ) if ( ( b * 5 ) + ( c * 10 ) + ( d * 20 ) >= ( N - A ) ) ans ++ ; return ans ; } public static void main ( String [ ] args ) { int N = 123 ; int arr1 [ ] = { 1 , 5 , 10 , 20 } ; int arr2 [ ] = { 6 , 4 , 3 , 5 } ; System . out . print ( calculateWays ( arr1 , arr2 , N ) ) ; } }
Number of ways to sum up a total of N from limited denominations | Java program for the above approach ; Function to find the number of ways to sum up a total of N from limited denominations ; Store the count of denominations ; Stores the final result ; This will give the number of coins for all combinations of coins with value 1 and 5 ; L2 will sum the values of those indices of ways [ ] which is equal to ( N - ( c * 10 + d * 20 ) ) ; Return the final count ; Driver Code ; Given denominations ; Function call
import java . util . * ; class GFG { static int [ ] ways = new int [ 1010 ] ; static int calculateWays ( int arr1 [ ] , int arr2 [ ] , int N ) { int A = arr2 [ 0 ] , B = arr2 [ 1 ] ; int C = arr2 [ 2 ] , D = arr2 [ 3 ] ; int ans = 0 ; for ( int b = 0 ; b <= B && b * 5 <= N ; b ++ ) { for ( int a = 0 ; a <= A && a * 1 + b * 5 <= N ; a ++ ) { ways [ a + b * 5 ] ++ ; } } for ( int c = 0 ; c <= C && c * 10 <= ( N ) ; c ++ ) { for ( int d = 0 ; d <= D && c * 10 + d * 20 <= ( N ) ; d ++ ) { ans += ways [ N - c * 10 - d * 20 ] ; } } return ans ; } public static void main ( String [ ] args ) { int N = 123 ; int arr1 [ ] = { 1 , 5 , 10 , 20 } ; int arr2 [ ] = { 6 , 4 , 3 , 5 } ; System . out . print ( calculateWays ( arr1 , arr2 , N ) ) ; } }
Count of Missing Numbers in a sorted array | Java program for the above approach ; Function that find the count of missing numbers in array a [ ] ; Calculate the count of missing numbers in the array ; Driver code
class GFG { public static void countMissingNum ( int [ ] a , int N ) { int count = a [ N - 1 ] - a [ 0 ] + 1 - N ; System . out . println ( count ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 10 , 20 , 40 } ; int N = arr . length ; countMissingNum ( arr , N ) ; } }
Length of longest subsequence whose XOR value is odd | Java program for the above approach ; Function for find max XOR subsequence having odd value ; Initialize odd and even count ; Count the number of odd and even numbers in given array ; If all values are odd in given array ; If all values are even in given array ; If both odd and even are present in given array ; Driver Code ; Given array arr [ ] ; Function Call
class GFG { static int maxXORSubsequence ( int arr [ ] , int n ) { int i , odd = 0 , even = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & 1 ) != 0 ) odd ++ ; else even ++ ; } int maxlen ; if ( odd == n ) { if ( odd % 2 == 0 ) maxlen = n - 1 ; else maxlen = n ; } else if ( even == n ) { maxlen = 0 ; } else { if ( odd % 2 == 0 ) maxlen = even + odd - 1 ; else maxlen = even + odd ; } return maxlen ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 , 7 } ; int n = arr . length ; System . out . print ( maxXORSubsequence ( arr , n ) ) ; } }
Count of pairs with sum N from first N natural numbers | Java program to count the number of pairs among the first N natural numbers with sum N ; Function to calculate the value of count ; If n is even ; Count of pairs ; Otherwise ; Driver code
import java . io . * ; class GFG { public static int numberOfPairs ( int n ) { if ( n % 2 == 0 ) return n / 2 - 1 ; else return n / 2 ; } public static void main ( String [ ] args ) { int n = 8 ; System . out . println ( numberOfPairs ( n ) ) ; } }
Count pair of strings whose concatenation has every vowel | Java program for the above approach ; Function to return the count of all concatenated String with each vowel at least once ; Concatenating all possible pairs of String ; Creating an array which checks , the presence of each vowel ; Checking for each vowel by traversing the concatenated String ; Checking if all the elements are set in vowel [ ] ; Check if all vowels are present or not ; Return the final count ; Driver Code ; Given array of Strings ; Function Call
import java . util . * ; class GFG { static int good_pair ( String str [ ] , int N ) { int countStr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { String res = str [ i ] + str [ j ] ; int vowel [ ] = new int [ 5 ] ; for ( int k = 0 ; k < res . length ( ) ; k ++ ) { if ( res . charAt ( k ) == ' a ' ) vowel [ 0 ] = 1 ; else if ( res . charAt ( k ) == ' e ' ) vowel [ 1 ] = 1 ; else if ( res . charAt ( k ) == ' i ' ) vowel [ 2 ] = 1 ; else if ( res . charAt ( k ) == ' o ' ) vowel [ 3 ] = 1 ; else if ( res . charAt ( k ) == ' u ' ) vowel [ 4 ] = 1 ; } int temp = 0 ; for ( int ind = 0 ; ind < 5 ; ind ++ ) { if ( vowel [ ind ] == 1 ) temp ++ ; } if ( temp == 5 ) countStr ++ ; } } return countStr ; } public static void main ( String [ ] args ) { String arr [ ] = { " aaweiolkju " , " oxdfgujkmi " } ; int N = arr . length ; System . out . print ( good_pair ( arr , N ) ) ; } }
Find sum of exponents of prime factors of numbers 1 to N | Java program for the above approach ; Function to implement sieve of erastosthenes ; Create a boolean array and initialize all entries as false ; Initializing smallest factor equal to 2 for all the even numbers ; Iterate for odd numbers less then equal to n ; s ( i ) for a prime is the number itself ; For all multiples of current prime number ; i is the smallest prime factor for number " i * j " ; Function to generate prime factors and its power ; s [ i ] is going to store smallest prime factor of i ; Current prime factor of N ; Power of current prime factor ; Calculating prime factors and their powers sum ; Increment the count and continue the process ; Add count to the sum ; Reinitialize count ; Return the result ; Function to find the sum of all the power of prime factors of N ; Iterate for in [ 2 , N ] ; Driver Code ; Given Number N ; Function Call
class GFG { static void sieveOfEratosthenes ( int N , int s [ ] ) { boolean [ ] prime = new boolean [ N + 1 ] ; for ( int i = 2 ; i <= N ; i += 2 ) s [ i ] = 2 ; for ( int i = 3 ; i <= N ; i += 2 ) { if ( prime [ i ] == false ) { s [ i ] = i ; for ( int j = i ; j * i <= N ; j += 2 ) { if ( prime [ i * j ] == false ) { prime [ i * j ] = true ; s [ i * j ] = i ; } } } } } static int generatePrimeFactors ( int N ) { int [ ] s = new int [ N + 1 ] ; int sum = 0 ; sieveOfEratosthenes ( N , s ) ; int curr = s [ N ] ; int cnt = 1 ; while ( N > 1 ) { N /= s [ N ] ; if ( curr == s [ N ] ) { cnt ++ ; continue ; } sum = sum + cnt ; curr = s [ N ] ; cnt = 1 ; } return sum ; } static void findSum ( int N ) { int sum = 0 ; for ( int i = 2 ; i <= N ; i ++ ) { sum += generatePrimeFactors ( i ) ; } System . out . print ( sum + "NEW_LINE"); } public static void main ( String [ ] args ) { int N = 4 ; findSum ( N ) ; } }