Split the integer in two parts such that the difference between the digit sum of those values is not more than 1
Last Updated :
10 Aug, 2023
Given an integer val. Split the given integer in two integers val1 and val2 such that val1 + val2 = val and the difference between the digit sum of val1 and val2 is not more than 1, the task is to print val1 and val2. (If there are multiple answers, then print any)
Examples:
Input: val = 161
Output: 130 and 31
Explanation: The digit sum of 130 is = 4 and the Digit sum of 31 is = 4 so the difference between them is 0 which is not more than 1.
Input: val = 19
Output: 14 and 5
Naive Approach:
The naive approach is to check all the possible values by traversing.
Steps:
Steps involved in the implementation of the code:
- First we create a loop to traverse from 1 to val/2.
- Then we check if the current value(i)’s and the remaining value(val-i)’s digit sum satisfy the condition or not.
- If condition satisfy then we print those two values.
- Else continue the iterate until reach to the val/2.
- If none of the values’ digit sum satisfy the condition, then print splitting is not possible.
Below is the code for the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int digitSum( int n)
{
int sum = 0;
while (n != 0) {
sum = sum + n % 10;
n = n / 10;
}
return sum;
}
void printTwoNumbers( int val)
{
int num1 = 0, num2 = 0;
for ( int i = 1; i <= val / 2; i++) {
if ( abs (digitSum(i) - digitSum(val - i)) <= 1) {
num1 = i;
num2 = val - i;
break ;
}
}
if (num1 == 0) {
cout << "Splitting is not possible" << endl;
return ;
}
cout << "First Number: " << num1 << endl
<< "Second Number: " << num2 << endl;
}
int main()
{
int val = 19;
printTwoNumbers(val);
return 0;
}
|
Java
import java.util.*;
class Main {
public static int digitSum( int n) {
int sum = 0 ;
while (n != 0 ) {
sum = sum + n % 10 ;
n = n / 10 ;
}
return sum;
}
public static void printTwoNumbers( int val) {
int num1 = 0 , num2 = 0 ;
for ( int i = 1 ; i <= val / 2 ; i++) {
if (Math.abs(digitSum(i) - digitSum(val - i)) <= 1 ) {
num1 = i;
num2 = val - i;
break ;
}
}
if (num1 == 0 ) {
System.out.println( "Splitting is not possible" );
return ;
}
System.out.println( "First Number: " + num1);
System.out.println( "Second Number: " + num2);
}
public static void main(String[] args) {
int val = 19 ;
printTwoNumbers(val);
}
}
|
Python3
def digitSum(n):
sum = 0
while n ! = 0 :
sum = sum + n % 10
n = n / / 10
return sum
def printTwoNumbers(val):
num1, num2 = 0 , 0
for i in range ( 1 , (val / / 2 ) + 1 ):
if abs (digitSum(i) - digitSum(val - i)) < = 1 :
num1 = i
num2 = val - i
break
if num1 = = 0 :
print ( "Splitting is not possible" )
return
print ( "First Number:" , num1)
print ( "Second Number:" , num2)
val = 19
printTwoNumbers(val)
|
C#
using System;
public class GFG {
static int DigitSum( int n)
{
int sum = 0;
while (n != 0) {
sum = sum + n % 10;
n = n / 10;
}
return sum;
}
static void PrintTwoNumbers( int val)
{
int num1 = 0, num2 = 0;
for ( int i = 1; i <= val / 2; i++) {
if (Math.Abs(DigitSum(i) - DigitSum(val - i))
<= 1) {
num1 = i;
num2 = val - i;
break ;
}
}
if (num1 == 0) {
Console.WriteLine( "Splitting is not possible" );
return ;
}
Console.WriteLine( "First Number: " + num1);
Console.WriteLine( "Second Number: " + num2);
}
static public void Main()
{
int val = 19;
PrintTwoNumbers(val);
}
}
|
Javascript
function digitSum(n) {
let sum = 0;
while (n !== 0) {
sum += n % 10;
n = Math.floor(n / 10);
}
return sum;
}
function printTwoNumbers(val) {
let num1 = 0, num2 = 0;
for (let i = 1; i <= Math.floor(val / 2); i++) {
if (Math.abs(digitSum(i) - digitSum(val - i)) <= 1) {
num1 = i;
num2 = val - i;
break ;
}
}
if (num1 === 0) {
console.log( "Splitting is not possible" );
return ;
}
console.log( "First Number:" , num1);
console.log( "Second Number:" , num2);
}
let val = 19;
printTwoNumbers(val);
|
Output
First Number: 5
Second Number: 14
Time Complexity: O(val*log(val))
Auxiliary Space: O(1)
Efficient Approach:
This can be solved with the following idea:
If you observe carefully, you’ll realize that we can solve the problem by dividing every digit of the given number equally into two numbers, in this way the difference between the digit sum of those two numbers will always remain less than or equal to 1.
Steps involved in the implementation of the code:
- We will traverse every digit of the given integer from backward. (Example: if val = 19, then we will apply the operation on 9 first then next to 1, and so on while val is greater than zero.)
- Now, if the digit from the val is odd then it can’t be divided into two equal halves, so we will give the larger part to the number whose digit sum is greater or if the digit sum is equal then we will give the larger part to any number.
- Else if the digit from the val is even then we will divide the two equal halves into both numbers.
- Finally, print both numbers.
Below is the code for the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int digitSum( int n)
{
int sum = 0;
while (n != 0) {
sum = sum + n % 10;
n = n / 10;
}
return sum;
}
void printTwoNumbers( int val)
{
int num1 = 0;
int num2 = 0;
int pow = 1;
while (val > 0) {
int digit = val % 10;
if (digit % 2 == 0) {
num1 = (digit / 2) * pow + num1;
num2 = (digit / 2) * pow + num2;
}
else {
if (digitSum(num1) > digitSum(num2)) {
num1 = (digit / 2) * pow + num1;
num2 = (digit / 2 + 1) * pow + num2;
}
else {
num2 = (digit / 2) * pow + num2;
num1 = (digit / 2 + 1) * pow + num1;
}
}
pow *= 10;
val /= 10;
}
cout << "First Number: " << num1
<< " and Second Number: " << num2 << endl;
}
int main()
{
int val = 161;
printTwoNumbers(val);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
int val = 161 ;
printTwoNumbers(val);
}
public static void printTwoNumbers( int val)
{
int num1 = 0 ;
int num2 = 0 ;
int pow = 1 ;
while (val > 0 ) {
int digit = val % 10 ;
if (digit % 2 == 0 ) {
num1 = (digit / 2 ) * pow + num1;
num2 = (digit / 2 ) * pow + num2;
}
else {
if (digitSum(num1) > digitSum(num2)) {
num1 = (digit / 2 ) * pow + num1;
num2 = (digit / 2 + 1 ) * pow + num2;
}
else {
num2 = (digit / 2 ) * pow + num2;
num1 = (digit / 2 + 1 ) * pow + num1;
}
}
pow *= 10 ;
val /= 10 ;
}
System.out.println( "First Number: " + num1
+ " and Second Number: " + num2);
}
public static int digitSum( int n)
{
int sum = 0 ;
while (n != 0 ) {
sum = sum + n % 10 ;
n = n / 10 ;
}
return sum;
}
}
|
Python
def digitSum(n):
sum = 0
while n ! = 0 :
sum = sum + n % 10
n = n / / 10
return sum
def printTwoNumbers(val):
num1 = 0
num2 = 0
pow = 1
while val > 0 :
digit = val % 10
if digit % 2 = = 0 :
num1 = (digit / / 2 ) * pow + num1
num2 = (digit / / 2 ) * pow + num2
else :
if digitSum(num1) > digitSum(num2):
num1 = (digit / / 2 ) * pow + num1
num2 = (digit / / 2 + 1 ) * pow + num2
else :
num2 = (digit / / 2 ) * pow + num2
num1 = (digit / / 2 + 1 ) * pow + num1
pow * = 10
val / / = 10
print ( "First Number:" , num1, "and Second Number:" , num2)
val = 161
printTwoNumbers(val)
|
C#
using System;
class Program
{
static int DigitSum( int n)
{
int sum = 0;
while (n != 0) {
sum = sum + n % 10;
n = n / 10;
}
return sum;
}
static void PrintTwoNumbers( int val)
{
int num1 = 0;
int num2 = 0;
int pow = 1;
while (val > 0) {
int digit = val % 10;
if (digit % 2 == 0) {
num1 = (digit / 2) * pow + num1;
num2 = (digit / 2) * pow + num2;
}
else {
if (DigitSum(num1) > DigitSum(num2)) {
num1 = (digit / 2) * pow + num1;
num2 = (digit / 2 + 1) * pow + num2;
}
else {
num2 = (digit / 2) * pow + num2;
num1 = (digit / 2 + 1) * pow + num1;
}
}
pow *= 10;
val /= 10;
}
Console.WriteLine( "First Number: " + num1
+ " and Second Number: " + num2);
}
static void Main( string [] args)
{
int val = 161;
PrintTwoNumbers(val);
}
}
|
Javascript
function digitSum(n)
{
let sum = 0;
while (n !== 0) {
sum += n % 10;
n = Math.floor(n / 10);
}
return sum;
}
function printTwoNumbers(val)
{
let num1 = 0;
let num2 = 0;
let pow = 1;
while (val > 0) {
let digit = val % 10;
if (digit % 2 === 0) {
num1 = Math.floor((digit / 2)) * pow + num1;
num2 = Math.floor((digit / 2)) * pow + num2;
}
else {
if (digitSum(num1) > digitSum(num2)) {
num1 = Math.floor((digit / 2)) * pow + num1;
num2 = Math.floor((digit / 2) + 1) * pow
+ num2;
}
else {
num2 = Math.floor((digit / 2)) * pow + num2;
num1 = Math.floor((digit / 2) + 1) * pow
+ num1;
}
}
pow *= 10;
val = Math.floor(val / 10);
}
console.log(`First Number: ${ num1 } and Second Number: ${ num2 }`);
}
let val = 161;
printTwoNumbers(val);
|
Output
First Number: 31 and Second Number: 130
Time Complexity: O(log(val))
Auxiliary Space: O(1) space is used.
Share your thoughts in the comments
Please Login to comment...