codeparrot/xlcost-text-to-code huggingface dataset:
Karp 's minimum mean (or average) weight cycle algorithm | Java program to find minimum average weight of a cycle in connected and directed graph . ; a struct to represent edges ; vector to store edges @ SuppressWarnings ( " unchecked " ) ; calculates the shortest path ; initializing all distances as - 1 ; shortest distance from first vertex to in tself consisting of 0 edges ; filling up the dp table ; Returns minimum value of average weight of a cycle in graph . ; array to store the avg values ; Compute average values for all vertices using weights of shortest paths store in dp . ; Find minimum value in avg [ ] ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int V = 4 ; static class Edge { int from , weight ; Edge ( int from , int weight ) { this . from = from ; this . weight = weight ; } } static Vector < Edge > [ ] edges = new Vector [ V ] ; static { for ( int i = 0 ; i < V ; i ++ ) edges [ i ] = new Vector < > ( ) ; } static void addedge ( int u , int v , int w ) { edges [ v ] . add ( new Edge ( u , w ) ) ; } static void shortestpath ( int [ ] [ ] dp ) { for ( int i = 0 ; i <= V ; i ++ ) for ( int j = 0 ; j < V ; j ++ ) dp [ i ] [ j ] = - 1 ; dp [ 0 ] [ 0 ] = 0 ; for ( int i = 1 ; i <= V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { for ( int k = 0 ; k < edges [ j ] . size ( ) ; k ++ ) { if ( dp [ i - 1 ] [ edges [ j ] . elementAt ( k ) . from ] != - 1 ) { int curr_wt = dp [ i - 1 ] [ edges [ j ] . elementAt ( k ) . from ] + edges [ j ] . elementAt ( k ) . weight ; if ( dp [ i ] [ j ] == - 1 ) dp [ i ] [ j ] = curr_wt ; else dp [ i ] [ j ] = Math . min ( dp [ i ] [ j ] , curr_wt ) ; } } } } } static double minAvgWeight ( ) { int [ ] [ ] dp = new int [ V + 1 ] [ V ] ; shortestpath ( dp ) ; double [ ] avg = new double [ V ] ; for ( int i = 0 ; i < V ; i ++ ) avg [ i ] = - 1 ; for ( int i = 0 ; i < V ; i ++ ) { if ( dp [ V ] [ i ] != - 1 ) { for ( int j = 0 ; j < V ; j ++ ) if ( dp [ j ] [ i ] != - 1 ) avg [ i ] = Math . max ( avg [ i ] , ( ( double ) dp [ V ] [ i ] - dp [ j ] [ i ] ) / ( V - j ) ) ; } } double result = avg [ 0 ] ; for ( int i = 0 ; i < V ; i ++ ) if ( avg [ i ] != - 1 && avg [ i ] < result ) result = avg [ i ] ; return result ; } public static void main ( String [ ] args ) { addedge ( 0 , 1 , 1 ) ; addedge ( 0 , 2 , 10 ) ; addedge ( 1 , 2 , 3 ) ; addedge ( 2 , 3 , 2 ) ; addedge ( 3 , 1 , 0 ) ; addedge ( 3 , 0 , 8 ) ; System . out . printf ( " % .5f " , minAvgWeight ( ) ) ; } }
Frequency of maximum occurring subsequence in given string | Java program for the above approach ; Function to find the frequency ; freq stores frequency of each english lowercase character ; dp [ i ] [ j ] stores the count of subsequence with ' a ' + i and ' a ' + j character ; Increment the count of subsequence j and s [ i ] ; Update the frequency array ; For 1 length subsequence ; For 2 length subsequence ; Return the final result ; Driver Code ; Given String str ; Function call
class GFG { static int findCount ( String s ) { int [ ] freq = new int [ 26 ] ; int [ ] [ ] dp = new int [ 26 ] [ 26 ] ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { for ( int j = 0 ; j < 26 ; j ++ ) { dp [ j ] [ s . charAt ( i ) - ' a ' ] += freq [ j ] ; } freq [ s . charAt ( i ) - ' a ' ] ++ ; } int ans = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) ans = Math . max ( freq [ i ] , ans ) ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) { ans = Math . max ( dp [ i ] [ j ] , ans ) ; } } return ans ; } public static void main ( String [ ] args ) { String str = " acbab " ; System . out . print ( findCount ( str ) ) ; } }
Create an array such that XOR of subarrays of length K is X | Java implementation to create an array in which the XOR of all elements of each contiguous sub - array of length K is X ; Function to construct the array ; Creating an array of size K , initialised with 0 ; Initialising the first element with the given XOR ; Driver code
class GFG { public static void constructArray ( int N , int K , int X ) { int [ ] ans = new int [ K ] ; ans [ 0 ] = X ; for ( int i = 0 ; i < N ; ++ i ) { System . out . print ( ans [ i % K ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 5 , K = 2 , X = 4 ; constructArray ( N , K , X ) ; } }
Two player game in which a player can remove all occurrences of a number | Java implementation for Two player game in which a player can remove all occurrences of a number ; Function that print whether player1 can wins or loses ; Storing the number of occurrence of elements in unordered map ; variable to check if the occurrence of repeated elements is >= 4 and multiple of 2 or not ; count elements which occur more than once ; Driver code
import java . util . * ; class GFG { public static void game ( int [ ] v , int n ) { HashMap < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! m . containsKey ( v [ i ] ) ) m . put ( v [ i ] , 1 ) ; else m . replace ( v [ i ] , m . get ( v [ i ] ) + 1 ) ; } int count = 0 ; int check = 0 ; for ( Map . Entry < Integer , Integer > i : m . entrySet ( ) ) { if ( i . getValue ( ) > 1 ) { if ( i . getValue ( ) >= 4 && i . getValue ( ) % 2 == 0 ) check ++ ; count ++ ; } } boolean flag ; if ( check % 2 != 0 ) flag = false ; if ( check % 2 != 0 ) System . out . println ( " Yes " ) ; else if ( n % 2 == 0 && count % 2 == 0 ) System . out . println ( " No " ) ; else System . out . println ( " Yes " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 2 , 3 , 3 , 5 } ; int size = arr . length ; game ( arr , size ) ; } }
Count of elements which form a loop in an Array according to given constraints | Java program to number of elements which form a cycle in an array ; Function to count number of elements forming a cycle ; Array to store parent node of traversal . ; Array to determine whether current node is already counted in the cycle . ; Initialize the arrays . ; Check if current node is already traversed or not . If node is not traversed yet then parent value will be - 1. ; Traverse the graph until an already visited node is not found . ; Check parent value to ensure a cycle is present . ; Count number of nodes in the cycle . ; Driver code
import java . util . * ; class GFG { static final int mod = 1000000007 ; static int solve ( int A [ ] , int n ) { int i , cnt = 0 , j ; int [ ] parent = new int [ n ] ; int [ ] vis = new int [ n ] ; Arrays . fill ( parent , - 1 ) ; Arrays . fill ( vis , 0 ) ; for ( i = 0 ; i < n ; i ++ ) { j = i ; if ( parent [ j ] == - 1 ) { while ( parent [ j ] == - 1 ) { parent [ j ] = i ; j = __gcd ( j , A [ j ] ) % n ; } if ( parent [ j ] == i ) { while ( vis [ j ] == 0 ) { vis [ j ] = 1 ; cnt ++ ; j = __gcd ( j , A [ j ] ) % n ; } } } } return cnt ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 1 , 6 , 2 } ; int n = A . length ; System . out . print ( solve ( A , n ) ) ; } }
Sum and Product of all even digit sum Nodes of a Singly Linked List | Java program for the above approach ; Node of Linked List ; Function to insert a node at the beginning of the singly Linked List ; Allocate new node ; Insert the data ; Link old list to the new node ; Move head to point the new node ; Function to find the digit sum for a number ; Return the sum ; Function to find the required sum and product ; Initialise the sum and product to 0 and 1 respectively ; Traverse the given linked list ; If current node has even digit sum then include it in resultant sum and product ; Find the sum and the product ; Print the final Sum and Product ; Driver Code ; Head of the linked list ; Create the linked list 15.16 . 8.6 .13 ; Function call
class GFG { static class Node { int data ; Node next ; } ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static int digitSum ( int num ) { int sum = 0 ; while ( num > 0 ) { sum += ( num % 10 ) ; num /= 10 ; } return sum ; } static void sumAndProduct ( Node head_ref ) { int prod = 1 ; int sum = 0 ; Node ptr = head_ref ; while ( ptr != null ) { if ( ( digitSum ( ptr . data ) % 2 != 1 ) ) { prod *= ptr . data ; sum += ptr . data ; } ptr = ptr . next ; } System . out . print ( " Sum ▁ = ▁ " + sum + "NEW_LINE"); System . out . print ( " Product ▁ = ▁ " + prod ) ; } public static void main ( String [ ] args ) { Node head = null ; head = push ( head , 13 ) ; head = push ( head , 6 ) ; head = push ( head , 8 ) ; head = push ( head , 16 ) ; head = push ( head , 15 ) ; sumAndProduct ( head ) ; } }
Shortest Palindromic Substring | Java program to find the shortest palindromic substring ; Function return the shortest palindromic substring ; One by one consider every character as center point of even and length palindromes ; Find the longest odd length palindrome with center point as i ; Find the even length palindrome with center points as i - 1 and i . ; Smallest substring which is not empty ; Driver code
import java . util . * ; import java . io . * ; class GFG { public static String ShortestPalindrome ( String s ) { int n = s . length ( ) ; Vector < String > v = new Vector < String > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int l = i ; int r = i ; String ans1 = " " ; String ans2 = " " ; while ( l >= 0 && r < n && s . charAt ( l ) == s . charAt ( r ) ) { ans1 += s . charAt ( l ) ; l -- ; r ++ ; } l = i - 1 ; r = i ; while ( l >= 0 && r < n && s . charAt ( l ) == s . charAt ( r ) ) { ans2 += s . charAt ( l ) ; l -- ; r ++ ; } v . add ( ans1 ) ; v . add ( ans2 ) ; } String ans = v . get ( 0 ) ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( v . get ( i ) != " " ) { if ( ans . charAt ( 0 ) >= v . get ( i ) . charAt ( 0 ) ) { ans = v . get ( i ) ; } } } return ans ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; System . out . println ( ShortestPalindrome ( s ) ) ; } }
Minimum increment or decrement operations required to make the array sorted | Java implementation of the approach ; Function to return the minimum number of given operations required to sort the array ; Number of elements in the array ; Smallest element in the array ; Largest element in the array ; dp ( i , j ) represents the minimum number of operations needed to make the array [ 0 . . i ] sorted in non - decreasing order given that ith element is j ; Fill the dp [ ] ] [ array for base cases ; Using results for the first ( i - 1 ) elements , calculate the result for the ith element ; If the ith element is j then we can have any value from small to j for the i - 1 th element We choose the one that requires the minimum operations ; If we made the ( n - 1 ) th element equal to j we required dp ( n - 1 , j ) operations We choose the minimum among all possible dp ( n - 1 , j ) where j goes from small to large ; Driver code
import java . util . * ; class GFG { static int getMinimumOps ( Vector < Integer > ar ) { int n = ar . size ( ) ; int small = Collections . min ( ar ) ; int large = Collections . max ( ar ) ; int [ ] [ ] dp = new int [ n ] [ large + 1 ] ; for ( int j = small ; j <= large ; j ++ ) { dp [ 0 ] [ j ] = Math . abs ( ar . get ( 0 ) - j ) ; } for ( int i = 1 ; i < n ; i ++ ) { int minimum = Integer . MAX_VALUE ; for ( int j = small ; j <= large ; j ++ ) { minimum = Math . min ( minimum , dp [ i - 1 ] [ j ] ) ; dp [ i ] [ j ] = minimum + Math . abs ( ar . get ( i ) - j ) ; } } int ans = Integer . MAX_VALUE ; for ( int j = small ; j <= large ; j ++ ) { ans = Math . min ( ans , dp [ n - 1 ] [ j ] ) ; } return ans ; } public static void main ( String [ ] args ) { Integer [ ] arr = { 1 , 2 , 1 , 4 , 3 } ; Vector < Integer > ar = new Vector < > ( Arrays . asList ( arr ) ) ; System . out . println ( getMinimumOps ( ar ) ) ; } }
Minimize the cost of partitioning an array into K groups | Java implementation of above approach ; Function to return the minimum number of operations needed to partition the array in k contiguous groups such that all elements of a given group are identical ; n is the size of the array ; dp ( i , j ) represents the minimum cost for partitioning the array [ 0. . i ] into j groups ; Base case , cost is 0 for parititoning the array [ 0. .0 ] into 1 group ; Fill dp ( i , j ) and the answer will be stored at dp ( n - 1 , k ) ; The maximum groups that the segment 0. . i can be divided in is represented by maxGroups ; Initialize dp ( i , j ) to infinity ; Divide segment 0. . i in 1 group ; map and freqOfMode are together used to keep track of the frequency of the most occurring element in [ 0. . i ] ; Change all the elements in the range 0. . i to the most frequent element in this range ; If the jth group is the segment from it . . i , we change all the elements in this range to this range 's most occurring element ; Number of elements we need to change in the jth group i . e . the range it . . i ; For all the possible sizes of the jth group that end at the ith element we pick the size that gives us the minimum cost for dp ( i , j ) elementsToChange is the cost of making all the elements in the jth group identical and we make use of dp ( it - 1 , j - 1 ) to find the overall minimal cost ; Return the minimum cost for partitioning array [ 0. . n - 1 ] into k groups which is stored at dp ( n - 1 , k ) ; Driver code
class GFG { static int getMinimumOps ( int ar [ ] , int k ) { int n = ar . length ; int dp [ ] [ ] = new int [ n ] [ k + 1 ] ; dp [ 0 ] [ 1 ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int maxGroups = Math . min ( k , i + 1 ) ; for ( int j = 1 ; j <= maxGroups ; j ++ ) { dp [ i ] [ j ] = Integer . MAX_VALUE ; if ( j == 1 ) { int freq [ ] = new int [ 100000 ] ; int freqOfMode = 0 ; for ( int it = 0 ; it <= i ; it ++ ) { freq [ ar [ it ] ] ++ ; int newElementFreq = freq [ ar [ it ] ] ; if ( newElementFreq > freqOfMode ) freqOfMode = newElementFreq ; } dp [ i ] [ 1 ] = ( i + 1 ) - freqOfMode ; } else { int freq [ ] = new int [ 100000 ] ; int freqOfMode = 0 ; for ( int it = i ; it >= j - 1 ; it -- ) { freq [ ar [ it ] ] ++ ; int newElementFreq = freq [ ar [ it ] ] ; if ( newElementFreq > freqOfMode ) freqOfMode = newElementFreq ; int elementsToChange = i - it + 1 ; elementsToChange -= freqOfMode ; dp [ i ] [ j ] = Math . min ( dp [ it - 1 ] [ j - 1 ] + elementsToChange , dp [ i ] [ j ] ) ; } } } } return dp [ n - 1 ] [ k ] ; } public static void main ( String args [ ] ) { int k = 3 ; int ar [ ] = { 3 , 1 , 3 , 3 , 2 , 1 , 8 , 5 } ; System . out . println ( getMinimumOps ( ar , k ) ) ; } }
Find numbers which are multiples of first array and factors of second array | Java implementation of the approach ; Function to return the LCM of two numbers ; Function to print the required numbers ; To store the lcm of array a [ ] elements and the gcd of array b [ ] elements ; Finding LCM of first array ; Finding GCD of second array ; No such element exists ; All the multiples of lcmA which are less than or equal to gcdB and evenly divide gcdB will satisfy the conditions ; Driver code
import java . util . * ; class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static int lcm ( int x , int y ) { int temp = ( x * y ) / __gcd ( x , y ) ; return temp ; } static void findNumbers ( int a [ ] , int n , int b [ ] , int m ) { int lcmA = 1 , gcdB = 0 ; for ( int i = 0 ; i < n ; i ++ ) lcmA = lcm ( lcmA , a [ i ] ) ; for ( int i = 0 ; i < m ; i ++ ) gcdB = __gcd ( gcdB , b [ i ] ) ; if ( gcdB % lcmA != 0 ) { System . out . print ( " - 1" ) ; return ; } int num = lcmA ; while ( num <= gcdB ) { if ( gcdB % num == 0 ) System . out . print ( num + " ▁ " ) ; num += lcmA ; } } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 2 , 4 } ; int b [ ] = { 16 , 32 , 64 } ; int n = a . length ; int m = b . length ; findNumbers ( a , n , b , m ) ; } }
Probability such that two subset contains same number of elements | Java implementation of the above approach ; Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of ; Iterative Function to calculate ( x ^ y ) in O ( log y ) ; Initialize result ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Change x to x ^ 2 ; Recursive function to return gcd of a and b ; Function to find probability ; Calculate total possible ways and favourable ways . ; Divide by gcd such that they become relatively coprime ; Driver code
class GFG { static int binomialCoeff ( int n , int k ) { int res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static int power ( int x , int y ) { int res = 1 ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = res * x ; y = y >> 1 ; x = x * x ; } return res ; } static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void FindProbability ( int n ) { int up = binomialCoeff ( 2 * n , n ) ; int down = power ( 2 , 2 * n ) ; int g = gcd ( up , down ) ; up /= g ; down /= g ; System . out . println ( up + " / " + down ) ; } public static void main ( String [ ] args ) { int N = 8 ; FindProbability ( N ) ; } }
How to learn Pattern printing easily ? | Java implementation of the approach
class GFG { public static void main ( String [ ] args ) { int N = 4 , i , j , min ; System . out . println ( " Value ▁ of ▁ N : ▁ " + N ) ; for ( i = 1 ; i <= N ; i ++ ) { for ( j = 1 ; j <= N ; j ++ ) { min = i < j ? i : j ; System . out . print ( N - min + 1 ) ; } for ( j = N - 1 ; j >= 1 ; j -- ) { min = i < j ? i : j ; System . out . print ( N - min + 1 ) ; } System . out . println ( ) ; } for ( i = N - 1 ; i >= 1 ; i -- ) { for ( j = 1 ; j <= N ; j ++ ) { min = i < j ? i : j ; System . out . print ( N - min + 1 ) ; } for ( j = N - 1 ; j >= 1 ; j -- ) { min = i < j ? i : j ; System . out . print ( N - min + 1 ) ; } System . out . println ( ) ; } } }
Largest perfect square number in an Array | Java program to find the largest perfect square number among n numbers ; Function to check if a number is perfect square number or not ; takes the sqrt of the number ; checks if it is a perfect square number ; Function to find the largest perfect square number in the array ; stores the maximum of all perfect square numbers ; Traverse all elements in the array ; store the maximum if current element is a perfect square ; Driver Code
import java . lang . Math ; import java . io . * ; class GFG { static boolean checkPerfectSquare ( double n ) { double d = Math . sqrt ( n ) ; if ( d * d == n ) return true ; return false ; } static int largestPerfectSquareNumber ( int a [ ] , double n ) { int maxi = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( checkPerfectSquare ( a [ i ] ) ) maxi = Math . max ( a [ i ] , maxi ) ; } return maxi ; } public static void main ( String [ ] args ) { int [ ] a = { 16 , 20 , 25 , 2 , 3 , 10 } ; double n = a . length ; System . out . println ( largestPerfectSquareNumber ( a , n ) ) ; } }
Maximum number of parallelograms that can be made using the given length of line segments | Function to find the maximum number of parallelograms can be made ; Finding the length of the frequency array ; Increasing the occurrence of each segment ; To store the count of parallelograms ; Counting parallelograms that can be made using 4 similar sides ; counting segments which have occurrence left >= 2 ; Adding parallelograms that can be made using 2 similar sides ; Driver code
import java . util . * ; class GFG { static void convert ( int n , int a [ ] ) { int z = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] > z ) z = a [ i ] ; } z = z + 1 ; int ff [ ] = new int [ z ] ; for ( int i = 0 ; i < n ; i ++ ) { ff [ a [ i ] ] += 1 ; } int cc = 0 ; for ( int i = 0 ; i < z ; i ++ ) { cc += ( ff [ i ] / 4 ) ; ff [ i ] = ff [ i ] % 4 ; } int vv = 0 ; for ( int i = 0 ; i < z ; i ++ ) { if ( ff [ i ] >= 2 ) vv += 1 ; } cc += ( vv / 2 ) ; System . out . println ( cc ) ; } public static void main ( String [ ] args ) { int n = 4 ; int a [ ] = { 1 , 2 , 1 , 2 } ; convert ( n , a ) ; } }
Count pairs ( i , j ) such that ( i + j ) is divisible by A and B both | Java implementation of above approach ; Function to find the LCM ; Function to count the pairs ; Driver code
import java . util . * ; public class ACE { static int gcd ( int a , int b ) { return b == 0 ? a : gcd ( b , a % b ) ; } static int find_LCM ( int x , int y ) { return ( x * y ) / gcd ( x , y ) ; } static int CountPairs ( int n , int m , int A , int B ) { int cnt = 0 ; int lcm = find_LCM ( A , B ) ; for ( int i = 1 ; i <= n ; i ++ ) cnt += ( m + ( i % lcm ) ) / lcm ; return cnt ; } public static void main ( String [ ] args ) { int n = 60 , m = 90 , A = 5 , B = 10 ; System . out . println ( CountPairs ( n , m , A , B ) ) ; } }
Sorting without comparison of elements | Java program to sort an array without comparison operator . ; Represents node of a doubly linked list ; Count of elements in given range ; Count frequencies of all elements ; Traverse through range . For every element , print it its count times . ; Driver Code
import java . util . * ; class Node { static void sortArr ( int arr [ ] , int n , int min , int max ) { int m = max - min + 1 ; int [ ] c = new int [ m ] ; for ( int i = 0 ; i < n ; i ++ ) { c [ arr [ i ] - min ] ++ ; } for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < c [ i ] ; j ++ ) { System . out . print ( ( i + min ) + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 10 , 1 , 4 , 4 , 100 , 0 } ; int min = 0 , max = 100 ; int n = arr . length ; sortArr ( arr , n , min , max ) ; } }
Minimize the maximum distance between adjacent points after adding K points anywhere in between | Java program for the above approach ; Function to check if it is possible to add K points such that the maximum distance between adjacent points is D ; Stores the count of point used ; Iterate over all given points ; Add number of points required to be placed between ith and ( i + 1 ) th point ; Return answer ; Function to find the minimum value of maximum distance between adjacent points after adding K points any where between ; Stores the lower bound and upper bound of the given range ; Perform binary search ; Find the middle value ; Update the current range to lower half ; Update the current range to upper half ; Driver Code
import java . math . BigDecimal ; class GFG { public static boolean isPossible ( double D , int arr [ ] , int N , int K ) { int used = 0 ; for ( int i = 0 ; i < N - 1 ; ++ i ) { used += ( int ) ( ( arr [ i + 1 ] - arr [ i ] ) / D ) ; } return used <= K ; } public static double minMaxDist ( int stations [ ] , int N , int K ) { double low = 0 , high = 1e8 ; while ( high - low > 1e-6 ) { double mid = ( low + high ) / 2.0 ; if ( isPossible ( mid , stations , N , K ) ) { high = mid ; } else { low = mid ; } } return low ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int K = 9 ; int N = arr . length ; System . out . printf ( " % .1f " , minMaxDist ( arr , N , K ) ) ; } }
Find Binary permutations of given size not present in the Array | Java program for the above approach ; Function to find a Binary String of same length other than the Strings present in the array ; Map all the Strings present in the array ; Find all the subString that can be made ; If num already exists then increase counter ; If not found print ; If all the subStrings are present then print - 1 ; Driver Code
import java . util . * ; class GFG { static void findMissingBinaryString ( String [ ] nums , int N ) { HashSet < String > s = new HashSet < String > ( ) ; int counter = 0 ; for ( String str : nums ) { s . add ( str ) ; } int total = ( int ) Math . pow ( 2 , N ) ; String ans = " " ; for ( int i = 0 ; i < total ; i ++ ) { String num = " " ; for ( int j = N - 1 ; j >= 0 ; j -- ) { if ( ( i & ( 1 << j ) ) > 0 ) { num += '1' ; } else { num += '0' ; } } if ( s . contains ( num ) ) { counter ++ ; continue ; } else { System . out . print ( num + " , ▁ " ) ; } } if ( counter == total ) { System . out . print ( " - 1" ) ; } } public static void main ( String [ ] args ) { int N = 3 ; String [ ] arr = { "101" , "111" , "001" , "011" , "100" , "110" } ; findMissingBinaryString ( arr , N ) ; } }