Rotations of a Binary String with Odd Value
Last Updated :
30 Nov, 2022
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>
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.
Similar Reads
Number of substrings with odd decimal value in a binary string Given a binary string containing only 0's and 1's. Write a program to find number of sub-strings of this string whose decimal representation is odd. Examples : Input : 101 Output : 3 Explanation : Substrings with odd decimal representation are: {1, 1, 101} Input : 1101 Output : 6 Explanation : Subst
6 min read
Left Rotation of a String Given a string s and an integer d, the task is to left rotate the string by d positions.Examples:Input: s = "GeeksforGeeks", d = 2Output: "eksforGeeksGe" Explanation: After the first rotation, string s becomes "eeksforGeeksG" and after the second rotation, it becomes "eksforGeeksGe".Input: s = "qwer
15+ min read
Minimum number of flips with rotation to make binary string alternating Given a binary string S of 0s and 1s. The task is to make the given string a sequence of alternate characters by using the below operations: Remove some prefix from the start and append it to the end.Flip some or every bit in the given string. Print the minimum number of bits to be flipped to make t
11 min read
Generate all rotations of a given string Given a string S. The task is to print all the possible rotated strings of the given string.Examples: Input : S = "geeks"Output : geeks eeksg eksge ksgee sgeekInput : S = "abc" Output : abc bca cabMethod 1 (Simple): The idea is to run a loop from i = 0 to n - 1 ( n = length of string) i.e for each p
8 min read
Count rotations of N which are Odd and Even Given a number n, the task is to count all rotations of the given number which are odd and even. Examples: Input: n = 1234Output: Odd = 2, Even = 2Total rotations: 1234, 2341, 3412, 4123Odd rotations: 2341 and 4123Even rotations: 1234 and 3412Input: n = 246Output: Odd = 0, Even = 3 Brute force appro
7 min read
Counting even decimal value substrings in a binary string Given a binary string of size N. Count all substring that have even decimal value considering binary to decimal conversion from left to right (For example a substring "1011" is treated as 13) Examples : Input : 101Output : 2Explanation : Substring are : 1, 10, 101, 0, 01, 1 In decimal form : 1, 1, 3
9 min read