Count numbers from given range having odd digits at odd places and even digits at even places

Given two integers L and R, the task is to count numbers from the range [L, R] having odd digits at odd positions and even digits at even positions respectively.

Examples:

Input: L = 3, R = 25
Output: 9
Explanation: The numbers satisfying the conditions are 3, 5, 7, 9, 10, 12, 14, 16 and 18.

Input: L = 128, R = 162
Output: 7
Explanation: The numbers satisfying the conditions are 129, 141, 143, 145, 147, 149 and 161.

 

Approach: The given problem can be solved based on the following observations:



It can be observed that every even position has 5 choices {0, 2, 4, 6, 8} and every odd position also has 5 choices {1, 3, 5, 7, 9}. Therefore, there are 5 possibilities for every position. Considering d to be the number of digits in any number satisfying the condition and D to be the number of digits in N, following observations can be made:

  • Case 1: If d < D, then the count of total numbers consisting of d digits satisfying the condition is 5d as every number has 5 choices and numbers made will all be less than N.
  • Case 2: If D = d, then the count of total numbers satisfying the condition of length d is 5d . But the numbers exceeding N needs to be discarded, which is determined by the following: 
    • For even places, if the digit in pth place is x, then (5 — (x / 2 + 1)) * 5(D — p) numbers are greater than N.
    • For odd places, if the digit in pth place is x, then (5 — (x + 1) / 2) * 5(D — p) numbers will be greater than N.

Follow the steps below to solve the problem:

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to implement
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
 
// Function to calculate 5^p
ll getPower(int p)
{
    // Stores the result
    ll res = 1;
 
    // Multiply 5 p times
    while (p--) {
        res *= 5;
    }
 
    // Return the result
    return res;
}
 
// Function to count all numbers upto N
// having odd digits at odd places and
// even digits at even places
ll countNumbersUtil(ll N)
{
    // Stores the count
    ll count = 0;
 
    // Stores the digits of N
    vector<int> digits;
 
    // Insert the digits of N
    while (N) {
        digits.push_back(N % 10);
        N /= 10;
    }
 
    // Reverse the vector to arrange
    // the digits from first to last
    reverse(digits.begin(), digits.end());
 
    // Stores count of digits of n
    int D = digits.size();
 
    for (int i = 1; i <= D; i++) {
 
        // Stores the count of numbers
        // with i digits
        ll res = getPower(i);
 
        // If the last digit is reached,
        // subtract numbers eceeding range
        if (i == D) {
 
            // Iterate over all the places
            for (int p = 1; p <= D; p++) {
 
                // Stores the digit in the pth place
                int x = digits[p - 1];
 
                // Stores the count of numbers
                // having a digit greater than x
                // in the p-th position
                ll tmp = 0;
 
                // Calculate the count of numbers
                // exceeding the range if p is even
                if (p % 2 == 0) {
                    tmp = (5 - (x / 2 + 1))
                          * getPower(D - p);
                }
 
                // Calculate the count of numbers
                // exceeding the range if p is odd
                else {
                    tmp = (5 - (x + 1) / 2)
                          * getPower(D - p);
                }
 
                // Substract the count of numbers
                // exceeding the range from total count
                res -= tmp;
 
                // If the parity of p and the
                // parity of x are not same
                if (p % 2 != x % 2) {
                    break;
                }
            }
        }
 
        // Add count of numbers having i digits
        // and satisfies the given conditions
        count += res;
    }
 
    // Return the total count of numbers till n
    return count;
}
 
// Function to calculate the count of numbers
// from given range having odd digits places
// and even digits at even places
void countNumbers(ll L, ll R)
{
    // Count of numbers in range [L, R] =
    // Count of numbers till R -
    cout << (countNumbersUtil(R)
 
             // Count of numbers till (L-1)
             - countNumbersUtil(L - 1))
         << endl;
}
 
// Driver Code
int main()
{
    ll L = 128, R = 162;
    countNumbers(L, R);
 
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above approach
import java.util.*;
import java.util.Vector;
import java.util.Collections;
 
class GFG{
     
// Function to calculate 5^p
static int getPower(int p)
{
     
    // Stores the result
    int res = 1;
     
    // Multiply 5 p times
    while (p > 0)
    {
        res *= 5;
        p--;
    }
     
    // Return the result
    return res;
}
 
// Function to count all numbers upto N
// having odd digits at odd places and
// even digits at even places
static int countNumbersUtil(int N)
{
     
    // Stores the count
    int count = 0;
     
    // Stores the digits of N
    Vector<Integer> digits = new Vector<Integer>();
     
    // Insert the digits of N
    while (N > 0)
    {
        digits.add(N % 10);
        N /= 10;
    }
 
    // Reverse the vector to arrange
    // the digits from first to last
    Collections.reverse(digits);
 
    // Stores count of digits of n
    int D = digits.size();
 
    for(int i = 1; i <= D; i++)
    {
         
        // Stores the count of numbers
        // with i digits
        int res = getPower(i);
 
        // If the last digit is reached,
        // subtract numbers eceeding range
        if (i == D)
        {
             
            // Iterate over all the places
            for(int p = 1; p <= D; p++)
            {
                 
                // Stores the digit in the pth place
                int x = digits.get(p - 1);
                 
                // Stores the count of numbers
                // having a digit greater than x
                // in the p-th position
                int tmp = 0;
                 
                // Calculate the count of numbers
                // exceeding the range if p is even
                if (p % 2 == 0)
                {
                    tmp = (5 - (x / 2 + 1)) *
                           getPower(D - p);
                }
 
                // Calculate the count of numbers
                // exceeding the range if p is odd
                else
                {
                    tmp = (5 - (x + 1) / 2) *
                       getPower(D - p);
                }
 
                // Substract the count of numbers
                // exceeding the range from total count
                res -= tmp;
 
                // If the parity of p and the
                // parity of x are not same
                if (p % 2 != x % 2)
                {
                    break;
                }
            }
        }
 
        // Add count of numbers having i digits
        // and satisfies the given conditions
        count += res;
    }
 
    // Return the total count of numbers till n
    return count;
}
 
// Function to calculate the count of numbers
// from given range having odd digits places
// and even digits at even places
static void countNumbers(int L, int R)
{
     
    // Count of numbers in range [L, R] =
    // Count of numbers till R -
    System.out.println(countNumbersUtil(R) -
     
                       // Count of numbers till (L-1)
                       countNumbersUtil(L - 1));
}
 
// Driver Code
public static void main(String args[])
{
    int L = 128, R = 162;
     
    countNumbers(L, R);
}
}
 
// This code is contributed by ipg2016107
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement
# the above approach
 
# Function to calculate 5^p
def getPower(p) :
     
    # Stores the result
    res = 1
  
    # Multiply 5 p times
    while (p) :
        res *= 5
        p -= 1
      
    # Return the result
    return res
 
# Function to count anumbers upto N
# having odd digits at odd places and
# even digits at even places
def countNumbersUtil(N) :
     
    # Stores the count
    count = 0
  
    # Stores the digits of N
    digits = []
  
    # Insert the digits of N
    while (N) :
        digits.append(N % 10)
        N //= 10
      
    # Reverse the vector to arrange
    # the digits from first to last
    digits.reverse() 
  
    # Stores count of digits of n
    D = len(digits)
  
    for i in range(1, D + 1, 1) :
  
        # Stores the count of numbers
        # with i digits
        res = getPower(i)
  
        # If the last digit is reached,
        # subtract numbers eceeding range
        if (i == D) :
  
            # Iterate over athe places
            for p in range(1, D + 1, 1) :
  
                # Stores the digit in the pth place
                x = digits[p - 1]
  
                # Stores the count of numbers
                # having a digit greater than x
                # in the p-th position
                tmp = 0
  
                # Calculate the count of numbers
                # exceeding the range if p is even
                if (p % 2 == 0) :
                    tmp = ((5 - (x // 2 + 1))
                            * getPower(D - p))
                 
                # Calculate the count of numbers
                # exceeding the range if p is odd
                else :
                    tmp = ((5 - (x + 1) // 2)
                            * getPower(D - p))
                 
                # Substract the count of numbers
                # exceeding the range from total count
                res -= tmp
  
                # If the parity of p and the
                # parity of x are not same
                if (p % 2 != x % 2) :
                    break
                 
        # Add count of numbers having i digits
        # and satisfies the given conditions
        count += res
     
    # Return the total count of numbers tin
    return count
 
# Function to calculate the count of numbers
# from given range having odd digits places
# and even digits at even places
def countNumbers(L, R) :
     
    # Count of numbers in range [L, R] =
    # Count of numbers tiR -
    print(countNumbersUtil(R)
     
             # Count of numbers ti(L-1)
            - countNumbersUtil(L - 1))
 
# Driver Code
 
L = 128
R = 162
   
countNumbers(L, R) 
 
# This code is contributed by code_hunt
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for
// the above approach
using System;
using System.Collections.Generic;
 
class GFG{
     
// Function to calculate 5^p
static int getPower(int p)
{
     
    // Stores the result
    int res = 1;
     
    // Multiply 5 p times
    while (p > 0)
    {
        res *= 5;
        p--;
    }
     
    // Return the result
    return res;
}
 
// Function to count all numbers upto N
// having odd digits at odd places and
// even digits at even places
static int countNumbersUtil(int N)
{
     
    // Stores the count
    int count = 0;
     
    // Stores the digits of N
    List<int> digits = new List<int>();
     
    // Insert the digits of N
    while (N > 0)
    {
        digits.Add(N % 10);
        N /= 10;
    }
 
    // Reverse the vector to arrange
    // the digits from first to last
    digits.Reverse();
 
    // Stores count of digits of n
    int D = digits.Count;
 
    for(int i = 1; i <= D; i++)
    {
         
        // Stores the count of numbers
        // with i digits
        int res = getPower(i);
 
        // If the last digit is reached,
        // subtract numbers eceeding range
        if (i == D)
        {
             
            // Iterate over all the places
            for(int p = 1; p <= D; p++)
            {
                 
                // Stores the digit in the pth place
                int x = digits[p - 1];
                 
                // Stores the count of numbers
                // having a digit greater than x
                // in the p-th position
                int tmp = 0;
                 
                // Calculate the count of numbers
                // exceeding the range if p is even
                if (p % 2 == 0)
                {
                    tmp = (5 - (x / 2 + 1)) *
                           getPower(D - p);
                }
 
                // Calculate the count of numbers
                // exceeding the range if p is odd
                else
                {
                    tmp = (5 - (x + 1) / 2) *
                       getPower(D - p);
                }
 
                // Substract the count of numbers
                // exceeding the range from total count
                res -= tmp;
 
                // If the parity of p and the
                // parity of x are not same
                if (p % 2 != x % 2)
                {
                    break;
                }
            }
        }
 
        // Add count of numbers having i digits
        // and satisfies the given conditions
        count += res;
    }
 
    // Return the total count of numbers till n
    return count;
}
 
// Function to calculate the count of numbers
// from given range having odd digits places
// and even digits at even places
static void countNumbers(int L, int R)
{
     
    // Count of numbers in range [L, R] =
    // Count of numbers till R -
    Console.WriteLine(countNumbersUtil(R) -
     
                       // Count of numbers till (L-1)
                       countNumbersUtil(L - 1));
}
 
 
// Driver Code
public static void Main(String[] args)
{
    int L = 128, R = 162;
     
    countNumbers(L, R);
}
}
 
// This code is contributed by jana_sayantan
chevron_right

Output: 
7

 

Time Complexity: O(N2), where N is the number of digits in R
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




Article Tags :
Practice Tags :