Check if All the 1s in a Binary String are Equidistant in C++



In this problem, we are given a binary string, and we have to check if all the 1s present in the binary string are at an equivalent distance from each other.

Example 1

  • Input:
    binaryString = "10101010"
  • Output: Yes

Explanation

The positions of 1s in the given string are 1, 3, 5, and 7.
Here, the difference between consecutive 1s is 2 (as 7-5 equals 5-3 equals 3-1), so all 1s are at an equivalent distance from each other.

Example 2

  • Input:
    binaryString = "1000011"
  • Output: No

Explanation

The positions of 1s in the given string are 1, 6, and 7.
Here, the difference between consecutive 1s is inconsistent (as 7-6 is not equal to 6-1), which breaks the equidistant pattern.

Example 3

  • Input:
    binaryString = "11111111"
  • Output: Yes

Explanation

The binary given string contains only 1s.
The difference between all that given 1s is equidistant.

Brute Force Approach

In this brute force approach, we will first traverse through the given string using a loop and store the indices of all 1s present in the binary string. Then, we check if the distance between all 1s is constant or not. If the distance between all 1s is equidistant, then return true; else return false.

Steps for Implementation

  1. Start traversing the binary string using a loop and store the index at which 1s occur.
  2. Now, check for the difference between the indices of each consecutive 1s present in the binary string.
  3. If all differences are equal, then print "yes"; else print "no".

Implementation Code

#include <bits/stdc++.h>
using namespace std;

bool are1sEquidistant(string &inputString) {
    vector<int> oneIndex;

    // Step 1: Store all indices where '1' occurs
    for (size_t i = 0; i < inputString.size(); ++i) {
        if (inputString[i] == '1') {
            oneIndex.push_back(i);
        }
    }

    // Step 2: If less than 2 '1's, return true
    if (oneIndex.size() < 2) return true;

    // Step 3: Check equal difference between every consecutive 1s
    int distance = oneIndex[1] - oneIndex[0];
    for (size_t i = 2; i < oneIndex.size(); ++i) {
        if (oneIndex[i] - oneIndex[i - 1] != distance)
            return false;
    }

    return true;
}

int main() {
    string inputString = "100101";
    cout << (are1sEquidistant(inputString) ? "Yes" : "No") << endl;
    return 0;
}

Output

No

Time Complexity: O(N)
Space Complexity: O(k), where k is the number of 1s.

Optimized Approach

In this approach, we do not store the position of all 1s present in the binary string. Instead, we track the last-seen index of 1 and compare the distance on the go.

Steps for Implementation

  • Keep a variable to remember the previous index of 1.
  • Now, traverse the string, when a 1 is found: if it is the second 1, store the distance.
  • For later 1s, check if the current distance is equal to the stored one. 
  • If all the distances match, then return "Yes"; else return "No".

Implementation Code

#include <bits/stdc++.h>
using namespace std;

bool are1sEquidistant(string &inputString) {
    int prevIndex = -1;   // Last seen '1'
    int expectedDistance = -1;

    for (int i = 0; i < inputString.size(); ++i) {
        if (inputString[i] == '1') {
            if (prevIndex == -1) {
                prevIndex = i;  // First 1 found
            } else {
                int currentDistance = i - prevIndex;
                if (expectedDistance == -1) {

                   // Set expected distance
                    expectedDistance = currentDistance;
                } else if (currentDistance != expectedDistance) {
                    return false;  // Not equidistant
                }
                prevIndex = i;  // Update last seen index
            }
        }
    }

    return true;
}

int main() {
    string inputString = "100101";
    cout << (are1sEquidistant(inputString) ? "Yes" : "No") << endl;
    return 0;
}

Output

 No

Time Complexity: O(N)
Space Complexity: O(1)

Updated on: 2025-04-14T12:40:11+05:30

45 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements