Minimum jumps required to group all 1s together in a given Binary string
Given a binary string S, the task is to count the minimum number of jumps required to group all 1’s together.
Examples:
Input: S = “000010011000100”
Output: 5
Explanation:
000010011000100 -> 000000111000100 requires 2 jumps.
000000111000100 -> 000000111100000 requires 3 jumps.
Hence, at least 5 jumps are required to group all 1’s together.
Input: S = “100010001”
Output: 6
Explanation:
100010001 -> 000110001 requires 3 jumps.
000110001 -> 000111000 requires 3 jumps.
Approach:
We can observe that in order to minimize the number of jumps required for grouping all 1’s together, they need to be grouped near the median of their current positions. Calculate the median and the number of moves required to shift the 1’s to the nearest position of 0 in the left of the median. Perform the same operation for the right of the median.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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) << '\n' ;
return 0;
}
|
Java
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));
}
}
|
Python3
def getMinJumps(s):
ones = []
jumps, median, ind = 0 , 0 , 0
for i in range ( len (s)):
if (s[i] = = '1' ):
ones.append(i)
if ( len (ones) = = 0 ):
return jumps
median = ones[ len (ones) / / 2 ]
ind = median
for i in range (ind, - 1 , - 1 ):
if (s[i] = = '1' ):
jumps + = ind - i
ind - = 1
ind = median
for i in range (ind, len (s)):
if (s[i] = = '1' ):
jumps + = i - ind
ind + = 1
return jumps
if __name__ = = '__main__' :
s = "00100000010011"
print (getMinJumps(s))
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
public static int getMinJumps( string s)
{
ArrayList ones = new ArrayList();
int jumps = 0, median = 0, ind = 0;
for ( int i = 0; i < s.Length; i++)
{
if (s[i] == '1' )
ones.Add(i);
}
if (ones.Count== 0)
return jumps;
median = ( int )ones[ones.Count / 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;
}
public static void Main( string [] args)
{
string S = "00100000010011" ;
Console.Write(getMinJumps(S));
}
}
|
Javascript
<script>
function getMinJumps(s)
{
let ones = [];
let jumps = 0, median = 0, ind = 0;
for (let i = 0; i < s.length; i++)
{
if (s[i] == '1 ')
ones.push(i);
}
if (ones.length == 0)
return jumps;
// Calculate median
median = ones[parseInt(ones.length / 2, 10)];
ind = median;
// Jumps required for 1' s
for (let i = ind; i >= 0; i--)
{
if (s[i] == '1' )
{
jumps += ind - i;
ind--;
}
}
ind = median;
for (let i = ind; i < s.length; i++)
{
if (s[i] == '1')
{
jumps += i - ind;
ind++;
}
}
return jumps;
}
let S = "00100000010011" ;
document.write(getMinJumps(S));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
14 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...