Rotations of a Binary String with Odd Value

Given a binary string. We are allowed to do circular rotation of the string without changing the relative order of the bits in the string.
For Example, all possible circular rotation of string “011001” are:

101100
010110
001011
100101
110010

We are required to tell total number of distinct odd decimal equivalent possible of binary string, by doing circular rotation.
Examples:

Input : 011001 
Output : 3
Explanation:
All odd possible binary representations are:
["011001", "001011", "100101"]

Input : 11011
Output : 4
Explanation:
All odd possible binary representations are:
["11011", "01111", "10111", "11101"]

Concept:
It can be observed that a binary string can only be odd if it’s last bit is 1, because the value of last bit is 2^0.Hence, since we are doing circular rotation.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find count of rotations
// with odd value.
#include <bits/stdc++.h>
using namespace std;
  
// function to calculate total odd decimal
// equivalent
int oddEquivalent(string s, int n)
{
    int count = 0; 
    for (int i = 0; i < n; i++) {
        if (s[i] == '1')
            count++;
    }
    return count;
}
  
// Driver code
int main()
{
    string s = "1011011";
    int n = s.length();
    cout << oddEquivalent(s, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find count of rotations 
// with odd value.
  
class solution
{
static int oddEquivalent(String s, int n)
{
  
int count = 0
// function to calculate total odd decimal 
// equivalent 
for (int i = 0; i < n; i++) 
    
        if(s.charAt(i) == '1'
            count++; 
    
    return count;
}
  
// Driver code
public static void main(String ar[])
{
  
String s = "1011011"
int n = s.length(); 
System.out.println(oddEquivalent(s, n)); 
  
}
}
//This code is contributed 
//By Surendra_Gangwar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find count
# of rotations with odd value
  
#function to calculate total odd equivalent
def oddEquivalent(s, n):
    count=0
    for i in range(0,n):
        if (s[i] == '1'):
            count = count + 1
    return count
      
#Driver code
if __name__=='__main__':
    s = "1011011"
    n = len(s)
    print(oddEquivalent(s, n)) 
  
# this code is contributed by Shashank_Sharma

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find count of
// rotations with odd value. 
using System;
  
class GFG 
static int oddEquivalent(String s, int n) 
    int count = 0; 
      
    // function to calculate total 
    // odd decimal equivalent 
    for (int i = 0; i < n; i++) 
        
            if(s[i] == '1'
                count++; 
        
    return count; 
  
// Driver code 
public static void Main() 
    String s = "1011011"
    int n = s.Length; 
    Console.WriteLine(oddEquivalent(s, n)); 
  
// This code is contributed 
// by Subhadeep 

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
// PHP program to find count 
// of rotations with odd value. 
  
// function to calculate total 
// odd decimal equivalent 
function oddEquivalent($s, $n
    $count = 0; 
    for ($i = 0; $i < $n; $i++) 
    
        if ($s[$i] == '1'
            $count++; 
    
    return $count
  
// Driver code 
$s = "1011011"
$n = strlen($s); 
echo(oddEquivalent($s, $n)); 
  
// This code is contributed 
// by Smitha
?>

chevron_right


Output:

5


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.