The Wayback Machine - https://web.archive.org/web/20240927192544/https://www.geeksforgeeks.org/find-the-largest-number-with-given-number-of-digits-and-sum-of-digits/
Open In App

Find the Largest number with given number of digits and sum of digits

Last Updated : 18 Apr, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Companies:
Show Topics
Solve Problem
Easy
20.26%
1.4L

Given an integer s and d, The task is to find the largest number with given digit sum s and the number of digits d.

Examples: 

Input: s = 9, d = 2
Output: 90

Input: s = 20, d = 3
Output: 992

Recommended Practice

Naive Approach:

Consider all m digit numbers and keep a max variable to store the maximum number with m digits and digit sum as s. 

Time complexity: O(10m).
Auxiliary Space: O(1)

Find the Largest number with the given number of digits and sum of digits Greedy approach 

Below is the idea to solve the problem:

The idea is to one by one fill all digits from leftmost to rightmost compare the remaining sum with 9 if the remaining sum is more than or equal to 9, 9 at the current position, else put the remaining sum. Since digits are filled from left to right, the highest digits will be placed on the left side, hence get the largest number y.

Illustration: 

Follow the below steps to Implement the idea:

  • If s is zero 
    • if m=1 print 0
    • Else no such number is possible.
  • If s > 9*m then no such number is possible.
  • Run a for loop from 0 to m-1
    • If s>=9 subtract 9 from s and print 9.
    • Else print s and set s to 0.

Below is the Implementation of the above approach:

C++




// C++ program to find the largest number that can be
// formed from given sum of digits and number of digits.
#include <iostream>
using namespace std;
 
// Prints the smallest possible number with digit sum 's'
// and 'm' number of digits.
void findLargest(int m, int s)
{
    // If sum of digits is 0, then a number is possible
    // only if number of digits is 1.
    if (s == 0) {
        (m == 1) ? cout << "Largest number is " << 0
                 : cout << "Not possible";
        return;
    }
 
    // Sum greater than the maximum possible sum.
    if (s > 9 * m) {
        cout << "Not possible";
        return;
    }
 
    // Create an array to store digits of result
    int res[m];
 
    // Fill from most significant digit to least
    // significant digit.
    for (int i = 0; i < m; i++) {
        // Fill 9 first to make the number largest
        if (s >= 9) {
            res[i] = 9;
            s -= 9;
        }
 
        // If remaining sum becomes less than 9, then
        // fill the remaining sum
        else {
            res[i] = s;
            s = 0;
        }
    }
 
    cout << "Largest number is ";
    for (int i = 0; i < m; i++)
        cout << res[i];
}
 
// Driver code
int main()
{
    int s = 9, m = 2;
    findLargest(m, s);
    return 0;
}


C




// C program to find the largest number that can be
// formed from given sum of digits and number of digits.
#include <stdio.h>
 
// Prints the smallest possible number with digit sum 's'
// and 'm' number of digits.
void findLargest(int m, int s)
{
    // If sum of digits is 0, then a number is possible
    // only if number of digits is 1.
    if (s == 0) {
        (m == 1) ? printf("Largest number is 0")
                 : printf("Not possible");
        return;
    }
 
    // Sum greater than the maximum possible sum.
    if (s > 9 * m) {
        printf("Not possible");
        return;
    }
 
    // Create an array to store digits of result
    int res[m];
 
    // Fill from most significant digit to least
    // significant digit.
    for (int i = 0; i < m; i++) {
        // Fill 9 first to make the number largest
        if (s >= 9) {
            res[i] = 9;
            s -= 9;
        }
 
        // If remaining sum becomes less than 9, then
        // fill the remaining sum
        else {
            res[i] = s;
            s = 0;
        }
    }
 
    printf("Largest number is ");
    for (int i = 0; i < m; i++)
        printf("%d", res[i]);
}
 
// Driver code
int main()
{
    int s = 9, m = 2;
    findLargest(m, s);
    return 0;
}
 
// This code is contributed by Sania Kumari Gupta


Java




// Java program to find the largest number that can be
// formed from given sum of digits and number of digits
 
class GFG
{
    // Function to print the largest possible number with digit sum 's'
    // and 'm' number of digits
    static void findLargest(int m, int s)
    {
        // If sum of digits is 0, then a number is possible
        // only if number of digits is 1
        if (s == 0)
        {
            System.out.print(m == 1 ? "Largest number is 0" : "Not possible");
             
            return ;
        }
  
        // Sum greater than the maximum possible sum
        if (s > 9*m)
        {
            System.out.println("Not possible");
            return ;
        }
  
        // Create an array to store digits of result
        int[] res = new int[m];
  
        // Fill from most significant digit to least
        // significant digit
        for (int i=0; i<m; i++)
        {
            // Fill 9 first to make the number largest
            if (s >= 9)
            {
                res[i] = 9;
                s -= 9;
            }
  
            // If remaining sum becomes less than 9, then
            // fill the remaining sum
            else
            {
                res[i] = s;
                s = 0;
            }
        }
  
        System.out.print("Largest number is ");
        for (int i=0; i<m; i++)
            System.out.print(res[i]);
    }
     
    // driver program
    public static void main (String[] args)
    {
        int s = 9, m = 2;
        findLargest(m, s);
    }
}
 
// Contributed by Pramod Kumar


Python3




# Python 3 program to find
# the largest number that
# can be formed from given
# sum of digits and number
# of digits.
 
 
# Prints the smallest
# possible number with digit
# sum 's' and 'm' number of
# digits.
def findLargest( m, s) :
 
    # If sum of digits is 0,
    # then a number is possible
    # only if number of digits
    # is 1.
    if (s == 0) :
     
        if(m == 1) :
            print("Largest number is " , "0",end = "")
        else :
            print("Not possible",end = "")
     
        return
 
    # Sum greater than the
    # maximum possible sum.
    if (s > 9 * m) :
        print("Not possible",end = "")
        return
     
    # Create an array to
    # store digits of
    # result
    res = [0] * m
 
    # Fill from most significant
    # digit to least significant
    # digit.
    for i in range(0, m) :
         
        # Fill 9 first to make
        # the number largest
        if (s >= 9) :
            res[i] = 9
            s = s - 9
         
        # If remaining sum
        # becomes less than
        # 9, then fill the
        # remaining sum
        else :
            res[i] = s
            s = 0
         
         
    print( "Largest number is ",end = "")
     
    for i in range(0, m) :
        print(res[i],end = "")
 
# Driver code
s = 9
m = 2
findLargest(m, s)
 
# This code is contributed by Nikita Tiwari.


C#




// C# program to find the
// largest number that can
// be formed from given sum
// of digits and number of digits
using System;
 
class GFG
{
     
    // Function to print the 
    // largest possible number
    // with digit sum 's' and
    // 'm' number of digits
    static void findLargest(int m, int s)
    {
        // If sum of digits is 0,
        // then a number is possible
        // only if number of digits is 1
        if (s == 0)
        {
            Console.Write(m == 1 ?
                   "Largest number is 0" :
                          "Not possible");
             
            return ;
        }
 
        // Sum greater than the
        // maximum possible sum
        if (s > 9 * m)
        {
            Console.WriteLine("Not possible");
            return ;
        }
 
        // Create an array to
        // store digits of result
        int []res = new int[m];
 
        // Fill from most significant
        // digit to least significant digit
        for (int i = 0; i < m; i++)
        {
            // Fill 9 first to make
            // the number largest
            if (s >= 9)
            {
                res[i] = 9;
                s -= 9;
            }
 
            // If remaining sum becomes
            // less than 9, then
            // fill the remaining sum
            else
            {
                res[i] = s;
                s = 0;
            }
        }
 
        Console.Write("Largest number is ");
        for (int i = 0; i < m; i++)
            Console.Write(res[i]);
    }
     
