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
#include <bits/stdc++.h> NEW_LINE using namespace std ; string unixTimeToHumanReadable ( long int seconds ) { string ans = " " ; int daysOfMonth [ ] = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ; long 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 += to_string ( date ) ; ans += " / " ; ans += to_string ( month ) ; ans += " / " ; ans += to_string ( currYear ) ; ans += " ▁ " ; ans += to_string ( hours ) ; ans += " : " ; ans += to_string ( minutes ) ; ans += " : " ; ans += to_string ( secondss ) ; return ans ; } int main ( ) { long int T = 1595497956 ; string ans = unixTimeToHumanReadable ( T ) ; cout << ans << " STRNEWLINE " ; return 0 ; }
Maximum area of a Rectangle that can be circumscribed about a given Rectangle of size LxW | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; double AreaofRectangle ( int L , int W ) { double area = ( W + L ) * ( W + L ) / 2 ; return area ; } int main ( ) { int L = 18 ; int W = 12 ; cout << AreaofRectangle ( L , W ) ; return 0 ; }
Minimum number of operations required to reduce N to 0 | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int downToZero ( int n ) { if ( n <= 3 ) return n ; vector < int > dp ( n + 1 , -1 ) ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; dp [ 2 ] = 2 ; dp [ 3 ] = 3 ; int sqr ; for ( int i = 4 ; i <= n ; i ++ ) { sqr = sqrt ( i ) ; int best = INT_MAX ; while ( sqr > 1 ) { if ( i % sqr == 0 ) { best = min ( best , 1 + dp [ sqr ] ) ; } sqr -- ; } best = min ( best , 1 + dp [ i - 1 ] ) ; dp [ i ] = best ; } return dp [ n ] ; } int main ( ) { int n = 4 ; cout << downToZero ( n ) ; return 0 ; }
Minimum number of operations required to reduce N to 0 | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int downToZero ( int n ) { if ( n <= 3 ) return n ; return n % 2 == 0 ? 3 : 4 ; } int main ( ) { int n = 4 ; cout << downToZero ( n ) ; return 0 ; }
Minimum count of elements required to obtain the given Array by repeated mirror operations | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumrequired ( int A [ ] , int N ) { int K = N ; int ans ; while ( K > 0 ) { if ( K % 2 == 1 ) { ans = K ; break ; } bool ispalindrome = 1 ; for ( int i = 0 ; i < K / 2 ; i ++ ) { if ( A [ i ] != A [ K - 1 - i ] ) ispalindrome = 0 ; } if ( ispalindrome ) { ans = K / 2 ; K /= 2 ; } else { ans = K ; break ; } } return ans ; } int main ( ) { int a [ ] = { 1 , 2 , 2 , 1 , 1 , 2 , 2 , 1 } ; int N = sizeof a / sizeof a [ 0 ] ; cout << minimumrequired ( a , N ) ; return 0 ; }
Sum of product of all integers upto N with their count of divisors | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long sumOfFactors ( int N ) { long long ans = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { long long first = i ; long long last = ( N / i ) * i ; long long factors = ( last - first ) / i + 1 ; long long totalContribution = ( ( ( factors ) * ( factors + 1 ) ) / 2 ) * i ; ans += totalContribution ; } return ans ; } int main ( ) { int N = 3 ; cout << sumOfFactors ( N ) ; }
Find a number M < N such that difference between their XOR and AND is maximum | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; 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 ; } int main ( ) { int N = 6 ; cout << getMaxDifference ( N ) ; return 0 ; }
Find a number M < N such that difference between their XOR and AND is maximum | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findM ( int N ) { int M = 0 ; int MSB = ( int ) log2 ( N ) ; for ( int i = 0 ; i < MSB ; i ++ ) { if ( ! ( N & ( 1 << i ) ) ) M += ( 1 << i ) ; } return M ; } int main ( ) { int N = 6 ; cout << findM ( N ) ; return 0 ; }
Number of containers that can be filled in the given time | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; double cont [ 1000 ] [ 1000 ] ; 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 ; } } } cout << count ; } int main ( ) { int n = 3 ; double x = 5 ; num_of_containers ( n , x ) ; return 0 ; }
Check whether there exists a triplet ( i , j , k ) such that arr [ i ] < arr [ k ] < arr [ j ] for i < j < k | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool findTriplet ( vector < int > & arr ) { int n = arr . size ( ) ; stack < int > st ; int h3 = INT_MIN , h1 = INT_MAX ; for ( int i = n - 1 ; i >= 0 ; i -- ) { h1 = arr [ i ] ; while ( ! st . empty ( ) && st . top ( ) < arr [ i ] ) { h3 = st . top ( ) ; st . pop ( ) ; } st . push ( arr [ i ] ) ; if ( h1 < h3 ) { return true ; } } return false ; } int main ( ) { vector < int > arr = { 4 , 7 , 5 , 6 } ; if ( findTriplet ( arr ) ) { cout << " ▁ Yes " << ' ' ; } else { cout << " ▁ No " << ' ' ; } return 0 ; }
Minimum number of distinct elements after removing M items | Set 2 | C ++ program for 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int distinctNumbers ( int arr [ ] , int m , int n ) { unordered_map < int , int > count ; for ( int i = 0 ; i < n ; i ++ ) count [ arr [ i ] ] ++ ; vector < int > fre_arr ( n + 1 , 0 ) ; for ( auto it : count ) { fre_arr [ it . second ] ++ ; } int ans = count . size ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { int temp = fre_arr [ i ] ; if ( temp == 0 ) continue ; int t = min ( temp , m / i ) ; ans -= t ; m -= i * t ; } return ans ; } int main ( ) { int arr [ ] = { 2 , 4 , 1 , 5 , 3 , 5 , 1 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int m = 2 ; cout << distinctNumbers ( arr , m , n ) ; return 0 ; }
Find minimum moves to bring all elements in one cell of a matrix | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int M = 4 ; const int N = 5 ; void no_of_moves ( int Matrix [ M ] [ N ] , 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 += abs ( x - i ) ; moves += abs ( y - j ) ; } } } cout << moves << " STRNEWLINE " ; } int main ( ) { int x = 3 ; int y = 2 ; int Matrix [ M ] [ N ] = { { 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 ) ; return 0 ; }
Check if all array elements can be removed by the given operations | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void removeAll ( int arr [ ] , int n ) { if ( arr [ 0 ] < arr [ n - 1 ] ) cout << " YES " ; else cout << " NO " ; } int main ( ) { int Arr [ ] = { 10 , 4 , 7 , 1 , 3 , 6 } ; int size = sizeof ( Arr ) / sizeof ( Arr [ 0 ] ) ; removeAll ( Arr , size ) ; return 0 ; }
Smallest subarray having an element with frequency greater than that of other elements | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void FindSubarray ( int arr [ ] , int n ) { if ( n == 1 ) { cout << " No ▁ such ▁ subarray ! " << endl ; } int vis [ n + 1 ] ; memset ( vis , -1 , sizeof ( vis ) ) ; vis [ arr [ 0 ] ] = 0 ; int len = INT_MAX , flag = 0 ; int start , end ; 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 ) cout << " No ▁ such ▁ subarray ! " << endl ; else { for ( int i = start ; i <= end ; i ++ ) cout << arr [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 2 , 3 , 2 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; FindSubarray ( arr , n ) ; return 0 ; }
Count substring of Binary string such that each character belongs to a palindrome of size greater than 1 | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubstrings ( string s ) { int n = s . length ( ) ; int answer = ( n * ( n - 1 ) ) / 2 ; int cnt = 1 ; vector < int > v ; for ( int i = 1 ; i < n ; i ++ ) { if ( s [ i ] == s [ i - 1 ] ) cnt ++ ; else { v . push_back ( cnt ) ; cnt = 1 ; } } if ( cnt > 0 ) v . push_back ( cnt ) ; for ( int i = 0 ; i < v . size ( ) - 1 ; i ++ ) { answer -= ( v [ i ] + v [ i + 1 ] - 1 ) ; } return answer ; } int main ( ) { string s = "00111" ; cout << countSubstrings ( s ) ; return 0 ; }
Minimum minutes needed to make the time palindromic | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int get_palindrome_time ( string str ) { int hh , mm ; hh = ( str [ 0 ] - 48 ) * 10 + ( str [ 1 ] - 48 ) ; mm = ( str [ 3 ] - 48 ) * 10 + ( str [ 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 ; } int main ( ) { string str = "05:39" ; cout << get_palindrome_time ( str ) << endl ; }
Maximum sum of values in a given range of an Array for Q queries when shuffling is allowed | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumSubarraySum ( int a [ ] , int n , vector < pair < int , int > > & subarrays ) { int i , maxsum = 0 ; int prefixArray [ n ] = { 0 } ; for ( i = 0 ; i < subarrays . size ( ) ; ++ i ) { prefixArray [ subarrays [ i ] . first - 1 ] ++ ; prefixArray [ subarrays [ i ] . second ] -- ; } for ( i = 1 ; i < n ; i ++ ) { prefixArray [ i ] += prefixArray [ i - 1 ] ; } sort ( prefixArray , prefixArray + n , greater < int > ( ) ) ; sort ( a , a + n , greater < int > ( ) ) ; for ( i = 0 ; i < n ; i ++ ) maxsum += a [ i ] * prefixArray [ i ] ; return maxsum ; } int main ( ) { int n = 6 ; int a [ ] = { 4 , 1 , 2 , 1 , 9 , 2 } ; vector < pair < int , int > > subarrays ; subarrays . push_back ( { 1 , 2 } ) ; subarrays . push_back ( { 1 , 3 } ) ; subarrays . push_back ( { 1 , 4 } ) ; subarrays . push_back ( { 3 , 4 } ) ; cout << maximumSubarraySum ( a , n , subarrays ) ; }
Maximum profit such that total stolen value is less than K to get bonus | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxProfit ( vector < int > value , int N , int K ) { sort ( value . begin ( ) , value . end ( ) ) ; 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 = max ( curr_val + maxval * ( i + 1 ) , maxProfit ) ; } } } while ( next_permutation ( value . begin ( ) , value . end ( ) ) ) ; return maxProfit ; } int main ( ) { int N = 4 , K = 6 ; vector < int > values { 5 , 2 , 7 , 3 } ; cout << maxProfit ( values , N , K ) ; }
Min operations to reduce N by multiplying by any number or taking square root | C ++ program for 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minValue ( int n ) { while ( int ( sqrt ( n ) ) == sqrt ( n ) && n > 1 ) { n = sqrt ( n ) ; } for ( int i = sqrt ( n ) ; i > 1 ; i -- ) { while ( n % ( i * i ) == 0 ) n /= i ; } cout << n ; } int main ( ) { int N = 20 ; minValue ( N ) ; }
Count of ways to split given string into two non | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPalindrome ( int l , int r , string & s ) { while ( l <= r ) { if ( s [ l ] != s [ r ] ) return false ; l ++ ; r -- ; } return true ; } 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 ; } int main ( ) { string S = " aaaaa " ; cout << numWays ( S ) ; return 0 ; }
Final direction after visiting every cell of Matrix starting from ( 0 , 0 ) | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findDirection ( int n , int m ) { if ( n > m ) { if ( m % 2 == 0 ) printf ( " Up STRNEWLINE " ) ; else printf ( " Down STRNEWLINE " ) ; } else { if ( n % 2 == 0 ) printf ( " Left STRNEWLINE " ) ; else printf ( " Right STRNEWLINE " ) ; } } int main ( ) { int n = 3 , m = 3 ; findDirection ( n , m ) ; return 0 ; }
Maximize the sum of modulus with every Array element | C ++ 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 Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxModulosum ( int a [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } return sum - n ; } int main ( ) { int a [ ] = { 3 , 4 , 6 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << maxModulosum ( a , n ) ; return 0 ; }
Minimum jumps required to group all 1 s together in a given Binary string | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMinJumps ( string s ) { vector < int > ones ; int jumps = 0 , median = 0 , ind = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s [ i ] == '1' ) ones . push_back ( i ) ; } if ( ones . size ( ) == 0 ) return jumps ; median = ones [ ones . size ( ) / 2 ] ; ind = median ; for ( int i = ind ; i >= 0 ; i -- ) { if ( s [ i ] == '1' ) { jumps += ind - i ; ind -- ; } } ind = median ; for ( int i = ind ; i < s . length ( ) ; i ++ ) { if ( s [ i ] == '1' ) { jumps += i - ind ; ind ++ ; } } return jumps ; } int main ( ) { string S = "00100000010011" ; cout << getMinJumps ( S ) << ' ' ; return 0 ; }