codeparrot/xlcost-text-to-code huggingface dataset:
Convert Unix timestamp to DD / MM / YYYY HH : MM : SS format | Unix time is in seconds and Humar Readable Format : DATE : MONTH : YEAR : HOUR : MINUTES : SECONDS , Start of unix time : 01 Jan 1970 , 00 : 00 : 00 ; Function to convert unix time to Human readable format ; Save the time in Human readable format ; Number of days in month in normal year ; Calculate total days unix time T ; Calculating current year ; Updating extradays because it will give days till previous day and we have include current day ; Calculating MONTH and DATE ; Current Month ; Calculating HH : MM : YYYY ; Return the time ; Driver Code ; Given unix time ; Function call to convert unix time to human read able ; Print time in format DD : MM : YYYY : HH : MM : SS
import java . util . * ; class GFG { static String unixTimeToHumanReadable ( int seconds ) { String ans = " " ; int daysOfMonth [ ] = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ; int currYear , daysTillNow , extraTime , extraDays , index , date , month , hours , minutes , secondss , flag = 0 ; daysTillNow = seconds / ( 24 * 60 * 60 ) ; extraTime = seconds % ( 24 * 60 * 60 ) ; currYear = 1970 ; while ( daysTillNow >= 365 ) { if ( currYear % 400 == 0 || ( currYear % 4 == 0 && currYear % 100 != 0 ) ) { daysTillNow -= 366 ; } else { daysTillNow -= 365 ; } currYear += 1 ; } extraDays = daysTillNow + 1 ; if ( currYear % 400 == 0 || ( currYear % 4 == 0 && currYear % 100 != 0 ) ) flag = 1 ; month = 0 ; index = 0 ; if ( flag == 1 ) { while ( true ) { if ( index == 1 ) { if ( extraDays - 29 < 0 ) break ; month += 1 ; extraDays -= 29 ; } else { if ( extraDays - daysOfMonth [ index ] < 0 ) { break ; } month += 1 ; extraDays -= daysOfMonth [ index ] ; } index += 1 ; } } else { while ( true ) { if ( extraDays - daysOfMonth [ index ] < 0 ) { break ; } month += 1 ; extraDays -= daysOfMonth [ index ] ; index += 1 ; } } if ( extraDays > 0 ) { month += 1 ; date = extraDays ; } else { if ( month == 2 && flag == 1 ) date = 29 ; else { date = daysOfMonth [ month - 1 ] ; } } hours = extraTime / 3600 ; minutes = ( extraTime % 3600 ) / 60 ; secondss = ( extraTime % 3600 ) % 60 ; ans += String . valueOf ( date ) ; ans += " / " ; ans += String . valueOf ( month ) ; ans += " / " ; ans += String . valueOf ( currYear ) ; ans += " ▁ " ; ans += String . valueOf ( hours ) ; ans += " : " ; ans += String . valueOf ( minutes ) ; ans += " : " ; ans += String . valueOf ( secondss ) ; return ans ; } public static void main ( String [ ] args ) { int T = 1595497956 ; String ans = unixTimeToHumanReadable ( T ) ; System . out . print ( ans + "NEW_LINE"); } }
Maximum area of a Rectangle that can be circumscribed about a given Rectangle of size LxW | Java program for the above approach ; Function to find area of rectangle inscribed another rectangle of length L and width W ; Area of rectangle ; Return the area ; Driver Code ; Given dimensions ; Function call
import java . io . * ; import java . util . * ; class GFG { static double AreaofRectangle ( int L , int W ) { double area = ( W + L ) * ( W + L ) / 2 ; return area ; } public static void main ( String args [ ] ) { int L = 18 ; int W = 12 ; System . out . println ( AreaofRectangle ( L , W ) ) ; } }
Minimum number of operations required to reduce N to 0 | Java program to implement the above approach ; Function to count the minimum steps required to reduce n ; Base case ; Allocate memory for storing intermediate results ; Store base values ; Stores square root of each number ; Compute square root ; Use rule 1 to find optimized answer ; Check if it perfectly divides n ; Use of rule 2 to find the optimized answer ; Store computed value ; Return answer ; Driver Code
class GFG { static int downToZero ( int n ) { if ( n <= 3 ) return n ; int [ ] dp = new int [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) dp [ i ] = - 1 ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; dp [ 2 ] = 2 ; dp [ 3 ] = 3 ; int sqr ; for ( int i = 4 ; i <= n ; i ++ ) { sqr = ( int ) Math . sqrt ( i ) ; int best = Integer . MAX_VALUE ; while ( sqr > 1 ) { if ( i % sqr == 0 ) { best = Math . min ( best , 1 + dp [ sqr ] ) ; } sqr -- ; } best = Math . min ( best , 1 + dp [ i - 1 ] ) ; dp [ i ] = best ; } return dp [ n ] ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . print ( downToZero ( n ) ) ; } }
Minimum number of operations required to reduce N to 0 | Java Program to implement the above approach ; Function to find the minimum steps required to reduce n ; Base case ; Return answer based on parity of n ; Driver Code
class GFG { static int downToZero ( int n ) { if ( n <= 3 ) return n ; return n % 2 == 0 ? 3 : 4 ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( downToZero ( n ) ) ; } }
Minimum count of elements required to obtain the given Array by repeated mirror operations | Java Program to implement the above approach ; Function to find minimum number of elements required to form A [ ] by performing mirroring operation ; Initialize K ; Odd length array cannot be formed by mirror operation ; Check if prefix of length K is palindrome ; Check if not a palindrome ; If found to be palindrome ; Otherwise ; Return the final answer ; Driver Code
class GFG { static int minimumrequired ( int A [ ] , int N ) { int K = N ; int ans = 0 ; while ( K > 0 ) { if ( K % 2 == 1 ) { ans = K ; break ; } int ispalindrome = 1 ; for ( int i = 0 ; i < K / 2 ; i ++ ) { if ( A [ i ] != A [ K - 1 - i ] ) ispalindrome = 0 ; } if ( ispalindrome == 1 ) { ans = K / 2 ; K /= 2 ; } else { ans = K ; break ; } } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 2 , 1 , 1 , 2 , 2 , 1 } ; int N = a . length ; System . out . println ( minimumrequired ( a , N ) ) ; } }
Sum of product of all integers upto N with their count of divisors | Java program for the above approach ; Function to find the sum of the product of all the integers and their positive divisors up to N ; Iterate for every number between 1 and N ; Find the first multiple of i between 1 and N ; Find the last multiple of i between 1 and N ; Find the total count of multiple of in [ 1 , N ] ; Compute the contribution of i using the formula ; Add the contribution of i to the answer ; Return the result ; Driver code ; Given N ; function call
class GFG { static int sumOfFactors ( int N ) { int ans = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { int first = i ; int last = ( N / i ) * i ; int factors = ( last - first ) / i + 1 ; int totalContribution = ( ( ( factors ) * ( factors + 1 ) ) / 2 ) * i ; ans += totalContribution ; } return ans ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . println ( sumOfFactors ( N ) ) ; } }
Find a number M < N such that difference between their XOR and AND is maximum | Java program for the above approach ; Function to return M < N such that N ^ M - N & M is maximum ; Initialize variables ; Iterate for all values < N ; Find the difference between Bitwise XOR and AND ; Check if new difference is greater than previous maximum ; Update variables ; Return the answer ; Driver Code ; Given Number N ; Function Call
class GFG { static int getMaxDifference ( int N ) { int M = - 1 ; int maxDiff = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int diff = ( N ^ i ) - ( N & i ) ; if ( diff >= maxDiff ) { maxDiff = diff ; M = i ; } } return M ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . print ( getMaxDifference ( N ) ) ; } }
Find a number M < N such that difference between their XOR and AND is maximum | Java program for the above approach ; Function to flip all bits of N ; Finding most significant bit of N ; Calculating required number ; Return the answer ; Driver Code ; Given number ; Function call
class GFG { static int findM ( int N ) { int M = 0 ; int MSB = ( int ) Math . log ( N ) ; for ( int i = 0 ; i < MSB ; i ++ ) { if ( ( N & ( 1 << i ) ) == 0 ) M += ( 1 << i ) ; } return M ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . print ( findM ( N ) ) ; } }
Number of containers that can be filled in the given time | Java program for the above problem ; Matrix of containers ; Function to find the number of containers that will be filled in X seconds ; Container on top level ; If container gets filled ; Dividing the liquid equally in two halves ; Driver code
import java . util . * ; class GFG { static double cont [ ] [ ] = new double [ 1000 ] [ 1000 ] ; static void num_of_containers ( int n , double x ) { int count = 0 ; cont [ 1 ] [ 1 ] = x ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) { if ( cont [ i ] [ j ] >= ( double ) 1 ) { count ++ ; cont [ i + 1 ] [ j ] += ( cont [ i ] [ j ] - ( double ) 1 ) / ( double ) 2 ; cont [ i + 1 ] [ j + 1 ] += ( cont [ i ] [ j ] - ( double ) 1 ) / ( double ) 2 ; } } } System . out . print ( count ) ; } public static void main ( String [ ] args ) { int n = 3 ; double x = 5 ; num_of_containers ( n , x ) ; } }
Check whether there exists a triplet ( i , j , k ) such that arr [ i ] < arr [ k ] < arr [ j ] for i < j < k | Java program for the above approach ; Function to check if there exist triplet in the array such that i < j < k and arr [ i ] < arr [ k ] < arr [ j ] ; Initialize the heights of h1 and h2 to INT_MAX and INT_MIN respectively ; Store the current element as h1 ; If the element at top of stack is less than the current element then pop the stack top and keep updating the value of h3 ; Push the current element on the stack ; If current element is less than h3 , then we found such triplet and return true ; No triplet found , hence return false ; Driver code ; Given array ; Function call
import java . util . * ; class GFG { public static boolean findTriplet ( int [ ] arr ) { int n = arr . length ; Stack < Integer > st = new Stack < > ( ) ; int h3 = Integer . MIN_VALUE ; int h1 = Integer . MAX_VALUE ; for ( int i = n - 1 ; i >= 0 ; i -- ) { h1 = arr [ i ] ; while ( ! st . empty ( ) && st . peek ( ) < arr [ i ] ) { h3 = st . peek ( ) ; st . pop ( ) ; } st . push ( arr [ i ] ) ; if ( h1 < h3 ) { return true ; } } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 7 , 5 , 6 } ; if ( findTriplet ( arr ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Minimum number of distinct elements after removing M items | Set 2 | Java program to implement the above approach ; Function to return minimum distinct character after M removals ; Count the occurences of number and store in count ; Count the occurences of the frequencies ; Take answer as total unique numbers and remove the frequency and subtract the answer ; Remove the minimum number of times ; Return the answer ; Driver code ; Initialize array ; Size of array ; Function call
import java . util . * ; class GFG { static int distinctNumbers ( int arr [ ] , int m , int n ) { Map < Integer , Integer > count = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) count . put ( arr [ i ] , count . getOrDefault ( arr [ i ] , 0 ) + 1 ) ; int [ ] fre_arr = new int [ n + 1 ] ; for ( Integer it : count . values ( ) ) { fre_arr [ it ] ++ ; } int ans = count . size ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { int temp = fre_arr [ i ] ; if ( temp == 0 ) continue ; int t = Math . min ( temp , m / i ) ; ans -= t ; m -= i * t ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 1 , 5 , 3 , 5 , 1 , 3 } ; int n = arr . length ; int m = 2 ; System . out . println ( distinctNumbers ( arr , m , n ) ) ; } }
Find minimum moves to bring all elements in one cell of a matrix | Java implementation to find the minimum number of moves to bring all non - zero element in one cell of the matrix ; Function to find the minimum number of moves to bring all elements in one cell of matrix ; Moves variable to store the sum of number of moves ; Loop to count the number of the moves ; Condition to check that the current cell is a non - zero element ; Driver code ; Coordinates of given cell ; Given Matrix ; Element to be moved ; Function call
class GFG { static int M = 4 ; static int N = 5 ; public static void no_of_moves ( int [ ] [ ] Matrix , int x , int y ) { int moves = 0 ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( Matrix [ i ] [ j ] != 0 ) { moves += Math . abs ( x - i ) ; moves += Math . abs ( y - j ) ; } } } System . out . println ( moves ) ; } public static void main ( String [ ] args ) { int x = 3 ; int y = 2 ; int [ ] [ ] Matrix = { { 1 , 0 , 1 , 1 , 0 } , { 0 , 1 , 1 , 0 , 1 } , { 0 , 0 , 1 , 1 , 0 } , { 1 , 1 , 1 , 0 , 0 } } ; int num = 1 ; no_of_moves ( Matrix , x , y ) ; } }
Check if all array elements can be removed by the given operations | Java program to implement the above approach ; Function to check if it is possible to remove all array elements ; Condition if we can remove all elements from the array ; Driver Code
class GFG { static void removeAll ( int arr [ ] , int n ) { if ( arr [ 0 ] < arr [ n - 1 ] ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } public static void main ( String [ ] args ) { int Arr [ ] = { 10 , 4 , 7 , 1 , 3 , 6 } ; int size = Arr . length ; removeAll ( Arr , size ) ; } }
Smallest subarray having an element with frequency greater than that of other elements | Java program for the above approach ; Function to find subarray ; If the array has only one element , then there is no answer . ; Array to store the last occurrences of the elements of the array . ; To maintain the length ; Variables to store start and end indices ; Check if element is occurring for the second time in the array ; Find distance between last and current index of the element . ; If the current distance is less then len update len and set ' start ' and ' end ' ; Set the last occurrence of current element to be ' i ' . ; If flag is equal to 0 , it means there is no answer . ; Driver code
import java . util . * ; class GFG { public static void FindSubarray ( int [ ] arr , int n ) { if ( n == 1 ) { System . out . println ( " No ▁ such ▁ subarray ! " ) ; } int [ ] vis = new int [ n + 1 ] ; Arrays . fill ( vis , - 1 ) ; vis [ arr [ 0 ] ] = 0 ; int len = Integer . MAX_VALUE , flag = 0 ; int start = 0 , end = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int t = arr [ i ] ; if ( vis [ t ] != - 1 ) { int distance = i - vis [ t ] + 1 ; if ( distance < len ) { len = distance ; start = vis [ t ] ; end = i ; } flag = 1 ; } vis [ t ] = i ; } if ( flag == 0 ) System . out . println ( " No ▁ such ▁ subarray ! " ) ; else { for ( int i = start ; i <= end ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 2 , 4 , 5 } ; int n = arr . length ; FindSubarray ( arr , n ) ; } }
Count substring of Binary string such that each character belongs to a palindrome of size greater than 1 | Java implementation to find the substrings in binary string such that every character belongs to a palindrome ; Function to to find the substrings in binary string such that every character belongs to a palindrome ; Total substrings ; Loop to store the count of continious characters in the given string ; Subtract non special strings from answer ; Driver code ; Given string s ; Function call
import java . util . * ; class GFG { public static int countSubstrings ( String s ) { int n = s . length ( ) ; int answer = ( n * ( n - 1 ) ) / 2 ; int cnt = 1 ; Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( s . charAt ( i ) == s . charAt ( i - 1 ) ) cnt ++ ; else { v . add ( cnt ) ; cnt = 1 ; } } if ( cnt > 0 ) v . add ( cnt ) ; for ( int i = 0 ; i < v . size ( ) - 1 ; i ++ ) { answer -= ( v . get ( i ) + v . get ( i + 1 ) - 1 ) ; } return answer ; } public static void main ( String [ ] args ) { String s = "00111" ; System . out . print ( countSubstrings ( s ) ) ; } }
Minimum minutes needed to make the time palindromic | Java program for the above approach ; Function to get the required minutes ; Storing hour and minute value in integral form ; Keep iterating till first digit hour becomes equal to second digit of minute and second digit of hour becomes equal to first digit of minute ; If mins is 60 , increase hour , and reinitilialized to 0 ; If hours is 60 , reinitialized to 0 ; Return the required time ; Driver code ; Given Time as a string ; Function Call
class GFG { public static int get_palindrome_time ( String str ) { int hh , mm ; hh = ( str . charAt ( 0 ) - 48 ) * 10 + ( str . charAt ( 1 ) - 48 ) ; mm = ( str . charAt ( 3 ) - 48 ) * 10 + ( str . charAt ( 4 ) - 48 ) ; int requiredTime = 0 ; while ( hh % 10 != mm / 10 hh / 10 != mm % 10 ) { ++ mm ; if ( mm == 60 ) { mm = 0 ; ++ hh ; } if ( hh == 24 ) hh = 0 ; ++ requiredTime ; } return requiredTime ; } public static void main ( String [ ] args ) { String str = "05:39" ; System . out . println ( get_palindrome_time ( str ) ) ; } }
Maximum sum of values in a given range of an Array for Q queries when shuffling is allowed | Java implementation to find the maximum sum of K subarrays when shuffling is allowed ; Function to find the maximum sum of all subarrays ; Initialize maxsum and prefixArray ; Find the frequency using prefix Array ; Perform prefix sum ; Sort both arrays to get a greedy result ; Finally multiply largest frequency with largest array element . ; Return the answer ; Driver code ; Initial Array ; Subarrays ; Function call
import java . util . * ; import java . lang . * ; class GFG { static int maximumSubarraySum ( int a [ ] , int n , ArrayList < List < Integer > > subarrays ) { int i , maxsum = 0 ; int [ ] prefixArray = new int [ n ] ; for ( i = 0 ; i < subarrays . size ( ) ; ++ i ) { prefixArray [ subarrays . get ( i ) . get ( 0 ) - 1 ] ++ ; prefixArray [ subarrays . get ( i ) . get ( 1 ) ] -- ; } for ( i = 1 ; i < n ; i ++ ) { prefixArray [ i ] += prefixArray [ i - 1 ] ; } Arrays . sort ( prefixArray ) ; Arrays . sort ( a ) ; for ( i = 0 ; i < n ; i ++ ) maxsum += a [ i ] * prefixArray [ i ] ; return maxsum ; } public static void main ( String [ ] args ) { int n = 6 ; int a [ ] = { 4 , 1 , 2 , 1 , 9 , 2 } ; ArrayList < List < Integer > > subarrays = new ArrayList < > ( ) ; subarrays . add ( Arrays . asList ( 1 , 2 ) ) ; subarrays . add ( Arrays . asList ( 1 , 3 ) ) ; subarrays . add ( Arrays . asList ( 1 , 4 ) ) ; subarrays . add ( Arrays . asList ( 3 , 4 ) ) ; System . out . println ( maximumSubarraySum ( a , n , subarrays ) ) ; } }
Maximum profit such that total stolen value is less than K to get bonus | Java implementation to find the maximum stolen value such that total stolen value is less than K ; Function to find the maximum profit from the given values ; Iterating over every possible permutation ; Driver Code ; Function Call
import java . util . * ; class GFG { static int maxProfit ( int [ ] value , int N , int K ) { Arrays . sort ( value ) ; int maxval = value [ N - 1 ] ; int maxProfit = 0 ; int curr_val ; do { curr_val = 0 ; for ( int i = 0 ; i < N ; i ++ ) { curr_val += value [ i ] ; if ( curr_val <= K ) { maxProfit = Math . max ( curr_val + maxval * ( i + 1 ) , maxProfit ) ; } } } while ( next_permutation ( value ) ) ; return maxProfit ; } static boolean next_permutation ( int [ ] p ) { for ( int a = p . length - 2 ; a >= 0 ; -- a ) if ( p [ a ] < p [ a + 1 ] ) for ( int b = p . length - 1 ; ; -- b ) if ( p [ b ] > p [ a ] ) { int t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; for ( ++ a , b = p . length - 1 ; a < b ; ++ a , -- b ) { t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; } return true ; } return false ; } public static void main ( String [ ] args ) { int N = 4 , K = 6 ; int [ ] values = { 5 , 2 , 7 , 3 } ; System . out . print ( maxProfit ( values , N , K ) ) ; } }
Min operations to reduce N by multiplying by any number or taking square root | Java implementation of the above approach ; Function to reduce N to its minimum possible value by the given operations ; Keep replacing n until is an integer ; Keep replacing n until n is divisible by i * i ; Print the answer ; Driver code ; Given N ; Function call
import java . lang . Math ; class GFG { static void minValue ( int n ) { while ( ( int ) Math . sqrt ( n ) == Math . sqrt ( n ) && n > 1 ) { n = ( int ) ( Math . sqrt ( n ) ) ; } for ( int i = ( int ) ( Math . sqrt ( n ) ) ; i > 1 ; i -- ) { while ( n % ( i * i ) == 0 ) n /= i ; } System . out . println ( n ) ; } public static void main ( String args [ ] ) { int N = 20 ; minValue ( N ) ; } }
Count of ways to split given string into two non | Java program to implement the above approach ; Function to check whether the substring from l to r is palindrome or not ; If characters at l and r differ ; Not a palindrome ; If the string is a palindrome ; Function to count and return the number of possible splits ; Stores the count of splits ; Check if the two substrings after the split are palindromic or not ; If both are palindromes ; Print the final count ; Driver Code
class GFG { public static boolean isPalindrome ( int l , int r , String s ) { while ( l <= r ) { if ( s . charAt ( l ) != s . charAt ( r ) ) return false ; l ++ ; r -- ; } return true ; } public static int numWays ( String s ) { int n = s . length ( ) ; int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( isPalindrome ( 0 , i , s ) && isPalindrome ( i + 1 , n - 1 , s ) ) { ans ++ ; } } return ans ; } public static void main ( String args [ ] ) { String S = " aaaaa " ; System . out . println ( numWays ( S ) ) ; } }
Final direction after visiting every cell of Matrix starting from ( 0 , 0 ) | Java program to find the direction when stopped moving ; Function to find the direction when stopped moving ; Driver code ; Given size of NxM grid ; Function Call
class GFG { static void findDirection ( int n , int m ) { if ( n > m ) { if ( m % 2 == 0 ) System . out . print ( "UpNEW_LINE"); else System . out . print ( "DownNEW_LINE"); } else { if ( n % 2 == 0 ) System . out . print ( "LeftNEW_LINE"); else System . out . print ( "RightNEW_LINE"); } } public static void main ( String [ ] args ) { int n = 3 , m = 3 ; findDirection ( n , m ) ; } }
Maximize the sum of modulus with every Array element | Java program to find the maximum sum of modulus with every array element ; Function to return the maximum sum of modulus with every array element ; Sum of array elements ; Return the answer ; Driver Code
import java . io . * ; class GFG { static int maxModulosum ( int a [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } return sum - n ; } public static void main ( String [ ] args ) { int a [ ] = new int [ ] { 3 , 4 , 6 } ; int n = a . length ; System . out . println ( maxModulosum ( a , n ) ) ; } }
Minimum jumps required to group all 1 s together in a given Binary string | Java Program to find the minimum number of jumps required to group all ones together in the binary string ; Function to get the minimum jump value ; Store all indices of ones ; Populating one 's indices ; Calculate median ; Jumps required for 1 's  to the left of median ; Jumps required for 1 's  to the right of median ; Return the final answer ; Driver code
import java . io . * ; import java . util . * ; class GFG { public static int getMinJumps ( String s ) { Vector < Integer > ones = new Vector < Integer > ( ) ; int jumps = 0 , median = 0 , ind = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == '1' ) ones . add ( i ) ; } if ( ones . size ( ) == 0 ) return jumps ; median = ( int ) ones . get ( ones . size ( ) / 2 ) ; ind = median ; for ( int i = ind ; i >= 0 ; i -- ) { if ( s . charAt ( i ) == '1' ) { jumps += ind - i ; ind -- ; } } ind = median ; for ( int i = ind ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == '1' ) { jumps += i - ind ; ind ++ ; } } return jumps ; } public static void main ( String [ ] args ) { String S = "00100000010011" ; System . out . println ( getMinJumps ( S ) ) ; } }