    // Driver Code
    static public void Main ()
    {
        int s = 9, m = 2;
        findLargest(m, s);
    }
}
 
// This code is Contributed by ajit


PHP




<?php
// PHP program to find the largest
// number that can be formed from
// given sum of digits and number
// of digits.
 
// Prints the smallest possible
// number with digit sum 's'
// and 'm' number of digits.
function findLargest($m, $s)
{
    // If sum of digits is 0, then
    // a number is possible only if
    // number of digits is 1.
    if ($s == 0)
    {
        if(($m == 1) == true)
            echo "Largest number is " , 0;
        else
            echo "Not possible";
        return ;
    }
 
    // Sum greater than the
    // maximum possible sum.
    if ($s > 9 * $m)
    {
        echo "Not possible";
        return ;
    }
 
    // Create an array to store
    // digits of result Fill from
    // most significant digit to
    // least significant digit.
    for ($i = 0; $i < $m; $i++)
    {
        // Fill 9 first to make
        // the number largest
        if ($s >= 9)
        {
            $res[$i] = 9;
            $s -= 9;
        }
 
        // If remaining sum becomes
        // less than 9, then fill
        // the remaining sum
        else
        {
            $res[$i] = $s;
            $s = 0;
        }
    }
 
    echo "Largest number is ";
    for ($i = 0; $i < $m; $i++)
        echo $res[$i];
}
 
// Driver code
$s = 9; $m = 2;
findLargest($m, $s);
 
// This code is contributed by m_kit
?>


Javascript




<script>
// Javascript program to find the largest number that can be
// formed from given sum of digits and number of digits.
 
// Prints the smallest possible number with digit sum 's'
// and 'm' number of digits.
function findLargest(m, s)
{
    // If sum of digits is 0, then a number is possible
    // only if number of digits is 1.
    if (s == 0)
    {
        (m == 1)? document.write("Largest number is " + 0)
                    : document.write("Not possible");
        return ;
    }
 
    // Sum greater than the maximum possible sum.
    if (s > 9*m)
    {
        document.write("Not possible");
        return ;
    }
 
    // Create an array to store digits of result
    let res = new Array(m);
 
    // Fill from most significant digit to least
    // significant digit.
    for (let i=0; i<m; i++)
    {
        // Fill 9 first to make the number largest
        if (s >= 9)
        {
            res[i] = 9;
            s -= 9;
        }
 
        // If remaining sum becomes less than 9, then
        // fill the remaining sum
        else
        {
            res[i] = s;
            s = 0;
        }
    }
 
    document.write("Largest number is ");
    for (let i=0; i<m; i++)
        document.write(res[i]);
}
 
// Driver code
    let s = 9, m = 2;
    findLargest(m, s);
 
// This code is contributed by Mayank Tyagi
 
</script>


Output

Largest number is 90

Time Complexity of this solution is O(m).
Auxiliary Space: O(m), where m is the given integer.

Approach: Greedy Algorithm

  • Create an empty string to store the result
  • If d is 1, append s to the result and return it
  • Loop from the leftmost digit to the rightmost digit
    a. If the remaining sum of digits is greater than or equal to 9, append 9 to the result and subtract 9 from the remaining sum of digits
    b. If the remaining sum of digits is less than 9, append the remaining sum of digits to the result and fill the remaining digits with 0s
  • Return the result

C++




#include <iostream>
#include <string>
 
using namespace std;
 
int largest_number(int s, int d) {
    if (s == 0) {
        return 0;
    }
    if (s > 9 * d) {
        return -1;
    }
    string result = "";
    for (int i = 0; i < d; i++) {
        if (s >= 9) {
            result += '9';
            s -= 9;
        } else {
            result += to_string(s);
            s = 0;
        }
        if (s == 0 && i < d-1) {
            result += string(d-i-1, '0');
            break;
        }
    }
    return stoi(result);
}
 
