Saturday, September 21, 2024
Google search engine
HomeData Modelling & AIRotations of a Binary String with Odd Value

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 the last bit is 2^0.Hence, since we are doing the circular rotation. 

Implementation:

C++




// 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;
}


Java




// 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


Python3




# 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


C#




// 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


PHP




<?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
?>


Javascript




<script>
 
// Javascript program to find count of rotations
// with odd value.
 
// function to calculate total odd decimal
// equivalent
function oddEquivalent(s, n)
{
    var count = 0;
    for (var i = 0; i < n; i++) {
        if (s[i] == '1')
            count++;
    }
    return count;
}
 
// Driver code
var s = "1011011";
var n = s.length;
document.write( oddEquivalent(s, n));
 
</script>   


Output

5

Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments