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++
// 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 ?> |
5
Recommended Posts:
- Maximum contiguous 1 possible in a binary string after k rotations
- Generate all rotations of a given string
- Minimum rotations required to get the same string
- Minimum rotations required to get the same String | Set-2
- Longest sub string of 0's in a binary string which is repeated K times
- Check if strings are rotations of each other or not | Set 2
- Count rotations divisible by 4
- Check if two numbers are bit rotations of each other or not
- Binary Search a String
- A Program to check if strings are rotations of each other or not
- Count of substrings of a binary string containing K ones
- Calculate score for the given binary string
- Check if binary string multiple of 3 using DFA
- Modulo of a large Binary String
- Check divisibility of binary string by 2^k
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.



