Divide a number into two parts

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



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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output:

9331 110

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.