Least Greater number with same digit sum
Last Updated :
05 Apr, 2022
Given a number represented in the form of an array such that each element of the array stores a single digit of the number. That is, array for the number 1234 will be arr[] = {1,2,3,4}. The task is to find the least number greater than the given number but having the sum of digits equal to the given number. For simplicity: Consider the length of number can be 20 at maximum. Examples:
Input : arr[] = {0, 0, 0, 0, 0, 0, 0, 3, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 8 }; Output : 00000004799999999999 Explanation : Sum of digits = 110 Input : arr[] = {0, 0, 0, 0, 0, 0, 0, 3, 9, 7, 0, 0, 2, 9, 8, 9, 5, 9, 9, 0}; Output : 00000003970029896089 Explanation : Sum of digits = 70
A Brute Force Approach is to:
- Start from that number and increment the number by one.
- Check the sum. If the sum is same the return the number.
- Else return to step one.
A Better Approach is to jump to the next number in O(n) time complexity, where n is the length of string.
We divide the number into 4 regions : 1st: Trailing zeros . 2nd: The lowest digit not in Region 1. 3rd: Consecutive 9s starting with the digit above Region 2. 4th: All remaining digits. Then the next number is : [Region 4+1] [Region 1] [Region 2-1] [Region 3] . Example: Input Number = 548995000 Region 1 : 000 Region 2 : 5 Region 3 : 99 Region 4 : 548 Next number = 549000499
Below is the implementation of the above approach:
C++
// CPP program to find next greater number with
// same sum of digits.
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
void getnext(int arr[], int n)
{
// for storing 4 regions
vector<int> a1, a2, a3, a4;
// trailing zeros region1
int i = n - 1; // last index
while (arr[i] == 0)
{
a1.pb(0);
i--;
}
// lowest region(region 2) not in region 1
a2.pb(arr[i--]);
// Consecutive 9's (region 3)
while (arr[i] == 9)
{
a3.pb(9);
i--;
}
int j = 0;
while (arr[j] == 0)
j++; // Starting zeros
while (j <= i) // 4th region
{
a4.pb(arr[j]);
j++;
}
// Calculation of result
j = a4.size() - 1;
a4[j]++; // Region4 + 1
a2[0]--; // Region2 -1
int l = a1.size() + a2.size() + a3.size() + a4.size();
// Calculating the result
j = n-1;
i = a3.size() - 1;
// Copying the third region
while (i >= 0)
{
arr[j--] = a3[i--];
}
// Copying the 2nd region
i = a2.size() - 1;
while (i >= 0)
{
arr[j--] = a2[i--];
}
// Copying the 1st region
i = a1.size() - 1;
while (i >= 0)
{
arr[j--] = a1[i--];
}
// Copying the 4th region
i = a4.size() - 1;
while (i >= 0)
{
arr[j--] = a4[i--];
}
while (j >= 0)
arr[j--] = 0;
}
int main()
{
int arr[] = { 0, 0, 0, 0, 0, 0, 0, 3, 9, 7,
0, 0, 2, 9, 8, 9, 5, 9, 9, 0 };
int n = sizeof(arr)/sizeof(arr[0]);
getnext(arr, n); // Calling the function
for (int i = 0; i < n; i++)
cout << arr[i];
return 0;
}
Java
// Java program to find next greater number with
// same sum of digits.
import java.util.*;
class GFG
{
static void getnext(int []arr, int n)
{
// for storing 4 regions
ArrayList<Integer> a1 = new ArrayList<Integer>();
ArrayList<Integer> a2 = new ArrayList<Integer>();
ArrayList<Integer> a3 = new ArrayList<Integer>();
ArrayList<Integer> a4 = new ArrayList<Integer>();
// trailing zeros region1
int i = n - 1; // last index
while (arr[i] == 0)
{
a1.add(0);
i--;
}
// lowest region(region 2) not in region 1
a2.add(arr[i--]);
// Consecutive 9's (region 3)
while (arr[i] == 9)
{
a3.add(9);
i--;
}
int j = 0;
while (arr[j] == 0)
j++; // Starting zeros
while (j <= i) // 4th region
{
a4.add(arr[j]);
j++;
}
// Calculation of result
j = a4.size() - 1;
a4.set(j,a4.get(j) + 1); // Region4 + 1
a2.set(0,a2.get(0) - 1); // Region2 -1
//int l = a1.size() + a2.size() + a3.size() + a4.size();
// Calculating the result
j = n - 1;
i = a3.size() - 1;
// Copying the third region
while (i >= 0)
{
arr[j--] = (int)a3.get(i);
i--;
}
// Copying the 2nd region
i = a2.size() - 1;
while (i >= 0)
{
arr[j--] = (int)a2.get(i);
i--;
}
// Copying the 1st region
i = a1.size() - 1;
while (i >= 0)
{
arr[j--] = a1.get(i);
i--;
}
// Copying the 4th region
i = a4.size() - 1;
while (i >= 0)
{
arr[j--] = a4.get(i);
i--;
}
while (j >= 0)
arr[j--] = 0;
}
// Driver code
public static void main (String[] args)
{
int []arr = { 0, 0, 0, 0, 0, 0, 0, 3, 9, 7,
0, 0, 2, 9, 8, 9, 5, 9, 9, 0 };
int n = arr.length;
getnext(arr, n); // Calling the function
for (int i = 0; i < n; i++)
System.out.print(arr[i]);
}
}
// This code is contributed by mits
Python3
# Python3 program to find next greater number with
# same sum of digits.
def getnext(arr, n):
# for storing 4 regions
a1=[];
a2=[];
a3=[];
a4=[];
# trailing zeros region1
i = n - 1; # last index
while (arr[i] == 0):
a1.append(0);
i-=1;
# lowest region(region 2) not in region 1
a2.append(arr[i]);
i-=1;
# Consecutive 9's (region 3)
while (arr[i] == 9):
a3.append(9);
i-=1;
j = 0;
while (arr[j] == 0):
j+=1; # Starting zeros
while (j <= i): # 4th region
a4.append(arr[j]);
j+=1;
# Calculation of result
j = len(a4) - 1;
a4[j]+=1; # Region4 + 1
a2[0]-=1; # Region2 -1
l = len(a1) + len(a2) + len(a3) + len(a4);
# Calculating the result
j = n-1;
i = len(a3) - 1;
# Copying the third region
while (i >= 0):
arr[j] = a3[i];
j-=1;
i-=1;
# Copying the 2nd region
i = len(a2) - 1;
while (i >= 0):
arr[j] = a2[i];
j-=1;
i-=1;
# Copying the 1st region
i = len(a1) - 1;
while (i >= 0):
arr[j] = a1[i];
j-=1;
i-=1;
# Copying the 4th region
i = len(a4) - 1;
while (i >= 0):
arr[j] = a4[i];
j-=1;
i-=1;
while (j >= 0):
arr[j] = 0;
j-=1;
# Driver code
arr = [ 0, 0, 0, 0, 0, 0, 0, 3, 9, 7, 0,
0, 2, 9, 8, 9, 5, 9, 9, 0 ];
n = len(arr);
getnext(arr, n); # Calling the function
for i in range(0,n):
print(arr[i],end="");
# This code is contributed by mits
C#
// C# program to find next greater number with
// same sum of digits.
using System;
using System.Collections;
class GFG
{
static void getnext(int []arr, int n)
{
// for storing 4 regions
ArrayList a1 = new ArrayList();
ArrayList a2 = new ArrayList();
ArrayList a3 = new ArrayList();
ArrayList a4 = new ArrayList();
// trailing zeros region1
int i = n - 1; // last index
while (arr[i] == 0)
{
a1.Add(0);
i--;
}
// lowest region(region 2) not in region 1
a2.Add(arr[i--]);
// Consecutive 9's (region 3)
while (arr[i] == 9)
{
a3.Add(9);
i--;
}
int j = 0;
while (arr[j] == 0)
j++; // Starting zeros
while (j <= i) // 4th region
{
a4.Add(arr[j]);
j++;
}
// Calculation of result
j = a4.Count - 1;
a4[j] = (int)a4[j] + 1; // Region4 + 1
a2[0] = (int)a2[0] - 1; // Region2 -1
//int l = a1.Count + a2.Count + a3.Count + a4.Count;
// Calculating the result
j = n - 1;
i = a3.Count - 1;
// Copying the third region
while (i >= 0)
{
arr[j--] = (int)a3[i];
i--;
}
// Copying the 2nd region
i = a2.Count - 1;
while (i >= 0)
{
arr[j--] = (int)a2[i];
i--;
}
// Copying the 1st region
i = a1.Count - 1;
while (i >= 0)
{
arr[j--] = (int)a1[i];
i--;
}
// Copying the 4th region
i = a4.Count - 1;
while (i >= 0)
{
arr[j--] = (int)a4[i];
i--;
}
while (j >= 0)
arr[j--] = 0;
}
// Driver code
static void Main()
{
int []arr = { 0, 0, 0, 0, 0, 0, 0, 3, 9, 7,
0, 0, 2, 9, 8, 9, 5, 9, 9, 0 };
int n = arr.Length;
getnext(arr, n); // Calling the function
for (int i = 0; i < n; i++)
Console.Write(arr[i]);
}
}
// This code is contributed by mits
PHP
<?php
// PHP program to find next greater number with
// same sum of digits.
function getnext(&$arr, $n)
{
// for storing 4 regions
$a1=array();
$a2=array();
$a3=array();
$a4=array();
// trailing zeros region1
$i = $n - 1; // last index
while ($arr[$i] == 0)
{
array_push($a1,0);
$i--;
}
// lowest region(region 2) not in region 1
array_push($a2,$arr[$i--]);
// Consecutive 9's (region 3)
while ($arr[$i] == 9)
{
array_push($a3,9);
$i--;
}
$j = 0;
while ($arr[$j] == 0)
$j++; // Starting zeros
while ($j <= $i) // 4th region
{
array_push($a4,$arr[$j]);
$j++;
}
// Calculation of result
$j = count($a4) - 1;
$a4[$j]++; // Region4 + 1
$a2[0]--; // Region2 -1
$l = count($a1) + count($a2) + count($a3) + count($a4);
// Calculating the result
$j = $n-1;
$i = count($a3) - 1;
// Copying the third region
while ($i >= 0)
{
$arr[$j--] = $a3[$i--];
}
// Copying the 2nd region
$i = count($a2) - 1;
while ($i >= 0)
{
$arr[$j--] = $a2[$i--];
}
// Copying the 1st region
$i = count($a1) - 1;
while ($i >= 0)
{
$arr[$j--] = $a1[$i--];
}
// Copying the 4th region
$i = count($a4) - 1;
while ($i >= 0)
{
$arr[$j--] = $a4[$i--];
}
while ($j >= 0)
$arr[$j--] = 0;
}
// Driver code
$arr = array( 0, 0, 0, 0, 0, 0, 0, 3, 9, 7,
0, 0, 2, 9, 8, 9, 5, 9, 9, 0 );
$n = count($arr);
getnext($arr, $n); // Calling the function
for ($i = 0; $i < $n; $i++)
echo $arr[$i];
// This code is contributed by mits
?>
JavaScript
<script>
// JavaScript program to find next greater number with
// same sum of digits.
function getnext(arr, n)
{
// for storing 4 regions
let a1 = [], a2 = [], a3 = [], a4 = [];
// trailing zeros region1
let i = n - 1; // last index
while (arr[i] == 0)
{
a1.push(0);
i--;
}
// lowest region(region 2) not in region 1
a2.push(arr[i--]);
// Consecutive 9's (region 3)
while (arr[i] == 9)
{
a3.push(9);
i--;
}
let j = 0;
while (arr[j] == 0)
j++; // Starting zeros
while (j <= i) // 4th region
{
a4.push(arr[j]);
j++;
}
// Calculation of result
j = a4.length - 1;
a4[j]++; // Region4 + 1
a2[0]--; // Region2 -1
let l = a1.length + a2.length + a3.length + a4.length;
// Calculating the result
j = n-1;
i = a3.length - 1;
// Copying the third region
while (i >= 0)
{
arr[j--] = a3[i--];
}
// Copying the 2nd region
i = a2.length - 1;
while (i >= 0)
{
arr[j--] = a2[i--];
}
// Copying the 1st region
i = a1.length - 1;
while (i >= 0)
{
arr[j--] = a1[i--];
}
// Copying the 4th region
i = a4.length - 1;
while (i >= 0)
{
arr[j--] = a4[i--];
}
while (j >= 0)
arr[j--] = 0;
}
// driver code
let arr = [ 0, 0, 0, 0, 0, 0, 0, 3, 9, 7,
0, 0, 2, 9, 8, 9, 5, 9, 9, 0 ];
let n = arr.length;
getnext(arr, n); // Calling the function
for (let i = 0; i < n; i++)
document.write(arr[i]);
// code is contributed by shinjanpatra
</script>
Output:00000003970029896089
Similar Reads
Count numbers with same first and last digits
Given an interval, the task is to count numbers that have the same first and last digits. For example, 1231 has the same first and last digits. Examples: Input : start = 7, end : 68 Output : 9 Number with same first and last digits are, 7 8 9 11 22 33 44 55 66. Input : start = 5, end : 40 Output : 8
9 min read
Number with even sum of digits
Fixed compiling error in java programA positive integer is considered a good number if sum of its digits is even. Find n-th smallest good number. Examples : Input : n = 1 Output : 2 First good number is smallest positive number with sum of digits even which is 2. Input : n = 10 Output : 20 A simple
5 min read
Sum of numbers with exactly 2 bits set
Given a number n. Find the sum of all numbers up to n whose 2 bits are set. Examples: Input : 10 Output : 33 3 + 5 + 6 + 9 + 10 = 33 Input : 100 Output : 762 Naive Approach: Find each number up to n whose 2 bits are set. If its 2 bits are set add it to the sum. C++ // CPP program to find sum of numb
8 min read
Smallest even digits number not less than N
Given a number N, we need to write a program to find the smallest number not less than N, which has all digits even. Examples: Input: N = 1345 Output: 2000Explanation: 2000 is the smallest number not less than N, whose all digits are even. Input : N = 2397Output : 2400 Explanation: 2400 is the small
15+ min read
Smallest number greater than Y with sum of digits equal to X
Given two integers X and Y, find the minimal number with the sum of digits X, which is strictly greater than Y. Examples: Input: X = 18, Y = 99 Output: 189 Explanation: 189 is the smallest number greater than 99 having sum of digits = 18. Input: X = 12, Y = 72 Output: 75 Explanation: 75 is the small
11 min read
Smallest number with given digit count and sum
Given two integers s and d, find the smallest possible number that has exactly d digits and a sum of digits equal to s.Return the number as a string. If no such number exists, return "-1".Examples :Input: s = 9, d = 2Output: 18 Explanation: 18 is the smallest number possible with the sum of digits =
10 min read
Largest number less than N with digit sum greater than the digit sum of N
Given an integer N, the task is to find the greatest number less than N such that the sum of its digits is greater than the sum of the digits of N. If the condition isn't satisfied for any number then print -1.Examples: Input: N = 100 Output: 99 99 is the largest number less than 100 sum of whose di
9 min read
Next greater Number than N with the same quantity of digits A and B
Given a number N and two digits A and B . The task is to find the least number not less than N which contains the equal number of digits A and B.Note: N <= 107Examples: Input : N = 4500, A = 4, B = 7 Output : 4747 The number greater than 4500 which has the same quantity of number '4' and number '
7 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.
9 min read
Find maximum sum pair with same digit sum
Given an array arr having N integers, the task is to find a pair with maximum sum and having the same sum of digits. Print the sum of that pair, if it exists. Otherwise, print -1. Examples: Input: arr[]={55, 23, 32, 46, 88}Output: 46 55 101Explanation: Pair {55, 46} will give the sum of 55 + 46 = 10
7 min read