Largest and Smallest N-digit Octal Numbers
Last Updated :
20 Dec, 2022
Given an integer N, the task is to find the smallest and largest N-digit numbers in Octal Number System.
Examples:
Input: N = 4
Output:
Largest: 7777
Smallest: 1000
Input: N = 2
Output:
Largest: 77
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 Octal number system is ‘7‘. Therefore:
1 Digit Largest Number: '7'
2 Digit Largest Number: '77'
3 Digit Largest Number: '777'
.
.
.
N Digit Largest Number: '777....(N) times'
- Smallest Number: The smallest number in Octal 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++
#include <bits/stdc++.h>
using namespace std;
string findLargest( int N)
{
string largest = string(N, '7' );
return largest;
}
string findSmallest( int N)
{
string smallest
= "1"
+ string((N - 1), '0' );
return smallest;
}
void printLargestSmallest( int N)
{
cout << "Largest: "
<< findLargest(N) << endl;
cout << "Smallest: "
<< findSmallest(N) << endl;
}
int main()
{
int N = 4;
printLargestSmallest(N);
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class GFG
{
static String findLargest( int N)
{
String largest = strings(N, '7' );
return largest;
}
static String findSmallest( int N)
{
String smallest
= "1"
+ strings((N - 1 ), '0' );
return smallest;
}
private static String strings( int N, char c) {
String temp = "" ;
for ( int i= 0 ; i < N; i++) {
temp+=c;
}
return temp;
}
static void printLargestSmallest( int N)
{
System.out.print( "Largest: "
+ findLargest(N) + "\n" );
System.out.print( "Smallest: "
+ findSmallest(N) + "\n" );
}
public static void main(String[] args)
{
int N = 4 ;
printLargestSmallest(N);
}
}
|
Python3
def findLargest(N):
largest = strings(N, '7' );
return largest;
def findSmallest(N):
smallest = "1" + strings((N - 1 ), '0' );
return smallest;
def strings(N, c):
temp = "";
for i in range (N):
temp + = c;
return temp;
def printLargestSmallest(N):
print ( "Largest: " ,findLargest(N));
print ( "Smallest: " ,findSmallest(N));
if __name__ = = '__main__' :
N = 4 ;
printLargestSmallest(N);
|
C#
using System;
class GFG
{
static String findLargest( int N)
{
String largest = strings(N, '7' );
return largest;
}
static String findSmallest( int N)
{
String smallest
= "1"
+ strings((N - 1), '0' );
return smallest;
}
private static String strings( int N, char c) {
String temp = "" ;
for ( int i= 0; i < N; i++) {
temp+=c;
}
return temp;
}
static void printLargestSmallest( int N)
{
Console.Write( "Largest: "
+ findLargest(N) + "\n" );
Console.Write( "Smallest: "
+ findSmallest(N) + "\n" );
}
public static void Main(String[] args)
{
int N = 4;
printLargestSmallest(N);
}
}
|
Javascript
<script>
function findLargest(N)
{
var largest = new Array(N+1).join( '7' );
return largest;
}
function findSmallest(N)
{
var smallest
= "1"
+ new Array(N).join( '0' );
return smallest;
}
function printLargestSmallest(N)
{
document.write( "Largest: "
+ findLargest(N) + "<br>" );
document.write( "Smallest: "
+ findSmallest(N));
}
var N = 4;
printLargestSmallest(N);
</script>
|
Output:
Largest: 7777
Smallest: 1000
Time Complexity: O(N) where N is the length of the string.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...