int main() {
    // Test case 1
    cout << largest_number(9, 2) << endl; // Output: 90
 
    // Test case 2
    cout << largest_number(20, 3) << endl; // Output: 992
 
    return 0;
}


Java




import java.util.*;
 
public class Main {
 
    public static int largest_number(int s, int d)
    {
        // If s is 0, then the largest number is 0.
        if (s == 0) {
            return 0;
        }
        // If s is greater than 9 times d, then it is
        // impossible to form a d-digit number whose sum of
        // digits is s.
        if (s > 9 * d) {
            return -1;
        }
        // Initialize an empty string to store the result.
        String result = "";
        // Loop through each digit of the number.
        for (int i = 0; i < d; i++) {
            // If s is greater than or equal to 9, then add
            // 9 to the result and subtract 9 from s.
            if (s >= 9) {
                result += '9';
                s -= 9;
            }
            // Otherwise, add s to the result and set s to
            // 0.
            else {
                result += Integer.toString(s);
                s = 0;
            }
            // If s is 0 and there are still digits left to
            // fill, then fill the remaining digits with 0s
            // and break out of the loop.
            if (s == 0 && i < d - 1) {
                result += String.join(
                    "",
                    Collections.nCopies(d - i - 1, "0"));
                break;
            }
        }
        // Convert the result to an integer and return it.
        return Integer.parseInt(result);
    }
 
    public static void main(String[] args)
    {
        // Test case 1
        System.out.println(
            largest_number(9, 2)); // Output: 90
 
        // Test case 2
        System.out.println(
            largest_number(20, 3)); // Output: 992
    }
}


Python3




def largest_number(s, d):
    if s == 0:
        return 0
    if s > 9 * d:
        return -1
    result = ''
    for i in range(d):
        if s >= 9:
            result += '9'
            s -= 9
        else:
            result += str(s)
            s = 0
        if s == 0 and i < d-1:
            result += '0' * (d-i-1)
            break
    return int(result)
 
# Test case 1
print(largest_number(9, 2)) # Output: 90
 
# Test case 2
print(largest_number(20, 3)) # Output: 992


C#




using System;
 
class Program {
    static int LargestNumber(int s, int d) {
        if (s == 0) {
            return 0;
        }
        if (s > 9 * d) {
            return -1;
        }
        string result = "";
        for (int i = 0; i < d; i++) {
            if (s >= 9) {
                result += "9";
                s -= 9;
            } else {
                result += s.ToString();
                s = 0;
            }
            if (s == 0 && i < d - 1) {
                result += new string('0', d - i - 1);
                break;
            }
        }
        return int.Parse(result);
    }
 
    static void Main(string[] args) {
        // Test case 1
        Console.WriteLine(LargestNumber(9, 2)); // Output: 90
 
        // Test case 2
        Console.WriteLine(LargestNumber(20, 3)); // Output: 992
    }
}


Javascript




function largestNumber(s, d) {
if (s == 0) {
return 0;
}
if (s > 9 * d) {
return -1;
}
let result = "";
for (let i = 0; i < d; i++) {
if (s >= 9) {
result += "9";
s -= 9;
} else {
result += s.toString();
s = 0;
}
if (s == 0 && i < d - 1) {
result += "0".repeat(d - i - 1);
break;
}
}
return parseInt(result);
}
 
// Test cases
console.log(largestNumber(9, 2)); // Output: 90
console.log(largestNumber(20, 3)); // Output: 992


Output

90
992

Time complexity: O(d),
Auxiliary space: O(d)



Similar Reads

