codeparrot/xlcost-text-to-code huggingface dataset:
Construct a Perfect Binary Tree from Preorder Traversal | Java program for the above approach ; Structure of the tree ; Function to create a new node with the value val ; Return the newly created node ; Function to create the Perfect Binary Tree ; If preStart > preEnd return NULL ; Initialize root as pre [ preStart ] ; If the only node is left , then return node ; Parameters for further recursion ; Recursive Call to build the subtree of root node ; Return the created root ; Function to build Perfect Binary Tree ; Function to print the Inorder of the given Tree ; Base Case ; Left Recursive Call ; Print the data ; Right Recursive Call ; Driver Code ; Function Call ; Print Inorder Traversal
public class Main { static class Node { public int data ; public Node left , right ; public Node ( int val ) { data = val ; left = right = null ; } } static Node getNewNode ( int val ) { Node newNode = new Node ( val ) ; return newNode ; } static Node buildPerfectBT_helper ( int preStart , int preEnd , int [ ] pre ) { if ( preStart > preEnd ) return null ; Node root = getNewNode ( pre [ preStart ] ) ; if ( preStart == preEnd ) return root ; int leftPreStart = preStart + 1 ; int rightPreStart = leftPreStart + ( preEnd - leftPreStart + 1 ) / 2 ; int leftPreEnd = rightPreStart - 1 ; int rightPreEnd = preEnd ; root . left = buildPerfectBT_helper ( leftPreStart , leftPreEnd , pre ) ; root . right = buildPerfectBT_helper ( rightPreStart , rightPreEnd , pre ) ; return root ; } static Node buildPerfectBT ( int [ ] pre , int size ) { return buildPerfectBT_helper ( 0 , size - 1 , pre ) ; } static void printInorder ( Node root ) { if ( root == null ) return ; printInorder ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; printInorder ( root . right ) ; } public static void main ( String [ ] args ) { int [ ] pre = { 1 , 2 , 4 , 5 , 3 , 6 , 7 } ; int N = pre . length ; Node root = buildPerfectBT ( pre , N ) ; System . out . print ( " Inorder ▁ traversal ▁ of ▁ the ▁ tree : ▁ " ) ; printInorder ( root ) ; } }
Minimize operations to convert each node of N | Java program for the above approach ; Create adjacency list ; Function to add an edges in graph ; Function to perform the DFS of graph recursively from a given vertex u ; Check for the condition for the flipping of node 's initial value ; Traverse all the children of the current source node u ; Swap foo and foo1 signifies there is change of level ; Function to perform the DFSUtil ( ) for all the unvisited vertices ; Traverse the given set of nodes ; If the current node is unvisited ; Print the number of operations ; Function to count the number of flips required to change initial node values to final node value ; Add the given edges ; DFS Traversal ; Driver code
import java . util . * ; public class Main { static int N = 3 ; static Vector < Vector < Integer > > adj = new Vector < Vector < Integer > > ( ) ; static Vector < Boolean > visited ; static int ans ; static void addEdges ( int u , int v ) { adj . get ( u ) . add ( v ) ; adj . get ( v ) . add ( u ) ; } static void DFSUtil ( int u , boolean foo , boolean foo1 , boolean [ ] initial , boolean [ ] finall ) { visited . set ( u , true ) ; if ( ( initial [ u - 1 ] ^ foo ) ^ finall [ u - 1 ] == true ) { ans += 1 ; foo ^= true ; } for ( int i = 0 ; i < adj . get ( u ) . size ( ) ; i ++ ) { if ( visited . get ( adj . get ( u ) . get ( i ) ) == false ) DFSUtil ( adj . get ( u ) . get ( i ) , foo1 , foo , initial , finall ) ; } } static void DFS ( int V , boolean [ ] initial , boolean [ ] finall ) { ans = 0 ; visited = new Vector < Boolean > ( ) ; for ( int i = 0 ; i < V ; i ++ ) { visited . add ( false ) ; } for ( int u = 1 ; u <= 1 ; u ++ ) { if ( visited . get ( u ) == false ) DFSUtil ( u , false , false , initial , finall ) ; } System . out . print ( ans ) ; } static void countOperations ( int N , boolean [ ] initial , boolean [ ] finall , int [ ] [ ] Edges ) { for ( int i = 0 ; i < N - 1 ; i ++ ) { addEdges ( Edges [ i ] [ 0 ] , Edges [ i ] [ 1 ] ) ; } DFS ( N + 1 , initial , finall ) ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < N + 1 ; i ++ ) { adj . add ( new Vector < Integer > ( ) ) ; } int [ ] [ ] Edges = { { 1 , 2 } , { 1 , 3 } } ; boolean [ ] initial = { true , true , false } ; boolean [ ] finall = { false , true , true } ; countOperations ( N , initial , finall , Edges ) ; } }
Minimum number of moves to make M and N equal by repeatedly adding any divisor of number to itself except 1 and the number | Java program for above approach ;  ; Function to find the minimum number of moves to make N and M equal . ; Array to maintain the numbers included . ; Pair of vertex , count ; Run bfs from N ; If we reached goal ; Iterate in the range ; If i is a factor of aux ; If i is less than M - aux and is not included earlier . ; If aux / i is less than M - aux and is not included earlier . ; Not possible ; Driver Code
import java . util . * ; class GFG { static class pair < T , V > { T first ; V second ; } static pair < Integer , Integer > make_pair ( int f , int s ) { pair < Integer , Integer > p = new pair < > ( ) ; p . first = f ; p . second = s ; return p ; } static int countOperations ( int N , int M ) { boolean [ ] visited = new boolean [ 100001 ] ; Arrays . fill ( visited , false ) ; Queue < pair < Integer , Integer > > Q = new LinkedList < > ( ) ; Q . add ( make_pair ( N , 0 ) ) ; visited [ N ] = true ; while ( ! Q . isEmpty ( ) ) { int aux = Q . peek ( ) . first ; int cont = Q . peek ( ) . second ; Q . remove ( ) ; if ( aux == M ) return cont ; for ( int i = 2 ; i * i <= aux ; i ++ ) if ( aux % i == 0 ) { if ( aux + i <= M && ! visited [ aux + i ] ) { Q . add ( make_pair ( aux + i , cont + 1 ) ) ; visited [ aux + i ] = true ; } if ( aux + aux / i <= M && ! visited [ aux + aux / i ] ) { Q . add ( make_pair ( aux + aux / i , cont + 1 ) ) ; visited [ aux + aux / i ] = true ; } } } return - 1 ; } public static void main ( String [ ] args ) { int N = 4 , M = 24 ; System . out . println ( countOperations ( N , M ) ) ; } }
Finding Astronauts from different countries | Java program for the above approach ; Function to perform the DFS Traversal to find the count of connected components ; Function to perform the DFS Traversal to find the count of connected components ; Marking vertex visited ; DFS call to neighbour vertices ; If the current node is not visited , then recursively call DFS ; Function to find the number of ways to choose two astronauts from the different countries ; Stores the Adjacency list ; Constructing the graph ; Stores the visited vertices ; Stores the size of every connected components ; DFS call to the graph ; Store size of every connected component ; Stores the total number of ways to count the pairs ; Traverse the array ; Print the value of ans ; Driver Code
import java . util . * ; public class GFG { static Vector < Vector < Integer > > adj ; static boolean [ ] visited ; static int num ; static void dfs ( int v ) { visited [ v ] = true ; num ++ ; for ( int i = 0 ; i < adj . get ( v ) . size ( ) ; i ++ ) { if ( ! visited [ adj . get ( v ) . get ( i ) ] ) { dfs ( adj . get ( v ) . get ( i ) ) ; } } } static void numberOfPairs ( int N , int [ ] [ ] arr ) { adj = new Vector < Vector < Integer > > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { adj . add ( new Vector < Integer > ( ) ) ; } for ( int i = 0 ; i < 2 ; i ++ ) { adj . get ( arr [ i ] [ 0 ] ) . add ( arr [ i ] [ 1 ] ) ; adj . get ( arr [ i ] [ 1 ] ) . add ( arr [ i ] [ 0 ] ) ; } visited = new boolean [ N ] ; Arrays . fill ( visited , false ) ; Vector < Integer > v = new Vector < Integer > ( ) ; num = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! visited [ i ] ) { dfs ( i ) ; v . add ( num ) ; num = 0 ; } } int ans = N * ( N - 1 ) / 2 + 1 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { ans -= ( v . get ( i ) * ( v . get ( i ) - 1 ) / 2 ) + 1 ; } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int N = 6 ; int [ ] [ ] arr = { { 0 , 1 } , { 0 , 2 } , { 2 , 5 } } ; numberOfPairs ( N , arr ) ; } }
Count of Perfect Numbers in given range for Q queries | C ++ program for the above approach ; Function to check whether a number is perfect Number ; Stores sum of divisors ; Itearate over the range [ 2 , sqrt ( N ) ] ; If sum of divisors is equal to N , then N is a perfect number ; Function to find count of perfect numbers in a given range ; Stores the count of perfect Numbers upto a every number less than MAX ; Iterate over the range [ 1 , MAX ] ; Traverse the array arr [ ] ; Print the count of perfect numbers in the range [ arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ] ; Driver Code
import java . util . * ; public class MyClass { static int MAX = 100005 ; static int isPerfect ( long N ) { long sum = 1 ; for ( long i = 2 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { if ( i * i != N ) sum = sum + i + N / i ; else sum = sum + i ; } } if ( sum == N && N != 1 ) return 1 ; return 0 ; } static void Query ( int arr [ ] [ ] , int N ) { int [ ] prefix = new int [ MAX + 1 ] ; Arrays . fill ( prefix , 0 ) ; for ( int i = 2 ; i <= MAX ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] + isPerfect ( i ) ; } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( prefix [ arr [ i ] [ 1 ] ] - prefix [ arr [ i ] [ 0 ] - 1 ] + " ▁ " ) ; } } public static void main ( String args [ ] ) { int [ ] [ ] arr = { { 1 , 1000 } , { 1000 , 2000 } , { 2000 , 3000 } } ; int N = arr . length ; Query ( arr , N ) ; } }
2 Keys Keyboard Problem | Java Program for the above approach ; Function to find the minimum number of steps required to form N number of A 's ; Stores the count of steps needed ; Traverse over the range [ 2 , N ] ; Iterate while N is divisible by d ; Increment the value of ans by d ; Divide N by d ; If N is not 1 ; Return the ans ; Driver Code
import java . io . * ; class GFG { static int minSteps ( int N ) { int ans = 0 ; for ( int d = 2 ; d * d <= N ; d ++ ) { while ( N % d == 0 ) { ans += d ; N /= d ; } } if ( N != 1 ) { ans += N ; } return ans ; } public static void main ( String [ ] args ) { int N = 3 ; minSteps ( N ) ; System . out . println ( minSteps ( N ) ) ; } }
Find any possible two coordinates of Rectangle whose two coordinates are given | Java program for above approach ; Function to find the remaining two rectangle coordinates ; Pairs to store the position of given two coordinates of the rectangle . ; Pairs to store the remaining two coordinates of the rectangle . ; Traverse through matrix and find pairs p1 and p2 ; First Case ; Second Case ; Third Case ; Print the matrix ; Driver Code ; Given Input ; Function Call
import java . awt . * ; import java . util . * ; class GFG { static class pair < T , V > { T first ; V second ; } static void Create_Rectangle ( ArrayList < String > arr , int n ) { pair < Integer , Integer > p1 = new pair < > ( ) ; p1 . first = - 1 ; p1 . second = - 1 ; pair < Integer , Integer > p2 = new pair < > ( ) ; p2 . first = - 1 ; p2 . second = - 1 ; pair < Integer , Integer > p3 = new pair < > ( ) ; pair < Integer , Integer > p4 = new pair < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( arr . get ( i ) . charAt ( j ) == '1' ) if ( p1 . first == - 1 ) { p1 . first = i ; p1 . second = j ; } else { p2 . first = i ; p2 . second = j ; } } } p3 = p1 ; p4 = p2 ; if ( p1 . first . intValue ( ) == ( p2 . first ) . intValue ( ) ) { p3 . first = ( p1 . first + 1 ) % n ; p4 . first = ( p2 . first + 1 ) % n ; } else if ( p1 . second . intValue ( ) == ( p2 . second ) . intValue ( ) ) { p3 . second = ( p1 . second + 1 ) % n ; p4 . second = ( p2 . second + 1 ) % n ; } else { int temp = p3 . first ; p3 . first = p4 . first ; p4 . first = temp ; } for ( int i = 0 ; i < n ; i ++ ) { if ( i == p3 . first ) { for ( int j = 0 ; j < n ; j ++ ) { if ( j == p3 . second ) System . out . print ( '1' ) ; else System . out . print ( arr . get ( i ) . charAt ( j ) ) ; } } else if ( i == p4 . first ) { for ( int j = 0 ; j < n ; j ++ ) { if ( j == p4 . second ) System . out . print ( '1' ) ; else System . out . print ( arr . get ( i ) . charAt ( j ) ) ; } } else System . out . print ( arr . get ( i ) ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int n = 4 ; ArrayList < String > arr = new ArrayList < > ( ) ; arr . add ( "0010" ) ; arr . add ( "0000" ) ; arr . add ( "1000" ) ; arr . add ( "0000" ) ; Create_Rectangle ( arr , n ) ; } }
Sum of all the prime divisors of a number | Set 2 | Java program for the above approach ; Function to find sum of prime divisors of the given number N ; Add the number 2 if it divides N ; Traverse the loop from [ 3 , sqrt ( N ) ] ; If i divides N , add i and divide N ; This condition is to handle the case when N is a prime number greater than 2 ; Driver code ; Given Input ; Function Call
import java . io . * ; class GFG { public static int SumOfPrimeDivisors ( int n ) { int sum = 0 ; if ( n % 2 == 0 ) { sum = sum + 2 ; } while ( n % 2 == 0 ) { n = n / 2 ; } for ( int i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { sum = sum + i ; } while ( n % i == 0 ) { n = n / i ; } } if ( n > 2 ) { sum = sum + n ; } return sum ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( SumOfPrimeDivisors ( n ) ) ; } }
Count bases which contains a set bit as the Most Significant Bit in the representation of N | Java program for the above approach ; Function to count bases having MSB of N as a set bit ; Store the required count ; Iterate over the range [ 2 , N ] ; Store the MSB of N ; If MSB is 1 , then increment the count by 1 ; Return the count ; DRIVER CODE
import java . io . * ; class GFG { public static int countOfBase ( int N ) { int count = 0 ; for ( int i = 2 ; i <= N ; ++ i ) { int highestPower = ( int ) ( Math . log ( N ) / Math . log ( i ) ) ; int firstDigit = N / ( int ) Math . pow ( i , highestPower ) ; if ( firstDigit == 1 ) { ++ count ; } } return count ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . println ( countOfBase ( N ) ) ; } }
K | Java program for the above approach ; Function to find the kth digit from last in an integer n ; If k is less than equal to 0 ; Convert integer into string ; If k is greater than length of the string temp ; Print the k digit from last ; Driver code ; Given Input ; Function call
import java . io . * ; class GFG { public static void kthDigitFromLast ( int n , int k ) { if ( k <= 0 ) { System . out . println ( - 1 ) ; return ; } String temp = Integer . toString ( n ) ; if ( k > temp . length ( ) ) { System . out . println ( - 1 ) ; } else { int index = temp . length ( ) - k ; int res = temp . charAt ( index ) - '0' ; System . out . println ( res ) ; } } public static void main ( String [ ] args ) { int n = 2354 ; int k = 2 ; kthDigitFromLast ( n , k ) ; } }
Count ways to split array into three non | Java program for the above approach ; Function to count ways to split array into three subarrays with equal Bitwise XOR ; Stores the XOR value of arr [ ] ; Update the value of arr_xor ; Stores the XOR value of prefix and suffix array respectively ; Stores the ending points of all the required prefix arrays ; Stores the count of suffix arrays whose XOR value is equal to the total XOR value at each index ; Find all prefix arrays with XOR value equal to arr_xor ; Update pref_xor ; Fill the values of suff_inds [ ] ; Update suff_xor ; Update suff_inds [ i ] ; Stores the total number of ways ; Count total number of ways ; Return the final count ; Driver code ; Given Input ; Function Call
import java . lang . * ; import java . util . * ; class GFG { static int countWays ( int arr [ ] , int N ) { int arr_xor = 0 ; for ( int i = 0 ; i < N ; i ++ ) arr_xor ^= arr [ i ] ; int pref_xor = 0 , suff_xor = 0 ; ArrayList < Integer > pref_ind = new ArrayList < > ( ) ; int [ ] suff_inds = new int [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { pref_xor ^= arr [ i ] ; if ( pref_xor == arr_xor ) pref_ind . add ( i ) ; } for ( int i = N - 1 ; i >= 0 ; i -- ) { suff_xor ^= arr [ i ] ; suff_inds [ i ] += suff_inds [ i + 1 ] ; if ( suff_xor == arr_xor ) suff_inds [ i ] ++ ; } int tot_ways = 0 ; for ( Integer idx : pref_ind ) { if ( idx < N - 1 ) tot_ways += suff_inds [ idx + 2 ] ; } return tot_ways ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 0 , 5 , 2 , 7 } ; int N = arr . length ; System . out . println ( countWays ( arr , N ) ) ; } }
Count pairs up to N having sum equal to their XOR | Java program for the above approach ; 2D array for memoization ; Recursive Function to count pairs ( x , y ) such that x + y = x ^ y ; If the String is traversed completely ; If the current subproblem is already calculated ; If bound = 1 and s [ i ] = = '0' , only ( 0 , 0 ) can be placed ; Otherwise ; Placing ( 0 , 1 ) and ( 1 , 0 ) are equivalent . Hence , multiply by 2. ; Place ( 0 , 0 ) at the current position . ; Return the answer ;  ; Utility Function to convert N to its binary representation ; Function to count pairs ( x , y ) such that x + y = x ^ y ; Convert the number to equivalent binary representation ; Initialize dp array with - 1. ; Print answer returned by recursive function ; Driver code ; Input ; Function call
import java . util . * ; class GFG { static int [ ] [ ] dp = new int [ 1000 ] [ 2 ] ; static int IsSumEqualsXor ( int i , int n , int bound , char [ ] s ) { if ( i == n ) return 1 ; if ( dp [ i ] [ bound ] != - 1 ) return dp [ i ] [ bound ] ; int ans = 0 ; if ( bound != 0 && s [ i ] == '0' ) { ans = IsSumEqualsXor ( i + 1 , n , 1 , s ) ; } else { ans = 2 * IsSumEqualsXor ( i + 1 , n , bound != 0 & ( s [ i ] == '1' ) ? 1 : 0 , s ) ; ans += IsSumEqualsXor ( i + 1 , n , 0 , s ) ; } return dp [ i ] [ bound ] = ans ; } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , 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 ) ; } static String convertToBinary ( int n ) { String ans = " " ; while ( n > 0 ) { char rem = ( char ) ( n % 2 + '0' ) ; ans += ( rem ) ; n /= 2 ; } ans = reverse ( ans ) ; return ans ; } static void IsSumEqualsXorUtil ( int N ) { String s = convertToBinary ( N ) ; for ( int i = 0 ; i < dp . length ; i ++ ) { for ( int j = 0 ; j < dp [ 0 ] . length ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } System . out . print ( IsSumEqualsXor ( 0 , s . length ( ) , 1 , s . toCharArray ( ) ) + "NEW_LINE"); } public static void main ( String [ ] args ) { int N = 10 ; IsSumEqualsXorUtil ( N ) ; } }
Mean of fourth powers of first N natural numbers | Java program for the above approach ; Function to find the average of the fourth power of first N natural numbers ; Stores the sum of the fourth powers of first N natural numbers ; Calculate the sum of fourth power ; Return the average ; Driver code
class GFG { static double findAverage ( int N ) { double S = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { S += i * i * i * i ; } return S / N ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . println ( findAverage ( N ) ) ; } }
Construct a graph from given degrees of all vertices | Java program to generate a graph for a given fixed degrees ; A function to print the adjacency matrix . ; n is number of vertices ; For each pair of vertex decrement the degree of both vertex . ; Print the result in specified format ; Driver Code
import java . util . * ; class GFG { static void printMat ( int degseq [ ] , int n ) { int [ ] [ ] mat = new int [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( degseq [ i ] > 0 && degseq [ j ] > 0 ) { degseq [ i ] -- ; degseq [ j ] -- ; mat [ i ] [ j ] = 1 ; mat [ j ] [ i ] = 1 ; } } } System . out . print ( " " ▁ + ▁ setw ( 3 ) ▁ + ▁ " "); for ( int i = 0 ; i < n ; i ++ ) System . out . print ( setw ( 3 ) + " ( " + i + " ) " ) ; System . out . print ( "NEW_LINE"); for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( setw ( 4 ) + " ( " + i + " ) " ) ; for ( int j = 0 ; j < n ; j ++ ) System . out . print ( setw ( 5 ) + mat [ i ] [ j ] ) ; System . out . print ( "NEW_LINE"); } } static String setw ( int n ) { String space = " " ; while ( n -- > 0 ) space += " ▁ " ; return space ; } public static void main ( String [ ] args ) { int degseq [ ] = { 2 , 2 , 1 , 1 , 1 } ; int n = degseq . length ; printMat ( degseq , n ) ; } }
Decimal equivalent of concatenation of absolute difference of floor and rounded | Java program for above approach ; Function to find the decimal equivalent of the new binary array constructed from absolute decimal of floor and the round - off values ; Traverse the givenarray from the end ; Stores the absolute difference between floor and round - off each array element ; If bit / difference is 1 , then calculate the bit by proper power of 2 and add it to result ; Increment the value of power ; Print the result ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void findDecimal ( double arr [ ] , int N ) { int bit , power = 0 , result = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { bit = Math . abs ( ( int ) Math . floor ( arr [ i ] ) - ( int ) Math . round ( arr [ i ] ) ) ; if ( bit != 0 ) result += Math . pow ( 2 , power ) ; power ++ ; } System . out . print ( result ) ; } public static void main ( String [ ] args ) { double arr [ ] = { 1.2 , 2.6 , 4.2 , 6.9 , 3.1 , 21.6 , 91.2 } ; int N = arr . length ; findDecimal ( arr , N ) ; } }
Calculate money placed in boxes after N days based on given conditions | Java program for the above approach ; Function to find the total money placed in boxes after N days ; Stores the total money ; Iterate for N days ; Adding the Week number ; Adding previous amount + 1 ; Return the total amount ; Driver Code ; Input ; Function call to find total money placed
import java . io . * ; class GFG { public static int totalMoney ( int N ) { int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { ans += i / 7 ; ans += ( i % 7 + 1 ) ; } return ans ; } public static void main ( String [ ] args ) { int N = 15 ; System . out . println ( totalMoney ( N ) ) ; } }
Calculate money placed in boxes after N days based on given conditions | Java program for above approach ; Function to find total money placed in the box ; Number of complete weeks ; Remaining days in the last week ; Driver Code ; Input ; Function call to find the total money placed
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void totalMoney ( int N ) { int CompWeeks = N / 7 ; int RemDays = N % 7 ; int X = 28 * CompWeeks + 7 * ( CompWeeks * ( CompWeeks - 1 ) / 2 ) ; int Y = RemDays * ( RemDays + 1 ) / 2 + CompWeeks * RemDays ; int cost = X + Y ; System . out . print ( cost ) ; } public static void main ( String [ ] args ) { int N = 15 ; totalMoney ( N ) ; } }