Given an integer N, the task is to find the smallest and largest N-digit numbers 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 complete 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 numbers is ‘0‘. The idea is that the first digit needs to be as minimal as possible other than 0 which is ‘1’ and the remaining digits need 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++
#include <bits/stdc++.h>
using namespace std;
string findLargest( int N)
{
string largest = string(N, 'F' );
return largest;
}
string findSmallest( int N)
{
string smallest = "1" + string((N - 1), '0' );
return smallest;
}
void print( int largest)
{
cout << "Largest: " << findLargest(largest) << endl;
cout << "Smallest: " << findSmallest(largest) << endl;
}
int main()
{
int N = 4;
print(N);
return 0;
}
|
Java
class GFG {
static String findLargest( int N)
{
String largest = "" ;
for ( int i = 0 ; i < N ; i++)
largest += 'F' ;
return largest;
}
static String findSmallest( int N)
{
String smallest = "1" ;
for ( int i = 0 ; i < N - 1 ; i++)
smallest += '0' ;
return smallest;
}
static void print( int largest)
{
System.out.println( "Largest: " + findLargest(largest)) ;
System.out.println( "Smallest: " + findSmallest(largest)) ;
}
public static void main (String[] args)
{
int N = 4 ;
print(N);
}
}
|
Python3
def findLargest(N) :
largest = 'F' * N
return largest;
def findSmallest(N) :
smallest = '1' + '0' * (N - 1 )
return smallest;
def printAns(largest) :
print ( "Largest: " , findLargest(largest));
print ( "Smallest: " , findSmallest(largest));
if __name__ = = "__main__" :
N = 4 ;
printAns(N);
|
C#
using System;
class GFG {
static string findLargest( int N)
{
string largest = "" ;
for ( int i = 0; i < N ; i++)
largest += 'F' ;
return largest;
}
static string findSmallest( int N)
{
string smallest = "1" ;
for ( int i = 0; i < N - 1; i++)
smallest += '0' ;
return smallest;
}
static void print( int largest)
{
Console.WriteLine( "Largest: " +
findLargest(largest)) ;
Console.WriteLine( "Smallest: " +
findSmallest(largest)) ;
}
public static void Main( string [] args)
{
int N = 4;
print(N);
}
}
|
Javascript
<script>
function findLargest(N)
{
var largest = "F" .repeat(N);
return largest;
}
function findSmallest(N)
{
var smallest = "1" + "0" .repeat(N-1);
return smallest;
}
function print(largest)
{
document.write( "Largest: " + findLargest(largest) + "<br>" );
document.write( "Smallest: " + findSmallest(largest) + "<br>" );
}
var N = 4;
print(N);
</script>
|
Output: Largest: FFFF
Smallest: 1000
Time Complexity: O(N) where N is the length of the string.
Auxiliary Space: O(1)