Find smallest number with given number of digits and sum of digits under given constraints
Given two integers S and D, the task is to find the number having D number of digits and the sum of its digits as S such that the difference between the maximum and the minimum digit in the number is as minimum as possible. If multiple such numbers are possible, print the smallest number.Examples: Input: S = 25, D = 4 Output: 6667 The difference be
7 min read
Check if sum of digits in the left half is divisible by sum of digits in the right half in the largest permutation of N
Given a positive integer N, the task is to maximize the integer N by rearranging the digits and check if the sum of the left half digits is divisible by the sum of the right half digits or not. If found to be true, then print "Yes". Otherwise, print "No". If the number of digits(say D) in the given number N is odd, then consider any of the two poss
8 min read
Find smallest number with given number of digits and sum of digits
How to find the smallest number with given digit sum s and number of digits d? Examples : Input : s = 9, d = 2 Output : 18 There are many other possible numbers like 45, 54, 90, etc with sum of digits as 9 and number of digits as 2. The smallest of them is 18. Input : s = 20, d = 3 Output : 299 Recommended PracticeSmallest numberTry It! A Simple So
9 min read
Smallest number with given sum of digits and sum of square of digits
Given the sum of digits a and sum of the square of digits b . Find the smallest number with the given sum of digits and the sum of the square of digits. The number should not contain more than 100 digits. Print -1 if no such number exists or if the number of digits is more than 100.Examples: Input : a = 18, b = 162 Output : 99 Explanation : 99 is t
15+ min read
Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
Given three positive integers N, A, and B. The task is to count the numbers of length N containing only digits A and B and whose sum of digits also contains the digits A and B only. Print the answer modulo 109 + 7.Examples: Input: N = 3, A = 1, B = 3 Output: 1 Possible numbers of length 3 are 113, 131, 111, 333, 311, 331 and so on... But only 111 i
15 min read
Find smallest number with given digits and sum of digits
Given two positive integers P and Q, find the minimum integer containing only digits P and Q such that the sum of the digits of the integer is N. Example: Input: N = 11, P = 4, Q = 7 Output: 47Explanation: There are two possible integers that can be formed from 4 and 7 such that their sum is 11 i.e. 47 and 74. Since we need to find the minimum poss
9 min read
Find second smallest number from sum of digits and number of digits
Given the sum of digits as S and the number of digits as D, the task is to find the second smallest number Examples: Input: S = 9, D = 2Output: 27Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Input: S = 16, D = 3Output: 178Explanation: 169 is the smallest number possible with s
8 min read
Count numbers in given range such that sum of even digits is greater than sum of odd digits
Given two integers L and R denoting a range [L, R]. The task is to find the total count of numbers in the given range [L,R] whose sum of even digits is greater than the sum of odd digits. Examples: Input : L=2 R=10 Output : 4 Numbers having the property that sum of even digits is greater than sum of odd digits are: 2, 4, 6, 8 Input : L=2 R=17 Outpu
14 min read
Find the largest Number that can be formed with the given Digits
Given an array of integers arr[] represents digits of a number. The task is to write a program to generate the largest number possible using these digits. Note: The digits in the array are between 0 and 9. That is, 0 < arr[i] < 9. Examples: Input: arr[] = {4, 7, 9, 2, 3}Output: Largest number: 97432 Input: arr[] = {8, 6, 0, 4, 6, 4, 2, 7}Outp
10 min read
Count number of integers in given range with adjacent digits different and sum of digits equal to M
Given integers T, A, and B, the task for this problem is to find numbers in the range [A, B] such that the adjacent digits of the number are different and the sum of digits is equal to T. ( A ? B ? 1018) Examples: Input: T = 5, A = 1, B = 100Output: 6Explanation: 5, 14, 23, 32, 41, and 50 are valid integers that are in between the range 1 to 100 an
15+ min read
Find largest sum of digits in all divisors of n
Given an integer number n, find the largest sum of digits in all divisors of n. Examples : Input : n = 12 Output : 6 Explanation: The divisors are: 1 2 3 4 6 12. 6 is maximum sum among all divisors Input : n = 68 Output : 14 Explanation: The divisors are: 1 2 4 68 68 consists of maximum sum of digitRecommended PracticeLargest sum of digits in all D
9 min read
Find the average of k digits from the beginning and l digits from the end of the given number
Given three integers N, K and L. The task is to find the average of the first K digits and the last L digits of the given number N without any digit overlapping.Examples: Input: N = 123456, K = 2, L = 3 Output: 3.0 Sum of first K digits will be 1 + 2 = 3 Sum of last L digits will be 4 + 5 + 6 = 15 Average = (3 + 15) / (2 + 3) = 18 / 5 = 3Input: N =
13 min read
Largest number by rearranging digits of a given positive or negative number
Given an integer N(positive or negative), the task is to find the maximum number that can be formed using all of the digits of this number. Examples: Input: -38290367Output: -20336789Explanation: As there is need to use all the digits, 0 cannot be the first digit because it becomes redundant at first position. Input: 1203465Output: 6543210 Approach
7 min read
Number formed by deleting digits such that sum of the digits becomes even and the number odd
Given a non-negative number N, the task is to convert the number by deleting some digits of the number, such that the sum of the digits becomes even but the number is odd. In case there is no possible number, then print -1.Note: There can be multiple numbers possible for a given N.Examples: Input: N = 18720 Output: 17 Explanation: After Deleting 8,
5 min read
Largest number with the given set of N digits that is divisible by 2, 3 and 5
Given a set of 'N' digits. The task is to find the maximum integer that we can make from these digits. The resultant number must be divisible by 2, 3, and 5. Note: It is not necessary to use all the digits from the set. Also, leading zeroes are not allowed.Examples: Input: N = 11, setOfDigits = {3, 4, 5, 4, 5, 3, 5, 3, 4, 4, 0} Output: 5554443330 A
15+ min read
Largest number divisible by 50 that can be formed from a given set of N digits consisting of 0s and 7s only
Given an array arr[] consisting of N integers which are either 0 or 7, the task is to find the largest number that can be formed using the array elements such that it is divisible by 50. Examples: Input: arr[] = {7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 0}Output: 777770000000 Input: arr[] = {7, 0}Output: 0 Naive Approach: The simplest approach to solve
7 min read
Maximize the given number by replacing a segment of digits with the alternate digits given
Given many N digits. We are also given 10 numbers which represents the alternate number for all the one-digit numbers from 0 to 9. We can replace any digit in N with the given alternate digit to it, but we are only allowed to replace any consecutive segment of numbers for once only, the task is to replace any consecutive segment of numbers such tha
6 min read
Numbers with sum of digits equal to the sum of digits of its all prime factor
Given a range, the task is to find the count of the numbers in the given range such that the sum of its digit is equal to the sum of all its prime factors digits sum.Examples: Input: l = 2, r = 10 Output: 5 2, 3, 4, 5 and 7 are such numbers Input: l = 15, r = 22 Output: 3 17, 19 and 22 are such numbers As, 17 and 19 are already prime. Prime Factors
13 min read
Count of numbers in range [L, R] having sum of digits of its square equal to square of sum of digits
Given two integers L and R, the task is to find the count of numbers in range [L, R] such that the sum of digits of its square is equal to the square of sum of its digits, Example: Input: L = 22, R = 22Output: 1Explanation: 22 is only valid number in this range as sum of digits of its square = S(22*22) = S(484) = 16 and square of sum of its digits
11 min read
Construct the largest number whose sum of cost of digits is K
Given a positive integer K, and an array arr[] consisting of N( =9) integers such that arr[i] represents the cost of the digit (i+1), the task is to find the largest number that can be formed using the digits over the range [1, 9] such that the sum of the cost of digits of the number formed is K. Examples: Input: K = 14, arr[] = {3, 12, 9, 5, 3, 4,
11 min read
Find the Largest Cube formed by Deleting minimum Digits from a number
Given a number n, the task is to find the largest perfect cube that can be formed by deleting minimum digits(possibly 0) from the number. X is called a perfect cube if X = Y3 for some Y.Examples: Input : 4125 Output : 125 Explanation 125 = 53. We can form 125 by deleting digit 4 from 4125 Input : 876 Output :8 Explanation 8 = 23. We can form 8 by d
10 min read
Find the largest number that can be formed by changing at most K digits
Given string str representing a number and an integer K, the task is to find the largest number that can be formed by changing at most K digits in the given number. Examples: Input: str = "569431", K = 3 Output: 999931 Replace first, second and fourth digits with 9.Input: str = "5687", K = 2 Output: 9987 Approach: In order to get the maximum number
4 min read
Find largest number smaller than N with same set of digits
Given a number N in the form of string. The task is to find the greatest number that has same set of digits as N and is smaller than N. If it is not possible to find any such number, then print "not possible". Examples: Input: N = "218765" Output: 218756 Input: N = "1234" Output: Not Possible Input: N = "262345" Output: 256432Recommended: Please so
9 min read
Count numbers from a given range that can be expressed as sum of digits raised to the power of count of digits
Given an array arr[] consisting of queries of the form {L, R}, the task for each query is to count the numbers in the range [L, R] that can be expressed as the sum of its digits raised to the power of count of digits. Examples: Input: arr[][] = {{8, 11}}Output: 2Explanation:From the given range [1, 9], the numbers that can be expressed as the sum o
10 min read
Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
Given a range [L, R] and two positive integers N and M. The task is to count the numbers in the range containing only non-zero digits whose sum of digits is equal to N and the number is divisible by M.Examples: Input: L = 1, R = 100, N = 8, M = 2 Output: 4 Only 8, 26, 44 and 62 are valid numbers Input: L = 1, R = 200, N = 4, M = 11 Output: 2 Only 2
14 min read
Minimum digits to be removed to make either all digits or alternating digits same
Given a numeric string str, the task is to find the minimum number of digits to be removed from the string such that it satisfies either of the below conditions: All the elements of the string are the same.All the elements at even position are same and all the elements at the odd position are same, which means the string is alternating with the equ
7 min read
Number of digits in the nth number made of given four digits
Find the number of digits in the nth number constructed by using 6, 1, 4, and 9 as the only digits in the ascending order. First few numbers constructed by using only 6, 1, 4, and 9 as digits in ascending order would be: 1, 6, 4, 9, 11, 14, 16, 19, 41, 44, 46, 49, 61, 64, 66, 69, 91, 94, 96, 99, 111, 114, 116, 119 and so on. Examples: Input : 6Outp
13 min read
Find a number x such that sum of x and its digits is equal to given n.
Given a positive number n. We need to find a number x such that sum of digits of x to itself is equal to n. If no such x is possible print -1.Examples: Input : n = 21 Output : x = 15 Explanation : x + its digit sum = 15 + 1 + 5 = 21 Input : n = 5 Output : -1 We iterate from 1 to n and for each intermediate number, x find its digit sum and then add
5 min read
Find the largest multiple of 3 from array of digits | Set 2 (In O(n) time and O(1) space)
Given an array of digits (contain elements from 0 to 9). Find the largest number that can be made from some or all digits of array and is divisible by 3. The same element may appear multiple times in the array, but each element in the array may only be used once. Examples: Input : arr[] = {5, 4, 3, 1, 1} Output : 4311Input : Arr[] = {5, 5, 5, 7} Ou
15+ min read
Count of groups having largest size while grouping according to sum of its digits
Given an integer N, the task is to find the number of groups having the largest size. Each number from 1 to N is grouped according to the sum of its digits. Examples: Input: N = 13 Output: 4 Explanation: There are 9 groups in total, they are grouped according to the sum of its digits of numbers from 1 to 13: [1, 10] [2, 11] [3, 12] [4, 13] [5] [6]
7 min read
Article Tags :
Practice Tags :