Sunday, January 12, 2025
Google search engine
HomeData Modelling & AILargest Even and Odd N-digit numbers of base B

Largest Even and Odd N-digit numbers of base B

Given an integer N and base B, the task is to find the largest Even and Odd N-digit numbers of Base B in decimal form.

Examples: 

Input: N = 2, B = 5 
Output: 
Even = 24 
Odd = 23 
Explanation: 
Largest Even Number of 2 digits in base 5 = 44 which is 24 in decimal form. 
Largest Odd Number of 2 digits in base 5 = 43 which is 23 in decimal form.

Input: N = 2, B = 10 
Output: 
Even = 98 
Odd = 99 

Approach: 
To get the largest Even and Odd N-digits number of base B in decimal form is given by: 

  1. If Base B is Even, then: 
    • Largest N-digit even number is (BN – 2).
    • Largest N-digit odd number is (BN – 1).
  2. If Base B is Odd, then: 
    • Largest N-digit Even number is (BN – 1).
    • Largest N-digit Odd number is (BN – 2).

Below is the implementation of the above approach:  

C++




// C++ implementation of the
// above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the largest
// N-digit even and odd numbers
// of base B
void findNumbers(int n, int b)
{
    // Initialise the Number
    int even = 0, odd = 0;
 
    // If Base B is even, then
    // B^n will give largest
    // Even number of N+1 digit
    if (b % 2 == 0) {
 
        // To get even number of
        // N digit subtract 2 from
        // B^n
        even = pow(b, n) - 2;
 
        // To get odd number of
        // N digit subtract 1 from
        // B^n
        odd = pow(b, n) - 1;
    }
 
    // If Base B is odd, then
    // B^n will give largest
    // Odd number of N+1 digit
    else {
 
        // To get even number of
        // N digit subtract 1 from
        // B^n
        even = pow(b, n) - 1;
 
        // To get odd number of
        // N digit subtract 2 from
        // B^n
        odd = pow(b, n) - 2;
    }
    cout << "Even Number = " << even << '\n';
    cout << "Odd Number = " << odd;
}
 
// Driver's Code
int main()
{
    int N = 2, B = 5;
 
    // Function to find the
    // numbers
    findNumbers(N, B);
    return 0;
}


Java




// Java implementation of the
// above approach
import java.util.*;
 
class GFG{
  
// Function to print the largest
// N-digit even and odd numbers
// of base B
static void findNumbers(int n, int b)
{
    // Initialise the Number
    double even = 0, odd = 0;
  
    // If Base B is even, then
    // B^n will give largest
    // Even number of N+1 digit
    if (b % 2 == 0) {
  
        // To get even number of
        // N digit subtract 2 from
        // B^n
        even = Math.pow(b, n) - 2;
  
        // To get odd number of
        // N digit subtract 1 from
        // B^n
        odd = Math.pow(b, n) - 1;
    }
  
    // If Base B is odd, then
    // B^n will give largest
    // Odd number of N+1 digit
    else {
  
        // To get even number of
        // N digit subtract 1 from
        // B^n
        even = Math.pow(b, n) - 1;
  
        // To get odd number of
        // N digit subtract 2 from
        // B^n
        odd = Math.pow(b, n) - 2;
    }
    System.out.println("Even Number = " +  (int)even );
    System.out.print("Odd Number = " +  (int)odd);
}
  
// Driver's Code
public static void main(String[] args)
{
    int N = 2, B = 5;
  
    // Function to find the
    // numbers
    findNumbers(N, B);
}
}
 
// This code is contributed by Rajput-Ji


Python3




# Python implementation of the
# above approach
 
# Function to print the largest
# N-digit even and odd numbers
# of base B
def findNumbers(n, b):
    # Initialise the Number
    even = 0;
    odd = 0;
 
    # If Base B is even, then
    # B^n will give largest
    # Even number of N+1 digit
    if (b % 2 == 0):
 
        # To get even number of
        # N digit subtract 2 from
        # B^n
        even = pow(b, n) - 2;
 
        # To get odd number of
        # N digit subtract 1 from
        # B^n
        odd = pow(b, n) - 1;
     
 
    # If Base B is odd, then
    # B^n will give largest
    # Odd number of N+1 digit
    else:
 
        # To get even number of
        # N digit subtract 1 from
        # B^n
        even = pow(b, n) - 1;
 
        # To get odd number of
        # N digit subtract 2 from
        # B^n
        odd = pow(b, n) - 2;
     
    print("Even Number = ",int(even));
    print("Odd Number = ", int(odd));
 
# Driver's Code
if __name__ == '__main__':
    N = 2;
    B = 5;
 
    # Function to find the
    # numbers
    findNumbers(N, B);
     
# This code is contributed by 29AjayKumar


C#




// C# implementation of the
// above approach
using System;
 
class GFG{
   
// Function to print the largest
// N-digit even and odd numbers
// of base B
static void findNumbers(int n, int b)
{
    // Initialise the Number
    double even = 0, odd = 0;
   
    // If Base B is even, then
    // B^n will give largest
    // Even number of N+1 digit
    if (b % 2 == 0) {
   
        // To get even number of
        // N digit subtract 2 from
        // B^n
        even = Math.Pow(b, n) - 2;
   
        // To get odd number of
        // N digit subtract 1 from
        // B^n
        odd = Math.Pow(b, n) - 1;
    }
   
    // If Base B is odd, then
    // B^n will give largest
    // Odd number of N+1 digit
    else {
   
        // To get even number of
        // N digit subtract 1 from
        // B^n
        even = Math.Pow(b, n) - 1;
   
        // To get odd number of
        // N digit subtract 2 from
        // B^n
        odd = Math.Pow(b, n) - 2;
    }
    Console.WriteLine("Even Number = " +  (int)even );
    Console.Write("Odd Number = " +  (int)odd);
}
   
// Driver's Code
public static void Main(String[] args)
{
    int N = 2, B = 5;
   
    // Function to find the
    // numbers
    findNumbers(N, B);
}
}
 
// This code is contributed by 29AjayKumar


Javascript




<script>
 
// Javascript implementation of the
// above approach
 
// Function to print the largest
// N-digit even and odd numbers
// of base B
function findNumbers(n, b)
{
 
    // Initialise the Number
    var even = 0, odd = 0;
 
    // If Base B is even, then
    // B^n will give largest
    // Even number of N+1 digit
    if (b % 2 == 0) {
 
        // To get even number of
        // N digit subtract 2 from
        // B^n
        even = Math.pow(b, n) - 2;
 
        // To get odd number of
        // N digit subtract 1 from
        // B^n
        odd = Math.pow(b, n) - 1;
    }
 
    // If Base B is odd, then
    // B^n will give largest
    // Odd number of N+1 digit
    else {
 
        // To get even number of
        // N digit subtract 1 from
        // B^n
        even = Math.pow(b, n) - 1;
 
        // To get odd number of
        // N digit subtract 2 from
        // B^n
        odd = Math.pow(b, n) - 2;
    }
    document.write("Even Number = " + even + "<br>");
    document.write("Odd Number = " + odd);
}
 
// Driver's Code
var N = 2, B = 5;
 
// Function to find the
// numbers
findNumbers(N, B);
 
// This code is contributed by rrrtnx.
</script>


Output

Even Number = 24
Odd Number = 23

Time complexity: O(logN) as an inbuilt pow() function is being used
Auxiliary space: O(1), If we consider a recursive call stack then it would be O(log(n))

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments