Open In App

Count rotations of N which are Odd and Even

Improve
Improve
Like Article
Like
Save
Share
Report

Given a number n, the task is to count all rotations of the given number which are odd and even. 
Examples: 
 

Input: n = 1234
Output: Odd = 2, Even = 2
Total rotations: 1234, 2341, 3412, 4123
Odd rotations: 2341 and 4123
Even rotations: 1234 and 3412
Input: n = 246
Output: Odd = 0, Even = 3

 

Brute force approach:

The brute force approach is very simple .

The steps are as follows:

1.Convert the number to a string.
2.Loop through all possible rotations of the string.
3.Check if the current rotation is odd or even.
4.Increment the count of odd or even rotations accordingly.
5.Print the counts.

C++




#include <bits/stdc++.h>
using namespace std;
void countoddrotation(int n)
{
 
     string s = to_string(n);
    int len = s.length();
    int count_odd = 0, count_even = 0;
 
    for (int i = 0; i < len; i++) {
        string temp = s.substr(i) + s.substr(0, i);
        int x = stoi(temp);
        if (x % 2 == 0) {
            count_even++;
        } else {
            count_odd++;
        }
    }
        cout << "Odd = "<< count_odd<<endl<<"Even = "<< count_even;
}
int main() {
    int n = 1234;
   countoddrotation(n);
    return 0;
}


Java




import java.util.*;
 
public class Main {
    static void countOddRotation(int n) {
        String s = Integer.toString(n);
        int len = s.length();
        int countOdd = 0, countEven = 0;
 
        for (int i = 0; i < len; i++) {
            String temp = s.substring(i) + s.substring(0, i);
            int x = Integer.parseInt(temp);
            if (x % 2 == 0) {
                countEven++;
            } else {
                countOdd++;
            }
        }
         
        System.out.println("Odd = " + countOdd);
        System.out.println("Even = " + countEven);
    }
 
    public static void main(String[] args) {
        int n = 1234;
        countOddRotation(n);
    }
}


Python3




# function to count the number of odd and even numbers in a rotated number
def count_odd_rotation(n):
    # Convert the input number to a string to manipulate its digits
    s = str(n)
    len_s = len(s)  # Get the length of the string
    count_odd = 0   # Initialize a counter for odd numbers
    count_even = 0  # Initialize a counter for even numbers
 
    # Iterate through all possible rotations of the number
    for i in range(len_s):
        # Create a rotated string by slicing and reordering the original string
        temp = s[i:] + s[:i]
        x = int(temp)  # Convert the rotated string back to an integer
 
        # Check if the rotated number is even or odd
        if x % 2 == 0:
            count_even += 1  # Increment the even count if it's even
        else:
            count_odd += 1   # Increment the odd count if it's odd
 
    # Print the counts of odd and even numbers
    print("Odd =", count_odd)
    print("Even =", count_even)
 
if __name__ == "__main__":
     
    n = 1234
    count_odd_rotation(n)


C#




// C# implementation for the approach
using System;
public class GFG {
 
    // Function to count of all rotations
    // which are odd and even
    static void CountOddRotation(int n)
    {
        // Convert the integer to a string to manipulate its
        // digits
        string s = n.ToString();
        int len = s.Length;
        int countOdd = 0, countEven = 0;
 
        for (int i = 0; i < len; i++) {
 
            // Create a rotated string by moving the first
            // 'i' digits to the end
            string temp
                = s.Substring(i) + s.Substring(0, i);
 
            // Convert the rotated string back to an integer
            int x = int.Parse(temp);
            if (x % 2 == 0) {
                countEven++; // Count even rotations
            }
            else {
                countOdd++; // Count odd rotations
            }
        }
 
        Console.WriteLine("Odd = " + countOdd);
        Console.WriteLine("Even = " + countEven);
    }
 
    public static void Main(string[] args)
    {
        int n = 1234;
 
        // Call the function
        CountOddRotation(n);
    }
}


Javascript




// Function to count the number of odd and even rotations of an integer
function countOddRotation(n) {
    // Convert the integer to a string to manipulate its digits
    const s = n.toString();
    const len = s.length; // Get the length of the number as a string
    let countOdd = 0; // Initialize a counter for odd rotations
    let countEven = 0; // Initialize a counter for even rotations
 
    // Iterate through all possible rotations of the number
    for (let i = 0; i < len; i++) {
        // Create a rotated version of the number by shifting digits
        const temp = s.substring(i) + s.substring(0, i);
        const x = parseInt(temp); // Convert the rotated string back to an integer
 
        // Check if the rotated number is even or odd
        if (x % 2 === 0) {
            countEven++; // Increment the count for even rotations
        } else {
            countOdd++; // Increment the count for odd rotations
        }
    }
 
    // Display the counts of odd and even rotations
    console.log("Odd = " + countOdd);
    console.log("Even = " + countEven);
}
 
