Open In App
Related Articles

Divide a number into two parts

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Given an integer N containing the digit 4 at least once. The task is to divide the number into two parts x1 and x2 such that: 

  • x1 + x2 = N.
  • And none of the parts contain the digit 4.

Note that there may be multiple answers.

Examples: 

Input: N = 4 
Output: 1 3 
1 + 3 = 4

Input: N = 9441 
Output: 9331 110 
9331 + 110 = 9441 

Naive Approach: The idea is to run two nested for loops and pick two numbers whose sum is n and they do not have any digit equal to 4. Below are the steps:

  • Run two nested for loops from 1 to N.
  • If both the numbers from that nested for loops sum to N then,
    • Convert both the number into strings to check whether they contain ‘4’ or not
    • Declare a boolean variable temp with the value true
    • Now check for both the strings and if any one of the strings contains ‘4’, then make temp as false
    • In last if the temp is false then at least one number contains 4 else print those numbers

Below is the implementation of the above approach:

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the two parts
void twoParts(int n)
{
    // Run loop to pick two numbers
    for (int i = 1; i <= n; i++) {
 
        for (int j = 1; j <= n; j++) {
 
            // When both numbers gives sum as n
            if (i + j == n) {
               
                // convert both number in
                // string to check whether
                // they contain 4 or not
                string a = to_string(i);
                string b = to_string(j);
 
                // This will tell that at
                // least one number
                // contains 4 or not
                bool temp = true;
 
                // Check first number contain
                // 4 or not
                for (int k = 0; k < a.size(); k++) {
                    if (a[k] == '4') {
                        temp = false;
                        break;
                    }
                }
 
                // check second number
                // contain 4 or not
                for (int k = 0; k < b.size(); k++) {
                    if (b[k] == '4') {
                        temp = false;
                        break;
                    }
                }
 
                // If both the number doesn't
                // contain 4
                if (temp == true) {
                    cout << i << " " << j << endl;
                    return;
                }
            }
        }
    }
}
 
// Driver Code
int main()
{
    int N = 9441;
    twoParts(N);
    return 0;
}

                    

Java

public class GFG {
    // Function to print the two parts
    static void twoParts(int n) {
        // Run loop to pick two numbers
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                // When both numbers give sum as n
                if (i + j == n) {
                    // Convert both numbers to strings to check whether they contain 4 or not
                    String a = Integer.toString(i);
                    String b = Integer.toString(j);
 
                    // This will tell if at least one number contains 4 or not
                    boolean temp = true;
 
                    // Check if the first number contains 4 or not
                    for (int k = 0; k < a.length(); k++) {
                        if (a.charAt(k) == '4') {
                            temp = false;
                            break;
                        }
                    }
 
                    // Check if the second number contains 4 or not
                    for (int k = 0; k < b.length(); k++) {
                        if (b.charAt(k) == '4') {
                            temp = false;
                            break;
                        }
                    }
 
                    // If both numbers don't contain 4
                    if (temp == true) {
                        System.out.println(i + " " + j);
                        return;
                    }
                }
            }
        }
    }
 
    // Driver Code
    public static void main(String[] args) {
        int N = 9441;
        twoParts(N);
    }
}

                    

Python3

def twoParts(n):
    # Run loop to pick two numbers
    for i in range(1, n+1):
        for j in range(1, n+1):
            # When both numbers gives sum as n
            if i + j == n:
                # convert both number in
                # string to check whether
                # they contain 4 or not
                a = str(i)
                b = str(j)
 
                # This will tell that at
                # least one number
                # contains 4 or not
                temp = True
 
                # Check first number contain
                # 4 or not
                for k in range(len(a)):
                    if a[k] == '4':
                        temp = False
                        break
 
                # check second number
                # contain 4 or not
                for k in range(len(b)):
                    if b[k] == '4':
                        temp = False
                        break
 
                # If both the number doesn't
                # contain 4
                if temp == True:
                    print(i, j)
                    return
 
 
N = 9441
twoParts(N)

                    

C#

using System;
public class GFG
{
    // Function to print the two parts
    static void TwoParts(int n)
    {
        // Run loop to pick two numbers
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                // When both numbers give sum as n
                if (i + j == n)
                {
                    // Convert both numbers to strings to check whether they contain 4 or not
                    string a = i.ToString();
                    string b = j.ToString();
 
                    // This will tell if at least one number contains 4 or not
                    bool temp = true;
 
                    // Check if the first number contains 4 or not
                    for (int k = 0; k < a.Length; k++)
                    {
                        if (a[k] == '4')
                        {
                            temp = false;
                            break;
                        }
                    }
 
                    // Check if the second number contains 4 or not
                    for (int k = 0; k < b.Length; k++)
                    {
                        if (b[k] == '4')
                        {
                            temp = false;
                            break;
                        }
                    }
 
                    // If both numbers don't contain 4
                    if (temp == true)
                    {
                        Console.WriteLine(i + " " + j);
                        return;
                    }
                }
            }
        }
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        int N = 9441;
        TwoParts(N);
    }
}

                    

Javascript

