Largest and Smallest N-digit Hexadecimal Numbers

Given an integer N, the task is to find the smallest and largest N-digit numbers in Hexa-Decimal Number System.

Examples:

Input: N = 4
Output:
Largest: FFFF
Smallest: 1000

Input: N = 2
Output:
Largest: FF
Smallest: 10

Approach: The following steps can be followed to compute the required answer:



  • Largest Number: To get the largest number, every digit of the number must be maximum. The maximum digit in the Hexa-Decimal number system is ‘F‘. Therefore:
    1 Digit Largest Number: 'F'
    2 Digit Largest Number: 'FF'
    3 Digit Largest Number: 'FFF'
                    .
                    .
                    .
    N Digit Largest Number: 'FFF....(N) times'
    
  • Smallest Number: The smallest number in hexadecimal number is ‘0‘. The idea is that the first digit needs to be as minimum as possible other than 0 which is ‘1’ and the remaining digits needs to be 0. Therefore:
    1 Digit Smallest Number: '1'
    2 Digit Smallest Number: '10'
    3 Digit Smallest Number: '100'
                    .
                    .
                    .
    N Digit Smallest Number: '100....(N - 1) times'
    
Below is the implementation of the above approach:

C++

filter_none
edit close
play_arrow
link brightness_4 code
// C++ program to find the largest
// and smallest N-digit numbers
// in Hexa-Decimal Number System
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the largest
// N-digit number in Hexa-Decimal
// Number System
string findLargest(int N)
{
    // Append 'F' N times
    string largest = string(N, 'F');
  
    return largest;
}
  
// Function to return the smallest
// N-digit number in Hexa-Decimal
// Number System
string findSmallest(int N)
{
    // Append '0' (N - 1) times to 1
    string smallest = "1" + string((N - 1), '0');
  
    return smallest;
}
  
// Function to print the largest and smallest
// N-digit Hexa-Decimal number
void print(int largest)
{
    cout << "Largest: " << findLargest(largest) << endl;
    cout << "Smallest: " << findSmallest(largest) << endl;
}
  
// Driver code
int main()
{
  
    int N = 4;
    print(N);
  
    return 0;
}
chevron_right

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the largest
// and smallest N-digit numbers
// in Hexa-Decimal Number System
class GFG {
      
    // Function to return the largest
    // N-digit number in Hexa-Decimal
    // Number System
    static String findLargest(int N)
    {
          
        String largest = "";
        // Append 'F' N times
        for (int i = 0; i < N ; i++)
            largest += 'F';
      
        return largest;
    }
      
    // Function to return the smallest
    // N-digit number in Hexa-Decimal
    // Number System
    static String findSmallest(int N)
    {
          
        String smallest = "1" ;
        // Append '0' (N - 1) times to 1
        for(int i = 0; i < N - 1; i++)
            smallest += '0';
      
        return smallest;
    }
      
    // Function to print the largest and smallest
    // N-digit Hexa-Decimal number
    static void print(int largest)
    {
        System.out.println("Largest: " + findLargest(largest)) ;
        System.out.println("Smallest: " + findSmallest(largest)) ;
    }
      
    // Driver code
    public static void main (String[] args)
    {
      
        int N = 4;
        print(N);
      
    }
}
  
// This code is contributed by AnkitRai01

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the largest 
# and smallest N-digit numbers 
# in Hexa-Decimal Number System 
  
# Function to return the largest 
# N-digit number in Hexa-Decimal 
# Number System 
def findLargest(N) : 
  
    # Append 'F' N times 
    largest = 'F'*N
  
    return largest; 
  
# Function to return the smallest 
# N-digit number in Hexa-Decimal 
# Number System 
def findSmallest(N) : 
  
    # Append '0' (N - 1) times to 1 
    smallest = '1' + '0'*(N - 1)
  
    return smallest; 
  
# Function to print the largest and smallest 
# N-digit Hexa-Decimal number 
def printAns(largest) :
  
    print("Largest: " , findLargest(largest)); 
    print("Smallest: " , findSmallest(largest)); 
  
# Driver code 
if __name__ == "__main__"
  
    N = 4
    printAns(N); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the largest
// and smallest N-digit numbers
// in Hexa-Decimal Number System
using System;
  
class GFG {
      
    // Function to return the largest
    // N-digit number in Hexa-Decimal
    // Number System
    static string findLargest(int N)
    {
          
        string largest = "";
          
        // Append 'F' N times
        for (int i = 0; i < N ; i++)
            largest += 'F';
      
        return largest;
    }
      
    // Function to return the smallest
    // N-digit number in Hexa-Decimal
    // Number System
    static string findSmallest(int N)
    {
          
        string smallest = "1" ;
          
        // Append '0' (N - 1) times to 1
        for(int i = 0; i < N - 1; i++)
            smallest += '0';
      
        return smallest;
    }
      
    // Function to print the largest and smallest
    // N-digit Hexa-Decimal number
    static void print(int largest)
    {
        Console.WriteLine("Largest: "
                    findLargest(largest)) ;
        Console.WriteLine("Smallest: "
                    findSmallest(largest)) ;
    }
      
    // Driver code
    public static void Main(string[] args)
    {
        int N = 4;
        print(N);
    }
}
  
// This code is contributed by AnkitRai01

chevron_right


Output:

Largest: FFFF
Smallest: 1000

Time Complexity: O(N) where N is the length of the string.

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : AnkitRai01