Open in App
Not now

# Find the number of positive integers less than or equal to N that have an odd number of digits

• Difficulty Level : Basic
• Last Updated : 20 Dec, 2022

Given an integer N where 1 â‰¤ N â‰¤ 105, the task is to find the number of positive integers less than or equal to N that have an odd number of digits without leading zeros.
Examples:

Input: N = 11
Output:
1, 2, 3, …, 8 and 9 are the numbers â‰¤ 11
with odd number of digits.

Input: N = 893
Output: 803

Naive approach: Traverse from 1 to N and for each number check if it contains odd digits or not.
Efficient approach: For the values:

• When N < 10 then the count of valid numbers will be N.
• When N / 10 < 10 then 9.
• When N / 100 < 10 then 9 + N – 99.
• When N / 1000 < 10 then 9 + 900.
• When N / 10000 < 10 then 909 + N – 9999.
• Otherwise 90909.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach` `#include ` `using` `namespace` `std;`   `// Function to return the number of` `// positive integers less than or equal` `// to N that have odd number of digits` `int` `odd_digits(``int` `n)` `{` `    ``if` `(n < 10)` `        ``return` `n;` `    ``else` `if` `(n / 10 < 10)` `        ``return` `9;` `    ``else` `if` `(n / 100 < 10)` `        ``return` `9 + n - 99;` `    ``else` `if` `(n / 1000 < 10)` `        ``return` `9 + 900;` `    ``else` `if` `(n / 10000 < 10)` `        ``return` `909 + n - 9999;` `    ``else` `        ``return` `90909;` `}`   `// Driver code` `int` `main()` `{` `    ``int` `n = 893;`   `    ``cout << odd_digits(n);`   `    ``return` `0;` `}`

## Java

 `// Java implementation of the approach` `import` `java.io.*;` `public` `class` `GFG ` `{`   `// Function to return the number of` `// positive integers less than or equal` `// to N that have odd number of digits` `static` `int` `odd_digits(``int` `n)` `{` `    ``if` `(n < ``10``)` `        ``return` `n;` `    ``else` `if` `(n / ``10` `< ``10``)` `        ``return` `9``;` `    ``else` `if` `(n / ``100` `< ``10``)` `        ``return` `9` `+ n - ``99``;` `    ``else` `if` `(n / ``1000` `< ``10``)` `        ``return` `9` `+ ``900``;` `    ``else` `if` `(n / ``10000` `< ``10``)` `        ``return` `909` `+ n - ``9999``;` `    ``else` `        ``return` `90909``;` `}`   `// Driver code` `public` `static` `void` `main(String []args) ` `{` `    ``int` `n = ``893``;`   `    ``System.out.println(odd_digits(n));` `}` `}`   `// This code is contributed by 29AjayKumar`

## Python3

 `# Python3 implementation of the approach `   `# Function to return the number of ` `# positive integers less than or equal ` `# to N that have odd number of digits ` `def` `odd_digits(n) :`   `    ``if` `(n < ``10``) :` `        ``return` `n; ` `    ``elif` `(n ``/` `10` `< ``10``) :` `        ``return` `9``; ` `    ``elif` `(n ``/` `100` `< ``10``) :` `        ``return` `9` `+` `n ``-` `99``; ` `    ``elif` `(n ``/` `1000` `< ``10``) :` `        ``return` `9` `+` `900``; ` `    ``elif` `(n ``/` `10000` `< ``10``) : ` `        ``return` `909` `+` `n ``-` `9999``; ` `    ``else` `:` `        ``return` `90909``; `   `# Driver code ` `if` `__name__ ``=``=` `"__main__"` `: `   `    ``n ``=` `893``; `   `    ``print``(odd_digits(n)); `   `# This code is contributed by AnkitRai01`

## C#

 `// C# implementation of the approach` `using` `System;` `                    `  `class` `GFG ` `{`   `// Function to return the number of` `// positive integers less than or equal` `// to N that have odd number of digits` `static` `int` `odd_digits(``int` `n)` `{` `    ``if` `(n < 10)` `        ``return` `n;` `    ``else` `if` `(n / 10 < 10)` `        ``return` `9;` `    ``else` `if` `(n / 100 < 10)` `        ``return` `9 + n - 99;` `    ``else` `if` `(n / 1000 < 10)` `        ``return` `9 + 900;` `    ``else` `if` `(n / 10000 < 10)` `        ``return` `909 + n - 9999;` `    ``else` `        ``return` `90909;` `}`   `// Driver code` `public` `static` `void` `Main(String []args) ` `{` `    ``int` `n = 893;`   `    ``Console.WriteLine(odd_digits(n));` `}` `}`   `// This code is contributed by 29AjayKumar`

## Javascript

 ``

Output:

`803`

Time complexity: O(1)
Auxiliary space: O(1)

My Personal Notes arrow_drop_up
Related Articles