Thursday, July 4, 2024
HomeData ModellingData Structure & AlgorithmSmallest and Largest sum of two n-digit numbers

Smallest and Largest sum of two n-digit numbers

Given an integer N ? 1, the task is to find the smallest and the largest sum of two N digit numbers.
Examples: 
 

Input: N = 1 
Output: 
Largest = 18 
Smallest = 0 
Largest 1-digit number is 9 and 9 + 9 = 18 
Smallest 1-digit number is 0 and 0 + 0 = 0
Input: N = 2 
Output: 
Largest = 198 
Smallest = 20 
 

 

Approach: 
 

  • For largest: The answer will be 2 * (10N – 1) because the series of sum of two n digit numbers will go on like 2 * 9, 2 * 99, 2 * 999, …
  • For smallest: 
    • If N = 1 then the answer will be 0.
    • If N > 1 then the answer will be 2 * (10N – 1) because the series of sum of two n digit numbers will go on like 0, 20, 200, 2000, …

Below is the implementation of the above approach:
 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the smallest sum
// of 2 n-digit numbers
int smallestSum(int n)
{
    if (n == 1)
        return 0;
    return (2 * pow(10, n - 1));
}
 
// Function to return the largest sum
// of 2 n-digit numbers
int largestSum(int n)
{
    return (2 * (pow(10, n) - 1));
}
 
// Driver code
int main()
{
    int n = 4;
    cout << "Largest = " << largestSum(n) << endl;
    cout << "Smallest = " << smallestSum(n);
 
    return 0;
}


Java




// Java implementation of the approach
class GFG {
 
    // Function to return the smallest sum
    // of 2 n-digit numbers
    static int smallestSum(int n)
    {
        if (n == 1)
            return 0;
        return (2 * (int)Math.pow(10, n - 1));
    }
 
    // Function to return the largest sum
    // of 2 n-digit numbers
    static int largestSum(int n)
    {
        return (2 * ((int)Math.pow(10, n) - 1));
    }
 
    // Driver code
    public static void main(String args[])
    {
        int n = 4;
        System.out.println("Largest = " + largestSum(n));
        System.out.print("Smallest = " + smallestSum(n));
    }
}


Python3




# Python3 implementation of the approach
 
# Function to return the smallest sum
# of 2 n-digit numbers
def smallestSum(n):
  
    if (n == 1):
        return 0
    return (2 * pow(10, n - 1))
 
# Function to return the largest sum
# of 2 n-digit numbers
def largestSum(n):
    return (2 * (pow(10, n) - 1))
 
# Driver code
n = 4
print("Largest = ", largestSum(n))
print("Smallest = ", smallestSum(n))


C#




// C# implementation of the approach
using System;
class GFG {
 
    // Function to return the smallest sum
    // of 2 n-digit numbers
    static int smallestSum(int n)
    {
        if (n == 1)
            return 0;
        return (2 * (int)Math.Pow(10, n - 1));
    }
 
    // Function to return the largest sum
    // of 2 n-digit numbers
    static int largestSum(int n)
    {
        return (2 * ((int)Math.Pow(10, n) - 1));
    }
 
    // Driver code
    public static void Main()
    {
        int n = 4;
        Console.WriteLine("Largest = " + largestSum(n));
        Console.Write("Smallest = " + smallestSum(n));
    }
}


PHP




<?php
// PHP implementation of the approach
 
// Function to return the smallest sum
// of 2 n-digit numbers
function smallestSum($n)
{
    if ($n == 1)
        return 0;
    return (2 * pow(10, $n - 1));
}
  
// Function to return the largest sum
// of 2 n-digit numbers
function largestSum($n)
{
    return 2 * ( pow(10, $n) - 1 );
}
 
// Driver code
$n = 4;
echo "Largest = " . largestSum($n) . "\n";
echo "Smallest = " . smallestSum($n);
?>


Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to return the smallest sum
// of 2 n-digit numbers
function smallestSum(n)
{
    if (n == 1)
        return 0;
    return (2 * Math.pow(10, n - 1));
}
 
// Function to return the largest sum
// of 2 n-digit numbers
function largestSum(n)
{
    return (2 * (Math.pow(10, n) - 1));
}
 
// Driver code
var n = 4;
document.write("Largest = " + largestSum(n) + "<br>");
document.write("Smallest = " + smallestSum(n));
 
// This code is contributed by noob2000.
</script>


Output: 

Largest = 19998
Smallest = 2000

 

Time Complexity: O(log n)

Auxiliary Space: O(1)

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!

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments