codeparrot/xlcost-text-to-code huggingface dataset:
Given a number as a string , find the number of contiguous subsequences which recursively add up to 9 | Java program to count substrings with recursive sum equal to 9 ; To store result ; Consider every character as beginning of substring ; sum of digits in current substring ; One by one choose every character as an ending character ; Add current digit to sum , if sum becomes multiple of 5 then increment count . Let us do modular arithmetic to avoid overflow for big strings ; Driver Code
import java . io . * ; class GFG { static int count9s ( String number ) { int count = 0 ; int n = number . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int sum = number . charAt ( i ) - '0' ; if ( number . charAt ( i ) == '9' ) count ++ ; for ( int j = i + 1 ; j < n ; j ++ ) { sum = ( sum + number . charAt ( j ) - '0' ) % 9 ; if ( sum == 0 ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { System . out . println ( count9s ( "4189" ) ) ; System . out . println ( count9s ( "1809" ) ) ; } }
Generate n | Java program to generate n - bit Gray codes ; This function generates all n bit Gray codes and prints the generated codes ; base case ; ' arr ' will store all generated codes ; start with one - bit pattern ; Every iteration of this loop generates 2 * i codes from previously generated i codes . ; Enter the prviously generated codes again in arr [ ] in reverse order . Nor arr [ ] has double number of codes . ; append 0 to the first half ; append 1 to the second half ; print contents of arr [ ] ; Driver program to test above function
import java . util . * ; class GfG { static void generateGrayarr ( int n ) { if ( n <= 0 ) return ; ArrayList < String > arr = new ArrayList < String > ( ) ; arr . add ( "0" ) ; arr . add ( "1" ) ; int i , j ; for ( i = 2 ; i < ( 1 << n ) ; i = i << 1 ) { for ( j = i - 1 ; j >= 0 ; j -- ) arr . add ( arr . get ( j ) ) ; for ( j = 0 ; j < i ; j ++ ) arr . set ( j , "0" + arr . get ( j ) ) ; for ( j = i ; j < 2 * i ; j ++ ) arr . set ( j , "1" + arr . get ( j ) ) ; } for ( i = 0 ; i < arr . size ( ) ; i ++ ) System . out . println ( arr . get ( i ) ) ; } public static void main ( String [ ] args ) { generateGrayarr ( 3 ) ; } }
Divide a string in N equal parts | if ( start < str . length ( ) ) System . out . println ( ) ; to ignore final new line ; Driver code
import java . util . * ; class GFG { static void divide ( String str , int n ) { if ( str . length ( ) % n != 0 ) { System . out . print ( " Invalid ▁ Input : ▁ String ▁ size " ) ; System . out . print ( " ▁ is ▁ not ▁ divisible ▁ by ▁ n " ) ; return ; } int parts = str . length ( ) / n ; int start = 0 ; int t = parts ; while ( start < str . length ( ) ) { String temp = new String ( str ) ; System . out . print ( temp . substring ( start , parts ) + "NEW_LINE"); start = parts ; parts += t ; } } public static void main ( String [ ] args ) { String str = " a _ simple _ divide _ String _ quest " ; divide ( str , 4 ) ; } }
Least count of words required to construct a target String | Java program of the above approach ; countMap [ ] [ ] to store count of characters ; Function to find the minimum number of stickers ; Base Case ; Fill the countMap Array ; Recursive function call to get minimum number of stickers ; Function to get minimum number of stickers for a particular state ; If an optimal solution is already there , return ; If Target has been constructed update cnt and return ; Update charAvailable [ ] ; Recursively function call for ( pos + 1 ) ; Update charAvailable [ ] ; Update charAvailable [ ] ; Recursive Call ; Update charAvailable [ ] ; Driver Code ; Given Input ; Function Call ; Print the result
import java . io . * ; import java . util . * ; class Sol { int [ ] [ ] countMap ; int cnt = Integer . MAX_VALUE ; public int minStickers ( String [ ] stickers , String target ) { if ( target == null ) return - 1 ; if ( target . length ( ) == 0 ) return 0 ; if ( stickers == null stickers . length == 0 ) return - 1 ; int m = stickers . length ; countMap = new int [ m ] [ 26 ] ; for ( int i = 0 ; i < stickers . length ; i ++ ) { String s = stickers [ i ] ; for ( char c : s . toCharArray ( ) ) { countMap [ i ] ++ ; } } count ( 0 , 0 , new int [ 26 ] , target , stickers ) ; return cnt == Integer . MAX_VALUE ? - 1 : cnt ; } private void count ( int curCnt , int pos , int [ ] charAvailable , String target , String [ ] stickers ) { if ( curCnt >= cnt ) return ; int m = stickers . length ; int n = target . length ( ) ; if ( pos == n ) { cnt = Math . min ( cnt , curCnt ) ; return ; } char c = target . charAt ( pos ) ; if ( charAvailable > 0 ) { charAvailable -- ; count ( curCnt , pos + 1 , charAvailable , target , stickers ) ; charAvailable ++ ; } else { for ( int i = 0 ; i < m ; i ++ ) { if ( countMap [ i ] == 0 ) continue ; for ( int j = 0 ; j < 26 ; j ++ ) { charAvailable [ j ] += countMap [ i ] [ j ] ; } count ( curCnt + 1 , pos , charAvailable , target , stickers ) ; for ( int j = 0 ; j < 26 ; j ++ ) { charAvailable [ j ] -= countMap [ i ] [ j ] ; } } } } } class GFG { public static void main ( String [ ] args ) { Sol st = new Sol ( ) ; String [ ] str = { " with " , " example " , " science " } ; String target = " thehat " ; int Result = st . minStickers ( str , target ) ; System . out . println ( Result ) ; } }
Rat in a Maze Problem when movement in all possible directions is allowed | Java implementation of the above approach ; Vector to store all the possible paths ; Function returns true if the move taken is valid else it will return false . ; Function to print all the possible paths from ( 0 , 0 ) to ( n - 1 , n - 1 ) . ; This will check the initial point ( i . e . ( 0 , 0 ) ) to start the paths . ; If reach the last cell ( n - 1 , n - 1 ) then store the path and return ; Mark the cell as visited ; Check if downward move is valid ; Check if the left move is valid ; Check if the right move is valid ; Check if the upper move is valid ; Mark the cell as unvisited for other possible paths ; Function to store and print all the valid paths ; Call the utility function to find the valid paths ; Print all possible paths ; Driver code
import java . util . * ; class GFG { static Vector < String > possiblePaths = new Vector < > ( ) ; static String path = " " ; static final int MAX = 5 ; static boolean isSafe ( int row , int col , int m [ ] [ ] , int n , boolean visited [ ] [ ] ) { if ( row == - 1 row == n col == - 1 col == n visited [ row ] [ col ] m [ row ] [ col ] == 0 ) return false ; return true ; } static void printPathUtil ( int row , int col , int m [ ] [ ] , int n , boolean visited [ ] [ ] ) { if ( row == - 1 row == n col == - 1 col == n visited [ row ] [ col ] m [ row ] [ col ] == 0 ) return ; if ( row == n - 1 && col == n - 1 ) { possiblePaths . add ( path ) ; return ; } visited [ row ] [ col ] = true ; if ( isSafe ( row + 1 , col , m , n , visited ) ) { path += ' D ' ; printPathUtil ( row + 1 , col , m , n , visited ) ; path = path . substring ( 0 , path . length ( ) - 1 ) ; } if ( isSafe ( row , col - 1 , m , n , visited ) ) { path += ' L ' ; printPathUtil ( row , col - 1 , m , n , visited ) ; path = path . substring ( 0 , path . length ( ) - 1 ) ; } if ( isSafe ( row , col + 1 , m , n , visited ) ) { path += ' R ' ; printPathUtil ( row , col + 1 , m , n , visited ) ; path = path . substring ( 0 , path . length ( ) - 1 ) ; } if ( isSafe ( row - 1 , col , m , n , visited ) ) { path += ' U ' ; printPathUtil ( row - 1 , col , m , n , visited ) ; path = path . substring ( 0 , path . length ( ) - 1 ) ; } visited [ row ] [ col ] = false ; } static void printPath ( int m [ ] [ ] , int n ) { boolean [ ] [ ] visited = new boolean [ n ] [ MAX ] ; printPathUtil ( 0 , 0 , m , n , visited ) ; for ( int i = 0 ; i < possiblePaths . size ( ) ; i ++ ) System . out . print ( possiblePaths . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int m [ ] [ ] = { { 1 , 0 , 0 , 0 , 0 } , { 1 , 1 , 1 , 1 , 1 } , { 1 , 1 , 1 , 0 , 1 } , { 0 , 0 , 0 , 0 , 1 } , { 0 , 0 , 0 , 0 , 1 } } ; int n = m . length ; printPath ( m , n ) ; } }
N Queen in O ( n ) space | Java code to for n Queen placement ; Function to check queens placement ; Helper Function to check if queen can be placed ; Function to display placed queen ; Driver Code
class GfG { static void breakLine ( ) { System . out . print ( " -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - "); } static int MAX = 10 ; static int arr [ ] = new int [ MAX ] , no ; static void nQueens ( int k , int n ) { for ( int i = 1 ; i <= n ; i ++ ) { if ( canPlace ( k , i ) ) { arr [ k ] = i ; if ( k == n ) { display ( n ) ; } else { nQueens ( k + 1 , n ) ; } } } } static boolean canPlace ( int k , int i ) { for ( int j = 1 ; j <= k - 1 ; j ++ ) { if ( arr [ j ] == i || ( Math . abs ( arr [ j ] - i ) == Math . abs ( j - k ) ) ) { return false ; } } return true ; } static void display ( int n ) { breakLine ( ) ; System . out . print ( " Arrangement ▁ No . ▁ " + ++ no ) ; breakLine ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( arr [ i ] != j ) { System . out . print ( " TABSYMBOL _ " ) ; } else { System . out . print ( " TABSYMBOL Q " ) ; } } System . out . println ( " " ) ; } breakLine ( ) ; } public static void main ( String [ ] args ) { int n = 4 ; nQueens ( 1 , n ) ; } }
Numbers that are bitwise AND of at least one non | Java implementation of the approach ; Set to store all possible AND values . ; Starting index of the sub - array . ; Ending index of the sub - array . ; AND value is added to the set . ; The set contains all possible AND values .
import java . util . HashSet ; class CP { public static void main ( String [ ] args ) { int [ ] A = { 11 , 15 , 7 , 19 } ; int N = A . length ; HashSet < Integer > set = new HashSet < > ( ) ; int i , j , res ; for ( i = 0 ; i < N ; ++ i ) for ( j = i , res = Integer . MAX_VALUE ; j < N ; ++ j ) { res &= A [ j ] ; set . add ( res ) ; } System . out . println ( set ) ; } }
Sudo Placement | Range Queries | Java program to find the numbers having atmost 3 set bits within a given range ; Class to store the L and R range of a query ; It returns index of first element which is greater than searched value If searched element is bigger than any array element function returns first index after last element . ; Set of Numbers having at most 3 set bits arranged in non - descending order ; 0 Set bits ; Iterate over all possible combinations of i , j , k for 60 bits . And add all the numbers with 0 , 1 or 2 set bits into the set allNum . ; For one set bit , check if i , j , k are equal if yes , then set that bit and add it to the set ; For two set bits , two of the three variable i , j , k will be equal and the third will not be . Set both the bits where two variabls are equal and the bit which is not equal , and add it to the set ; Setting all the 3 bits ; Adding all the numbers to an array list so that it can be sorted ; Answer queries by applying binary search ; Swap L and R if R is smaller than L
import java . util . * ; import java . io . * ; public class RangeQueries { static class Query { long L ; long R ; } public static int upperBound ( ArrayList < Long > validNumbers , Long value ) { int low = 0 ; int high = validNumbers . size ( ) - 1 ; while ( low < high ) { int mid = ( low + high ) / 2 ; if ( value >= validNumbers . get ( mid ) ) { low = mid + 1 ; } else { high = mid ; } } return low ; } public static void answerQueries ( ArrayList < Query > queries ) { Set < Long > allNum = new HashSet < > ( ) ; allNum . add ( 0L ) ; for ( int i = 0 ; i <= 60 ; i ++ ) { for ( int j = 0 ; j <= 60 ; j ++ ) { for ( int k = 0 ; k <= 60 ; k ++ ) { if ( i == j && j == k ) { allNum . add ( 1L << i ) ; } else if ( i == j && j != k ) { long toAdd = ( 1L << i ) + ( 1L << k ) ; allNum . add ( toAdd ) ; } else if ( i == k && k != j ) { long toAdd = ( 1L << i ) + ( 1L << j ) ; allNum . add ( toAdd ) ; } else if ( j == k && k != i ) { long toAdd = ( 1L << j ) + ( 1L << i ) ; allNum . add ( toAdd ) ; } else { long toAdd = ( 1L << i ) + ( 1L << j ) + ( 1L << k ) ; allNum . add ( toAdd ) ; } } } } ArrayList < Long > validNumbers = new ArrayList < > ( ) ; for ( Long num : allNum ) { validNumbers . add ( num ) ; } Collections . sort ( validNumbers ) ; for ( int i = 0 ; i < queries . size ( ) ; i ++ ) { long L = queries . get ( i ) . L ; long R = queries . get ( i ) . R ; if ( R < L ) { long temp = L ; L = R ; R = temp ; } if ( L == 0 ) { int indxOfLastNum = upperBound ( validNumbers , R ) ; System . out . println ( indxOfLastNum + 1 ) ; } else { int indxOfFirstNum = upperBound ( validNumbers , L ) ; int indxOfLastNum = upperBound ( validNumbers , R ) ; System . out . println ( ( indxOfLastNum - indxOfFirstNum + 1 ) ) ; } } } public static void main ( String [ ] args ) { int Q = 2 ; ArrayList < Query > queries = new ArrayList < > ( ) ; Query q1 = new Query ( ) ; q1 . L = 3 ; q1 . R = 7 ; Query q2 = new Query ( ) ; q2 . L = 10 ; q2 . R = 16 ; queries . add ( q1 ) ; queries . add ( q2 ) ; answerQueries ( queries ) ; } }
Collect all coins in minimum number of steps | Java Code to Collect all coins in minimum number of steps ; recursive method to collect coins from height array l to r , with height h already collected ; if l is more than r , no steps needed ; loop over heights to get minimum height index ; choose minimum from , 1 ) collecting coins using all vertical lines ( total r - l ) 2 ) collecting coins using lower horizontal lines and recursively on left and right segments ; method returns minimum number of step to collect coin from stack , with height in height [ ] array ; Driver program to test above function
import java . util . * ; class GFG { public static int minStepsRecur ( int height [ ] , int l , int r , int h ) { if ( l >= r ) return 0 ; int m = l ; for ( int i = l ; i < r ; i ++ ) if ( height [ i ] < height [ m ] ) m = i ; return Math . min ( r - l , minStepsRecur ( height , l , m , height [ m ] ) + minStepsRecur ( height , m + 1 , r , height [ m ] ) + height [ m ] - h ) ; } public static int minSteps ( int height [ ] , int N ) { return minStepsRecur ( height , 0 , N , 0 ) ; } public static void main ( String [ ] args ) { int height [ ] = { 2 , 1 , 2 , 5 , 1 } ; int N = height . length ; System . out . println ( minSteps ( height , N ) ) ; } }
Count of Unique Direct Path Between N Points On a Plane | Java program for the above approach ; Function to count the total number of direct paths ; Driver Code
import java . io . * ; class GFG { static int countDirectPath ( int N ) { return N + ( N * ( N - 3 ) ) / 2 ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . print ( countDirectPath ( N ) ) ; } }
Find the coordinate that does not belong to any square | Java program for above approach ; Function to find the point that is not a part of the side of a square ; Traverse each pair of coordinates ; Minimize x - coordinate in all the points except current point ; Maximize x - coordinate in all the points except the current point ; Minimize y - coordinate in all the points except current point ; Maximize y - coordinate in all the points except current point ; If x - coordinate matches with other same line ; If y coordinate matches with other same line ; Check if the condition for square exists or not ; Print the output ; Driver Code
import java . util . ArrayList ; class GFG { static class pair < T , V > { T fi ; V se ; pair ( T a , V b ) { this . fi = a ; this . se = b ; } } static void findPoint ( int n , ArrayList < pair < Integer , Integer > > p ) { for ( int i = 0 ; i < n * 4 + 1 ; ++ i ) { int x1 = ( int ) 2e9 , x2 = ( int ) - 2e9 ; int y1 = ( int ) 2e9 , y2 = ( int ) - 2e9 ; for ( int j = 0 ; j < n * 4 + 1 ; ++ j ) if ( i != j ) { x1 = Math . min ( x1 , p . get ( j ) . fi ) ; x2 = Math . max ( x2 , p . get ( j ) . fi ) ; y1 = Math . min ( y1 , p . get ( j ) . se ) ; y2 = Math . max ( y2 , p . get ( j ) . se ) ; } boolean ok = true ; int c1 = 0 , c2 = 0 ; int c3 = 0 , c4 = 0 ; for ( int j = 1 ; j < n * 4 + 1 ; ++ j ) if ( i != j ) { if ( ( p . get ( j ) . fi == x1 || p . get ( j ) . fi == x2 ) || ( ( p . get ( j ) . se == y1 || p . get ( j ) . se == y2 ) ) ) { if ( p . get ( j ) . fi == x1 ) ++ c1 ; if ( p . get ( j ) . fi == x2 ) ++ c2 ; if ( p . get ( j ) . se == y1 ) ++ c3 ; if ( p . get ( j ) . se == y2 ) ++ c4 ; } else ok = false ; } if ( ok && c1 >= n && c2 >= n && c3 >= n && c4 >= n && x2 - x1 == y2 - y1 ) { System . out . println ( p . get ( i ) . fi + " ▁ " + p . get ( i ) . se ) ; } } } public static void main ( String [ ] args ) { int N = 2 ; ArrayList < pair < Integer , Integer > > arr = new ArrayList < > ( ) ; arr . add ( new pair < Integer , Integer > ( 0 , 0 ) ) ; arr . add ( new pair < Integer , Integer > ( 0 , 1 ) ) ; arr . add ( new pair < Integer , Integer > ( 0 , 2 ) ) ; arr . add ( new pair < Integer , Integer > ( 1 , 0 ) ) ; arr . add ( new pair < Integer , Integer > ( 1 , 1 ) ) ; arr . add ( new pair < Integer , Integer > ( 1 , 2 ) ) ; arr . add ( new pair < Integer , Integer > ( 2 , 0 ) ) ; arr . add ( new pair < Integer , Integer > ( 2 , 1 ) ) ; arr . add ( new pair < Integer , Integer > ( 2 , 2 ) ) ; findPoint ( N , arr ) ; } }
Triacontagon Number | Java program for above approach ; Finding the nth triacontagonal number ; Driver code
import java . io . * ; import java . util . * ; class GFG { static int triacontagonalNum ( int n ) { return ( 28 * n * n - 26 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( "3rd ▁ triacontagonal ▁ Number ▁ is ▁ = ▁ " + triacontagonalNum ( n ) ) ; } }
Hexacontagon Number | Java program for above approach ; Finding the nth hexacontagon number ; Driver code
class GFG { public static int hexacontagonNum ( int n ) { return ( 58 * n * n - 56 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( "3rd ▁ hexacontagon ▁ Number ▁ is ▁ = ▁ " + hexacontagonNum ( n ) ) ; } }
Enneacontagon Number | Java program for above approach ; Finding the nth enneacontagon number ; Driver code
class GFG { public static int enneacontagonNum ( int n ) { return ( 88 * n * n - 86 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( "3rd ▁ enneacontagon ▁ Number ▁ is ▁ = ▁ " + enneacontagonNum ( n ) ) ; } }
Triacontakaidigon Number | Java program for above approach ; Finding the nth triacontakaidigon number ; Driver code
class GFG { public static int triacontakaidigonNum ( int n ) { return ( 30 * n * n - 28 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( "3rd ▁ triacontakaidigon ▁ Number ▁ is ▁ = ▁ " + triacontakaidigonNum ( n ) ) ; } }
Icosihexagonal Number | Java program for above approach ; Finding the nth icosihexagonal number ; Driver code
class GFG { public static int IcosihexagonalNum ( int n ) { return ( 24 * n * n - 22 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( "3rd ▁ Icosihexagonal ▁ Number ▁ is ▁ = ▁ " + IcosihexagonalNum ( n ) ) ; } }
Icosikaioctagon or Icosioctagon Number | Java program for above approach ; Finding the nth icosikaioctagonal number ; Driver code
class GFG { public static int icosikaioctagonalNum ( int n ) { return ( 26 * n * n - 24 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( "3rd ▁ icosikaioctagonal ▁ Number ▁ is ▁ = ▁ " + icosikaioctagonalNum ( n ) ) ; } }
Octacontagon Number | Java program for above approach ; Finding the nth octacontagon Number ; Driver Code
import java . util . * ; class GFG { static int octacontagonNum ( int n ) { return ( 78 * n * n - 76 * n ) / 2 ; } public static void main ( String args [ ] ) { int n = 3 ; System . out . print ( "3rd ▁ octacontagon ▁ Number ▁ is ▁ = ▁ " + octacontagonNum ( n ) ) ; } }
Hectagon Number | Java program for above approach ; Finding the nth hectagon Number ; Driver Code
import java . util . * ; class GFG { static int hectagonNum ( int n ) { return ( 98 * n * n - 96 * n ) / 2 ; } public static void main ( String args [ ] ) { int n = 3 ; System . out . print ( "3rd ▁ hectagon ▁ Number ▁ is ▁ = ▁ " + hectagonNum ( n ) ) ; } }
Tetracontagon Number | Java program for above approach ; Finding the nth tetracontagon number ; Driver code
import java . util . * ; class GFG { static int tetracontagonNum ( int n ) { return ( 38 * n * n - 36 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( "3rd ▁ tetracontagon ▁ Number ▁ is ▁ = ▁ " + tetracontagonNum ( n ) ) ; } }
Perimeter of an Ellipse | Java program to find perimeter of an Ellipse . ; Function to find perimeter of an ellipse . ; formula to find the Perimeter of an Ellipse . ; Display the result ; Driver code
import java . lang . Math ; class GFG1 { static void Perimeter ( double a , double b ) { double Perimeter ; Perimeter = ( double ) 2 * 3.14 * Math . sqrt ( ( a * a + b * b ) / ( 2 * 1.0 ) ) ; System . out . println ( " Perimeter : ▁ " + Perimeter ) ; } public static void main ( String [ ] args ) { double a = 3 , b = 2 ; Perimeter ( a , b ) ; } }
Biggest Reuleaux Triangle within A Square | Java Program to find the area of the biggest Reuleaux triangle that can be inscribed within a square ; Function to find the Area of the Reuleaux triangle ; Side cannot be negative ; Area of the Reuleaux triangle ; Driver code
import java . lang . Math ; class cfg { static double ReuleauxArea ( double a ) { if ( a < 0 ) return - 1 ; double A = 0.70477 * Math . pow ( a , 2 ) ; return A ; } public static void main ( String [ ] args ) { double a = 6 ; System . out . println ( ReuleauxArea ( a ) ) ; }
Largest hexagon that can be inscribed within a square | Java Program to find the biggest hexagon which can be inscribed within the given square ; Function to return the side of the hexagon ; Side cannot be negative ; Side of the hexagon ; Driver code
import java . io . * ; class GFG { static double hexagonside ( double a ) { if ( a < 0 ) return - 1 ; double x = ( 0.5176 * a ) ; return x ; } public static void main ( String [ ] args ) { double a = 6 ; System . out . println ( hexagonside ( a ) ) ; } }
Largest hexagon that can be inscribed within an equilateral triangle | Java program to find the side of the largest hexagon which can be inscribed within an equilateral triangle ; Function to find the side of the hexagon ; Side cannot be negative ; Side of the hexagon ; Driver code
class CLG { static float hexagonside ( float a ) { if ( a < 0 ) return - 1 ; float x = a / 3 ; return x ; } public static void main ( String [ ] args ) { float a = 6 ; System . out . println ( hexagonside ( a ) ) ; } }
Find middle point segment from given segment lengths | Java implementation of the approach ; Function that returns the segment for the middle point ; the middle point ; stores the segment index ; increment sum by length of the segment ; if the middle is in between two segments ; if sum is greater than middle point ; Driver code
class GFG { static int findSegment ( int n , int m , int [ ] segment_length ) { double meet_point = ( 1.0 * n ) / 2.0 ; int sum = 0 ; int segment_number = 0 ; for ( int i = 0 ; i < m ; i ++ ) { sum += segment_length [ i ] ; if ( ( double ) sum == meet_point ) { segment_number = - 1 ; break ; } if ( sum > meet_point ) { segment_number = i + 1 ; break ; } } return segment_number ; } public static void main ( String [ ] args ) { int n = 13 ; int m = 3 ; int [ ] segment_length = new int [ ] { 3 , 2 , 8 } ; int ans = findSegment ( n , m , segment_length ) ; System . out . println ( ans ) ; } }
Maximum points of intersection n lines | Java program to find maximum intersecting points ; nC2 = ( n ) * ( n - 1 ) / 2 ; ; Driver code ; n is number of line
public class GFG { static long countMaxIntersect ( long n ) { return ( n ) * ( n - 1 ) / 2 ; } public static void main ( String args [ ] ) { long n = 8 ; System . out . println ( countMaxIntersect ( n ) ) ; } }