// Driver Code
const n = 1234;
countOddRotation(n);


Output:

Odd = 2
Even = 2

Time Complexity: O(n*n)
Auxiliary Space: O(n)

Efficient Approach: For large numbers, it is difficult to rotate and check whether it is odd or not for every rotation. Hence, in this approach, check the count of odd digits and even digits present in the number. These will be the answer to this problem.

Below is the implementation of the above approach:

Implementation:

C++




// C++ implementation of the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to count of all rotations
// which are odd and even
void countOddRotations(int n)
{
    int odd_count = 0, even_count = 0;
    do {
        int digit = n % 10;
        if (digit % 2 == 1)
            odd_count++;
        else
            even_count++;
        n = n / 10;
    } while (n != 0);
 
    cout << "Odd = " << odd_count << endl;
    cout << "Even = " << even_count << endl;
}
 
// Driver Code
int main()
{
    int n = 1234;
    countOddRotations(n);
}


Java




// Java implementation of the above approach
 
class Solution {
 
    // Function to count of all rotations
    // which are odd and even
    static void countOddRotations(int n)
    {
        int odd_count = 0, even_count = 0;
        do {
            int digit = n % 10;
            if (digit % 2 == 1)
                odd_count++;
            else
                even_count++;
            n = n / 10;
        } while (n != 0);
 
        System.out.println("Odd = " + odd_count);
        System.out.println("Even = " + even_count);
    }
 
    public static void main(String[] args)
    {
        int n = 1234;
        countOddRotations(n);
    }
}


Python3




# Python implementation of the above approach
 
# Function to count of all rotations
# which are odd and even
def countOddRotations(n):
    odd_count = 0; even_count = 0
    while n != 0:
        digit = n % 10
        if digit % 2 == 0:
            odd_count += 1
        else:
            even_count += 1
        n = n//10
    print("Odd =", odd_count)
    print("Even =", even_count)
 
# Driver code
n = 1234
countOddRotations(n)
 
# This code is contributed by Shrikant13


C#




// CSharp implementation of the above approach
 
using System;
class Solution {
 
    // Function to count of all rotations
    // which are odd and even
    static void countOddRotations(int n)
    {
        int odd_count = 0, even_count = 0;
        do {
            int digit = n % 10;
            if (digit % 2 == 1)
                odd_count++;
            else
                even_count++;
            n = n / 10;
        } while (n != 0);
 
        Console.WriteLine("Odd = " + odd_count);
        Console.WriteLine("Even = " + even_count);
    }
 
    public static void Main()
    {
        int n = 1234;
        countOddRotations(n);
    }
}


Javascript




<script>
 
// Javascript implementation of the above approach
 
// Function to count of all rotations
// which are odd and even
function countOddRotations(n)
{
    var odd_count = 0, even_count = 0;
    do {
        var digit = n % 10;
        if (digit % 2 == 1)
            odd_count++;
        else
            even_count++;
        n = parseInt(n / 10);
    } while (n != 0);
 
    document.write("Odd = " + odd_count + "<br>");
    document.write("Even = " + even_count + "<br>");
}
 
// Driver Code
var n = 1234;
countOddRotations(n);
 
// This code is contributed by rutvik_56.
 
</script>


PHP




<?php
// PHP implementation of the above approach
 
// Function to count of all rotations
// which are odd and even
function countOddRotations($n)
{
    $odd_count = 0;
    $even_count = 0;
    do {
        $digit = $n % 10;
        if ($digit % 2 == 1)
            $odd_count++;
        else
            $even_count++;
        $n = (int)($n / 10);
    } while ($n != 0);
 
    echo "Odd = ", $odd_count, "\n";
    echo "Even = ", $even_count, "\n";
}
 
// Driver Code
$n = 1234;
countOddRotations($n);
 
// This code is contributed by ajit..
?>


Output

Odd = 2
Even = 2




Time Complexity: O(log10n)
Auxiliary Space: O(1)



Last Updated : 16 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads