Software Development

Cut up the integer in two components such that the distinction between the digit sum of these values just isn’t greater than 1

Cut up the integer in two components such that the distinction between the digit sum of these values just isn’t greater than 1
Written by admin


  

import java.util.*;

  

class GFG {

  

    

    public static void predominant(String[] args)

    {

        int val = 161;

        printTwoNumbers(val);

    }

  

    

    

    

    

    

    public static void printTwoNumbers(int val)

    {

  

        

        

        int num1 = 0;

        int num2 = 0;

        int pow = 1;

  

        

        

        whereas (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 Quantity: " + num1

                           + " and Second Quantity: " + num2);

    }

  

    

    

    public static int digitSum(int n)

    {

        int sum = 0;

  

        whereas (n != 0) {

            sum = sum + n % 10;

            n = n / 10;

        }

  

        return sum;

    }

}

About the author

admin

Leave a Comment