// Function to print the two parts
function twoParts(n) {
   
    // Run loop to pick two numbers
    for (let i = 1; i <= n; i++) {
        for (let j = 1; j <= n; j++) {
           
            // When both numbers give sum as n
            if (i + j === n) {
               
                // Convert both numbers to strings to check whether they contain 4 or not
                const a = i.toString();
                const b = j.toString();
 
                // This will tell if at least one number contains 4 or not
                let temp = true;
 
                // Check if the first number contains 4 or not
                for (let k = 0; k < a.length; k++) {
                    if (a[k] === '4') {
                        temp = false;
                        break;
                    }
                }
 
                // Check if the second number contains 4 or not
                for (let k = 0; k < b.length; k++) {
                    if (b[k] === '4') {
                        temp = false;
                        break;
                    }
                }
 
                // If both numbers don't contain 4
                if (temp === true) {
                    console.log(i + " " + j);
                    return;
                }
            }
        }
    }
}
 
// Driver Code
const N = 9441;
twoParts(N);

                    

Output-

50 9391

Time Complexity: O(N2*maximum length of any number), because of two nested for loops and loop for checking whether any number contains 4 or not 
Auxiliary Space: O(1), because no extra space has been used


Approach: Since number can be too large take the number as string. Divide it into two strings:  

  • For string 1, find all the positions of digit 4 in the string change it to 3 we can also change it to another number.
  • For the second string put 1 at all positions of digit 4 and put 0 at all remaining positions from the 1st position of digit 4 to the end of the string.

Below is the implementation of the above approach: 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the two parts
void twoParts(string str)
{
    int flag = 0;
    string a = "";
 
    // Find the position of 4
    for (int i = 0; i < str.length(); i++) {
        if (str[i] == '4') {
            str[i] = '3';
            a += '1';
            flag = 1;
        }
 
        // If current character is not '4'
        // but appears after the first
        // occurrence of '4'
        else if (flag)
            a += '0';
    }
 
    // Print both the parts
    cout << str << " " << a;
}
 
// Driver code
int main()
{
    string str = "9441";
    twoParts(str);
 
    return 0;
}

                    

Java

// Java implementation of the approach
class GfG {
 
    // Function to print the two parts
    static void twoParts(String str)
    {
        int flag = 0;
        String a = "";
        char[] gfg = str.toCharArray();
 
        // Find the position of 4
        for (int i = 0; i < str.length(); i++) {
            if (gfg[i] == '4') {
                gfg[i] = '3';
                a += '1';
                flag = 1;
            }
 
            // If current character is not '4'
            // but appears after the first
            // occurrence of '4'
            else if (flag != 0)
                a += '0';
        }
 
        str = new String(gfg);
 
        // Print both the parts
        System.out.print(str + " " + a);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String str = "9441";
        twoParts(str);
    }
}
 
// This code is contributed by Rituraj Jain

                    

Python3

# Python3 implementation of the approach
 
# Function to print the two parts
 
 
def twoParts(string):
 
    flag = 0
    a = ""
 
    # Find the position of 4
    for i in range(len(string)):
 
        if (string[i] == '4'):
            string[i] = '3'
            a += '1'
            flag = 1
 
        # If current character is not '4'
        # but appears after the first
        # occurrence of '4'
        elif (flag):
            a += '0'
 
    string = "".join(string)
 
    # Print both the parts
    print(string, a)
 
 
# Driver code
if __name__ == "__main__":
 
    string = "9441"
 
    twoParts(list(string))
 
# This code is contributed by Ryuga

                    

C#

// C# implementation of the approach
using System;
 
class GfG {
 
    // Function to print the two parts
    static void twoParts(string str)
    {
        int flag = 0;
        string a = "";
        char[] gfg = str.ToCharArray();
 
        // Find the position of 4
        for (int i = 0; i < str.Length; i++) {
            if (gfg[i] == '4') {
                gfg[i] = '3';
                a += '1';
                flag = 1;
            }
 
            // If current character is not '4'
            // but appears after the first
            // occurrence of '4'
            else if (flag != 0)
                a += '0';
        }
 
        str = new String(gfg);
 
        // Print both the parts
        Console.WriteLine(str + " " + a);
    }
 
    // Driver code
    static void Main()
    {
        string str = "9441";
        twoParts(str);
    }
}
 
// This code is contributed by mits

                    

Javascript

<script>
// javascript implementation of the approach
 
   
    // Function to print the two parts
     function twoParts( str)
    {
        var flag = 0;
        var a = "";
        var gfg = str.split('') ;
       
        // Find the position of 4
        for (var i = 0; i < str.length; i++)
        {
            if (gfg[i] == '4')
            {
                gfg[i] = '3';
                a += '1';
                flag = 1;
            }
       
            // If current character is not '4'
            // but appears after the first
            // occurrence of '4'
            else if (flag != 0)
                a += '0';
        }
           
       
        // Print both the parts
        document.write(gfg.join('') + " " + a);
    }
   
    // Driver code
 
        var str = "9441";
        twoParts(str);
         
        // This code is contributed by bunnyram19.
        </script>

                    

PHP

<?php
// PHP implementation of the approach
 
// Function to print the two parts
function twoParts($str)
{
    $flag = 0;
    $a = "";
 
    // Find the position of 4
    for ($i = 0; $i < strlen($str); $i++)
    {
        if ($str[$i] == '4')
        {
            $str[$i] = '3';
            $a .= '1';
            $flag = 1;
        }
 
        // If current character is not '4'
        // but appears after the first
        // occurrence of '4'
        else if ($flag)
            $a .= '0';
    }
 
    // Print both the parts
    echo $str . " " . $a;
}
 
// Driver code
$str = "9441";
twoParts($str);
 
// This code is contributed by mits
?>

                    

Output
9331 110






Time Complexity: O(N)
Auxiliary Space: O(N)



Last Updated : 07 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads