codeparrot/xlcost-text-to-code huggingface dataset:
Centered Pentadecagonal Number | Java Program to find nth centered pentadecagonal number ; centered pentadecagonal function ; Formula to calculate nth centered pentadecagonal number ; Driver Code
import java . io . * ; class GFG { static long center_pentadecagonal_num ( long n ) { return ( 15 * n * n - 15 * n + 2 ) / 2 ; } public static void main ( String [ ] args ) { long n = 3 ; System . out . print ( n + " th ▁ number ▁ : ▁ " ) ; System . out . println ( center_pentadecagonal_num ( n ) ) ; n = 10 ; System . out . print ( n + " th ▁ number ▁ : ▁ " ) ; System . out . println ( center_pentadecagonal_num ( n ) ) ; } }
Centered nonadecagonal number | Java Program to find nth centered nonadecagonal number ; centered nonadecagonal function ; Formula to calculate nth centered nonadecagonal number ; Driver code
import java . io . * ; class GFG { static int center_nonadecagon_num ( int n ) { return ( 19 * n * n - 19 * n + 2 ) / 2 ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . print ( n + " th ▁ centered ▁ " + " nonadecagonal ▁ number ▁ : ▁ " ) ; System . out . println ( center_nonadecagon_num ( n ) ) ; n = 7 ; System . out . print ( n + " th ▁ centered ▁ " + " nonadecagonal ▁ number ▁ : ▁ " ) ; System . out . println ( center_nonadecagon_num ( n ) ) ; } }
Hendecagonal number | Java program to find nth Hendecagonal number ; Function to find Hendecagonal number ; Formula to calculate nth Hendecagonal number ; Driver Code
import java . io . * ; class GFG { static int hendecagonal_num ( int n ) { return ( 9 * n * n - 7 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . print ( n + " rd ▁ Hendecagonal ▁ " + " number : ▁ " ) ; System . out . println ( hendecagonal_num ( n ) ) ; n = 10 ; System . out . print ( n + " th ▁ Hendecagonal ▁ " + " number : ▁ " ) ; System . out . println ( hendecagonal_num ( n ) ) ; } }
Centered Octagonal Number | Java Program to find nth centered octagonal number ; Function to find centered octagonal number ; Formula to calculate nth centered octagonal number and return it into main function ; Driver Code
import java . io . * ; class GFG { static int centeredoctagonalNumber ( int n ) { return 4 * n * ( n - 1 ) + 1 ; } public static void main ( String args [ ] ) { int n = 6 ; System . out . print ( n + " th ▁ centered ▁ " + " octagonal ▁ number : ▁ " ) ; System . out . println ( centeredoctagonalNumber ( n ) ) ; n = 11 ; System . out . print ( n + " th ▁ centered ▁ " + " octagonal ▁ number : ▁ " ) ; System . out . println ( centeredoctagonalNumber ( n ) ) ; } }
Number of ordered points pair satisfying line equation | Java code to find number of ordered points satisfying line equation ; Checks if ( i , j ) is valid , a point ( i , j ) is valid if point ( arr [ i ] , arr [ j ] ) satisfies the equation y = mx + c And i is not equal to j ; check if i equals to j ; Equation LHS = y , and RHS = mx + c ; Returns the number of ordered pairs ( i , j ) for which point ( arr [ i ] , arr [ j ] ) satisfies the equation of the line y = mx + c ; for every possible ( i , j ) check if ( a [ i ] , a [ j ] ) satisfies the equation y = mx + c ; ( firstIndex , secondIndex ) is same as ( i , j ) ; check if ( firstIndex , secondIndex ) is a valid point ; Driver Code ; equation of line is y = mx + c
import java . io . * ; public class GFG { static boolean isValid ( int [ ] arr , int i , int j , int m , int c ) { if ( i == j ) return false ; int lhs = arr [ j ] ; int rhs = m * arr [ i ] + c ; return ( lhs == rhs ) ; } static int findOrderedPoints ( int [ ] arr , int n , int m , int c ) { int counter = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { int firstIndex = i , secondIndex = j ; if ( isValid ( arr , firstIndex , secondIndex , m , c ) ) counter ++ ; } } return counter ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 2 , 3 , 4 , 2 } ; int n = arr . length ; int m = 1 , c = 1 ; System . out . print ( findOrderedPoints ( arr , n , m , c ) ) ; } }
Check if a given circle lies completely inside the ring formed by two concentric circles | Java code to check if a circle lies in the ring ; Function to check if circle lies in the ring ; distance between center of circle center of concentric circles ( origin ) using Pythagoras theorem ; Condition to check if circle is strictly inside the ring ; Driver Code ; Both circle with radius ' r ' and ' R ' have center ( 0 , 0 )
import java . io . * ; class ring { public static boolean checkcircle ( int r , int R , int r1 , int x1 , int y1 ) { int dis = ( int ) Math . sqrt ( x1 * x1 + y1 * y1 ) ; return ( dis - r1 >= R && dis + r1 <= r ) ; } public static void main ( String args [ ] ) { int r = 8 , R = 4 , r1 = 2 , x1 = 6 , y1 = 0 ; if ( checkcircle ( r , R , r1 , x1 , y1 ) ) System . out . println ( " yes " ) ; else System . out . println ( " no " ) ; } }
Program for Surface Area of Octahedron | Java Program to calculate surface area of Octahedron . ; utility Function ; Driver Function
import java . io . * ; import java . util . * ; class GFG { static double surface_area_octahedron ( double side ) { return ( 2 * ( Math . sqrt ( 3 ) ) * ( side * side ) ) ; } public static void main ( String [ ] args ) { double side = 7 ; System . out . println ( " Surface ▁ area ▁ of ▁ octahedron ▁ = " + surface_area_octahedron ( side ) ) ; } }
Count of different straight lines with total n points with m collinear | Java program to count number of straight lines with n total points , out of which m are collinear . ; Returns value of binomial coefficient Code taken from https : goo . gl / vhy4jp ; C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; function to calculate number of straight lines can be formed ; Driver function
import java . util . * ; import java . lang . * ; public class GfG { public static int nCk ( int n , int k ) { int [ ] C = new int [ k + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = Math . min ( i , k ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } return C [ k ] ; } public static int count_Straightlines ( int n , int m ) { return ( nCk ( n , 2 ) - nCk ( m , 2 ) + 1 ) ; } public static void main ( String argc [ ] ) { int n = 4 , m = 3 ; System . out . println ( count_Straightlines ( n , m ) ) ; } }
Calculate Volume of Dodecahedron | Java program to calculate Volume of dodecahedron ; utility Function ; driver function
import java . io . * ; class GFG { static double vol_of_dodecahedron ( int side ) { return ( ( ( 15 + ( 7 * ( Math . sqrt ( 5 ) ) ) ) / 4 ) * ( Math . pow ( side , 3 ) ) ) ; } public static void main ( String [ ] args ) { int side = 4 ; System . out . print ( " Volume ▁ of ▁ dodecahedron ▁ = ▁ " ) ; System . out . println ( vol_of_dodecahedron ( side ) ) ; } }
Program to check if water tank overflows when n solid balls are dipped in the water tank | JAVA Code for Program to check if water tank overflows ; function to find if tak will overflow or not ; cylinder capacity ; volume of water in tank ; volume of n balls ; total volume of water and n dipped balls ; condition to check if tank is in overflow state or not ; Driver program to test above function ; giving dimensions ; calling function
import java . util . * ; class GFG { static void overflow ( int H , int r , int h , int N , int R ) { double tank_cap = 3.14 * r * r * H ; double water_vol = 3.14 * r * r * h ; double balls_vol = N * ( 4 / 3 ) * 3.14 * R * R * R ; double vol = water_vol + balls_vol ; if ( vol > tank_cap ) { System . out . println ( " Overflow " ) ; } else { System . out . println ( " Not ▁ in ▁ overflow ▁ state " ) ; } } public static void main ( String [ ] args ) { int H = 10 , r = 5 , h = 5 , N = 2 , R = 2 ; overflow ( H , r , h , N , R ) ; } }
Program to check if tank will overflow , underflow or filled in given time | Java program to check if Tank will overflow or not in given time ; function to calculate the volume of tank ; function to print overflow / filled / underflow accordingly ; driver code ; radius of the tank ; height of the tank ; rate of flow of water ; time given ; calculate the required time ; printing the result
class Number { public static float volume ( int radius , int height ) { return ( ( 22 / 7 ) * radius * radius * height ) ; } public static void check_and_print ( double required_time , double given_time ) { if ( required_time < given_time ) System . out . print ( " Overflow " ) ; else if ( required_time > given_time ) System . out . print ( " Underflow " ) ; else System . out . print ( " Filled " ) ; } public static void main ( String [ ] args ) { int radius = 5 , height = 10 , rate_of_flow = 10 ; double given_time = 70.0 ; double required_time = volume ( radius , height ) / rate_of_flow ; check_and_print ( required_time , given_time ) ; } }
Program to find third side of triangle using law of cosines | Java program to find third side of triangle using law of cosines ; Function to calculate cos value of angle c ; Converting degrees to radian ; Maps the sum along the series ; Holds the actual value of sin ( n ) ; Function to find third side ; Driver code ; function call
class GFG { static float cal_cos ( float n ) { float accuracy = 0.0001f , x1 ; float denominator , cosx , cosval ; n = n * ( 3.142f / 180.0f ) ; x1 = 1 ; cosx = x1 ; cosval = ( float ) Math . cos ( n ) ; int i = 1 ; do { denominator = 2 * i * ( 2 * i - 1 ) ; x1 = - x1 * n * n / denominator ; cosx = cosx + x1 ; i = i + 1 ; } while ( accuracy <= Math . abs ( cosval - cosx ) ) ; return cosx ; } static float third_side ( int a , int b , float c ) { float angle = cal_cos ( c ) ; return ( float ) Math . sqrt ( ( a * a ) + ( b * b ) - 2 * a * b * angle ) ; } public static void main ( String [ ] args ) { float c = 49 ; int a = 5 , b = 8 ; System . out . print ( Math . round ( third_side ( a , b , c ) * 100000.0 ) / 100000.0 ) ; } }
Check whether given circle resides in boundary maintained by two other circles | Java program to check whether circle with given co - ordinates reside within the boundary of outer circle and inner circle ; function to check if given circle fit in boundary or not ; Distance from the center ; Checking the corners of circle ; driver program ; Radius of outer circle and inner circle respectively ; Co - ordinates and radius of the circle to be checked
import java . util . * ; class GFG { static void fitOrNotFit ( int R , int r , int x , int y , int rad ) { double val = Math . sqrt ( Math . pow ( x , 2 ) + Math . pow ( y , 2 ) ) ; if ( val + rad <= R && val - rad >= R - r ) System . out . println ( " Fits " ) ; else System . out . println ( " Doesn ' t ▁ Fit " ) ; } public static void main ( String [ ] args ) { int R = 8 , r = 4 ; int x = 5 , y = 3 , rad = 3 ; fitOrNotFit ( R , r , x , y , rad ) ; } }
Number of Triangles that can be formed given a set of lines in Euclidean Plane | Java program to find the number of triangles that can be formed using a set of lines in Euclidean Plane ; Double variables can ' t ▁ be ▁ checked ▁ precisely ▁ using ▁ ' == ' this function returns true if the double variables are equal ; This function returns the number of triangles for a given set of lines ; Slope array stores the slope of lines ; Slope array is sorted so that all lines with same slope come together ; After sorting slopes , count different slopes . k is index in count [ ] . ; Count of current slope ; Calculating sum1 ( Sum of all slopes ) sum1 = m1 + m2 + ... ; Calculating sum2 . sum2 = m1 * m2 + m2 * m3 + ... ; Needed for sum3 ; Calculating sum3 which gives the final answer m1 * m2 * m3 + m2 * m3 * m4 + ... ; Driver code ; Lines are stored as arrays of a , b and c for ' ax + by = c ' ; n is the number of lines
import java . util . * ; class GFG { static double EPSILON = 1.0842e-19 ; static boolean compareDoubles ( double A , double B ) { double diff = A - B ; return ( diff < EPSILON ) && ( - diff < EPSILON ) ; } static int numberOfTringles ( int [ ] a , int [ ] b , int [ ] c , int n ) { Vector < Double > slope = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) slope . add ( ( double ) ( a [ i ] * 1.0 ) / b [ i ] ) ; Collections . sort ( slope ) ; int [ ] count = new int [ n ] ; int k = 0 ; int this_count = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( compareDoubles ( ( double ) slope . get ( i ) , ( double ) slope . get ( i - 1 ) ) ) this_count ++ ; else { count [ k ++ ] = this_count ; this_count = 1 ; } } count [ k ++ ] = this_count ; int sum1 = 0 ; for ( int i = 0 ; i < k ; i ++ ) sum1 += count [ i ] ; int sum2 = 0 ; int temp [ ] = new int [ n ] ; for ( int i = 0 ; i < k ; i ++ ) { temp [ i ] = count [ i ] * ( sum1 - count [ i ] ) ; sum2 += temp [ i ] ; } sum2 /= 2 ; int sum3 = 0 ; for ( int i = 0 ; i < k ; i ++ ) sum3 += count [ i ] * ( sum2 - temp [ i ] ) ; sum3 /= 3 ; return sum3 ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 4 } ; int b [ ] = { 2 , 4 , 5 , 5 } ; int c [ ] = { 5 , 7 , 8 , 6 } ; int n = a . length ; System . out . println ( " The ▁ number ▁ of ▁ triangles ▁ " + " that ▁ can ▁ be ▁ formed ▁ are : ▁ " + numberOfTringles ( a , b , c , n ) ) ; } }
Program to find line passing through 2 Points | Java Implementation to find the line passing through two points ; This pair is used to store the X and Y coordinate of a point respectively ; Function to find the line given two points ; Driver code
class GFG { static class Pair { int first , second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void lineFromPoints ( Pair P , Pair Q ) { int a = Q . second - P . second ; int b = P . first - Q . first ; int c = a * ( P . first ) + b * ( P . second ) ; if ( b < 0 ) { System . out . println ( " The ▁ line ▁ passing ▁ through ▁ points ▁ P ▁ and ▁ Q ▁ is : ▁ " + a + " x ▁ - ▁ " + b + " y ▁ = ▁ " + c ) ; } else { System . out . println ( " The ▁ line ▁ passing ▁ through ▁ points ▁ P ▁ and ▁ Q ▁ is : ▁ " + a + " x ▁ + ▁ " + b + " y ▁ = ▁ " + c ) ; } } public static void main ( String [ ] args ) { Pair P = new Pair ( 3 , 2 ) ; Pair Q = new Pair ( 2 , 6 ) ; lineFromPoints ( P , Q ) ; } }
Regular polygon using only 1 s in a binary numbered circle | Java program to find whether a regular polygon is possible in circle with 1 s as vertices ; method returns true if polygon is possible with ' midpoints ' number of midpoints ; loop for getting first vertex of polygon ; loop over array values at ' midpoints ' distance ; and ( & ) all those values , if even one of them is 0 , val will be 0 ; if val is still 1 and ( N / midpoints ) or ( number of vertices ) are more than two ( for a polygon minimum ) print result and return true ; method prints sides in the polygon or print not possible in case of no possible polygon ; limit for iterating over divisors ; If i divides N then i and ( N / i ) will be divisors ; check polygon for both divisors ; Driver method
class Test { static boolean checkPolygonWithMidpoints ( int arr [ ] , int N , int midpoints ) { for ( int j = 0 ; j < midpoints ; j ++ ) { int val = 1 ; for ( int k = j ; k < N ; k += midpoints ) { val &= arr [ k ] ; } if ( val != 0 && N / midpoints > 2 ) { System . out . println ( " Polygon ▁ possible ▁ with ▁ side ▁ length ▁ " + N / midpoints ) ; return true ; } } return false ; } static void isPolygonPossible ( int arr [ ] , int N ) { int limit = ( int ) Math . sqrt ( N ) ; for ( int i = 1 ; i <= limit ; i ++ ) { if ( N % i == 0 ) { if ( checkPolygonWithMidpoints ( arr , N , i ) || checkPolygonWithMidpoints ( arr , N , ( N / i ) ) ) return ; } } System . out . println ( " Not ▁ possible " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 , 1 } ; isPolygonPossible ( arr , arr . length ) ; } }
Maximum height when coins are arranged in a triangle | Java program to find maximum height of arranged coin triangle ; Returns the square root of n . Note that the function ; We are using n itself as initial approximation . This can definitely be improved ; e decides the accuracy level ; Method to find maximum height of arrangement of coins ; calculating portion inside the square root ; Driver code
class GFG { static float squareRoot ( float n ) { float x = n ; float y = 1 ; float e = 0.000001f ; while ( x - y > e ) { x = ( x + y ) / 2 ; y = n / x ; } return x ; } static int findMaximumHeight ( int N ) { int n = 1 + 8 * N ; int maxH = ( int ) ( - 1 + squareRoot ( n ) ) / 2 ; return maxH ; } public static void main ( String [ ] args ) { int N = 12 ; System . out . print ( findMaximumHeight ( N ) ) ; } }
Number of Integral Points between Two Points | Java code to find the number of integral points lying on the line joining the two given points ; Class to represent an Integral point on XY plane . ; Utility function to find GCD of two numbers GCD of a and b ; Finds the no . of Integral points between two given points . ; If line joining p and q is parallel to x axis , then count is difference of y values ; If line joining p and q is parallel to y axis , then count is difference of x values ; Driver program to test above
class GFG { static class Point { int x , y ; Point ( int a , int b ) { this . x = a ; this . y = b ; } } ; static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int getCount ( Point p , Point q ) { if ( p . x == q . x ) return Math . abs ( p . y - q . y ) - 1 ; if ( p . y == q . y ) return Math . abs ( p . x - q . x ) - 1 ; return gcd ( Math . abs ( p . x - q . x ) , Math . abs ( p . y - q . y ) ) - 1 ; } public static void main ( String [ ] args ) { Point p = new Point ( 1 , 9 ) ; Point q = new Point ( 8 , 16 ) ; System . out . println ( " The ▁ number ▁ of ▁ integral ▁ points ▁ between ▁ " + " ( " + p . x + " , ▁ " + p . y + " ) ▁ and ▁ ( " + q . x + " , ▁ " + q . y + " ) ▁ is ▁ " + getCount ( p , q ) ) ; } }
How to check if given four points form a square | A Java program to check if four given points form a square or not . ; Structure of a point in 2D space ; A utility function to find square of distance from point ' p ' to point ' q ' ; This function returns true if ( p1 , p2 , p3 , p4 ) form a square , otherwise false ; int d2 = distSq ( p1 , p2 ) ; from p1 to p2 int d3 = distSq ( p1 , p3 ) ; from p1 to p3 int d4 = distSq ( p1 , p4 ) ; from p1 to p4 ; If lengths if ( p1 , p2 ) and ( p1 , p3 ) are same , then following conditions must met to form a square . 1 ) Square of length of ( p1 , p4 ) is same as twice the square of ( p1 , p2 ) 2 ) Square of length of ( p2 , p3 ) is same as twice the square of ( p2 , p4 ) ; The below two cases are similar to above case ; Driver code
class GFG { static class Point { int x , y ; public Point ( int x , int y ) { this . x = x ; this . y = y ; } } ; static int distSq ( Point p , Point q ) { return ( p . x - q . x ) * ( p . x - q . x ) + ( p . y - q . y ) * ( p . y - q . y ) ; } static boolean isSquare ( Point p1 , Point p2 , Point p3 , Point p4 ) { if ( d2 == 0 d3 == 0 d4 == 0 ) return false ; if ( d2 == d3 && 2 * d2 == d4 && 2 * distSq ( p2 , p4 ) == distSq ( p2 , p3 ) ) { return true ; } if ( d3 == d4 && 2 * d3 == d2 && 2 * distSq ( p3 , p2 ) == distSq ( p3 , p4 ) ) { return true ; } if ( d2 == d4 && 2 * d2 == d3 && 2 * distSq ( p2 , p3 ) == distSq ( p2 , p4 ) ) { return true ; } return false ; } public static void main ( String [ ] args ) { Point p1 = new Point ( 20 , 10 ) , p2 = new Point ( 10 , 20 ) , p3 = new Point ( 20 , 20 ) , p4 = new Point ( 10 , 10 ) ; System . out . println ( isSquare ( p1 , p2 , p3 , p4 ) == true ? " Yes " : " No " ) ; } }
Count triplets such that product of two numbers added with third number is N | Java program for the above approach ; Function to find the divisors of the number ( N - i ) ; Stores the resultant count of divisors of ( N - i ) ; Iterate over range [ 1 , sqrt ( N ) ] ; Return the total divisors ; Function to find the number of triplets such that A * B - C = N ; Loop to fix the value of C ; Adding the number of divisors in count ; Return count of triplets ; Driver Code
import java . io . * ; class GFG { static int countDivisors ( int n ) { int divisors = 0 ; int i ; for ( i = 1 ; i * i < n ; i ++ ) { if ( n % i == 0 ) { divisors ++ ; } } if ( i - ( n / i ) == 1 ) { i -- ; } for ( ; i >= 1 ; i -- ) { if ( n % i == 0 ) { divisors ++ ; } } return divisors ; } static int possibleTriplets ( int N ) { int count = 0 ; for ( int i = 1 ; i < N ; i ++ ) { count += countDivisors ( N - i ) ; } return count ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . println ( possibleTriplets ( N ) ) ; } }
Maximize count of planes that can be stopped per second with help of given initial position and speed | Java program for the above approach ; Function to find maximum number of planes that can be stopped from landing ; Stores the times needed for landing for each plane ; Iterate over the arrays ; Stores the time needed for landing of current plane ; Update the value of t ; Append the t in set St ; Return the answer ; Driver Code
import java . util . * ; class GFG { static int maxPlanes ( int [ ] A , int [ ] B ) { Set < Integer > St = new HashSet < > ( ) ; for ( int i = 0 ; i < A . length ; i ++ ) { int t = ( A [ i ] % B [ i ] > 0 ) ? 1 : 0 ; t += ( A [ i ] / B [ i ] ) + t ; St . add ( t ) ; } return St . size ( ) ; } public static void main ( String [ ] args ) { int [ ] A = { 1 , 3 , 5 , 4 , 8 } ; int [ ] B = { 1 , 2 , 2 , 1 , 2 } ; System . out . println ( maxPlanes ( A , B ) ) ; } }
Find the player who will win by choosing a number in range [ 1 , K ] with sum total N | Java program for the above approach ; Function to predict the winner ; Driver Code ; Given Input ; Function call
import java . io . * ; class GFG { static void predictTheWinner ( int K , int N ) { if ( N % ( K + 1 ) == 0 ) System . out . println ( " Bob " ) ; else System . out . println ( " Alice " ) ; } public static void main ( String [ ] args ) { int K = 7 , N = 50 ; predictTheWinner ( K , N ) ; } }
Maximize the rightmost element of an array in k operations in Linear Time | Java program for the above approach ; Function to calculate maximum value of Rightmost element ; Initializing ans to store Maximum valued rightmost element ; Calculating maximum value of Rightmost element ; returning rightmost element ; Driver Code ; Given Input ; Function Call
import java . io . * ; class GFG { static int maxRightmostElement ( int N , int k , int p , int arr [ ] ) { int ans = arr [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; i -- ) { int d = Math . min ( arr [ i ] / p , k / ( N - 1 - i ) ) ; k -= d * ( N - 1 - i ) ; ans += d * p ; } return ans ; } public static void main ( String [ ] args ) { int N = 4 , k = 5 , p = 2 ; int arr [ ] = { 3 , 8 , 1 , 4 } ; System . out . println ( maxRightmostElement ( N , k , p , arr ) ) ; } }
Minimize the maximum element in constructed Array with sum divisible by K | Java program for the above approach ; Function to find smallest maximum number in an array whose sum is divisible by K . ; Minimum possible sum possible for an array of size N such that its sum is divisible by K ; If sum is not divisible by N ; If sum is divisible by N ; Driver code
import java . io . * ; import java . util . * ; class GFG { static int smallestMaximum ( int N , int K ) { int sum = ( ( N + K - 1 ) / K ) * K ; if ( sum % N != 0 ) return ( sum / N ) + 1 ; else return sum / N ; } public static void main ( String args [ ] ) { int N = 4 ; int K = 3 ; System . out . println ( smallestMaximum ( N , K ) ) ; } }