Posted on by Kalkicode
Code Backtracking

Split a large Even number into small Even number

The problem you've tackled in your code is about splitting a large even number into smaller even numbers while maintaining the original order of digits. This means breaking down the given even number into multiple segments such that each segment is also an even number. Your program achieves this by inserting spaces between the digits of the given number in all possible ways, and then checking if each resulting segment is even.

Problem Statement and Description

Given a large even number as a string, the goal is to split it into segments (sub-strings) in such a way that each segment is an even number. The order of the digits should be maintained, and segments should be formed by inserting spaces between digits. The problem is to find all possible valid segmentations of the given even number.

Example

Let's take an example to understand the problem better. Consider the input: "12341872017226".

The even segments [12 34 18 72 0 172 2 6] are:

  • 12
  • 34
  • 18
  • 72
  • 0
  • 172
  • 2
  • 6

The output of shows all possible segmentations of the given even number. Each line represents a valid segmentation of the number into even segments, with spaces between digits.

Idea to Solve

To solve this problem, your code employs a recursive approach. It starts by iterating through the input number and trying to form even segments at each position. If an even segment is found, the algorithm proceeds recursively to find even segments in the remaining part of the string. If a valid segmentation is found, it is printed as part of the output.

Pseudocode

Here's a high-level pseudocode representation of the algorithm:

function isEvenDigit(ch)
    if ch is '0' or '2' or '4' or '6' or '8'
        return true
    else
        return false

function partition(num, result, index, position, k)
    if position is equal to k
        print result
        return

    count = 0
    for i from position to k
        result[index + count] = num[i]

        if count is 0 and num[i] is '0'
            result[index + count + 1] = ' '
            count++
        else if isEvenDigit(num[i]) is true
            result[index + count + 1] = ' '
            partition(num, result, index + count + 2, i + 1, k)
        count++

function splitIntoEvenNumber(num)
    k = length of num
    if k is 0 or last digit of num is not even
        return

    result = array of characters
    partition(num, result, 0, 0, k)

function main
    splitIntoEvenNumber("12341872017226")

Algorithm Explanation

The algorithm starts by checking if the given input number is valid (not empty and ends with an even digit). Then, it initializes an empty character array called result to hold the segments.

The partition function iterates through the input number, and for each digit, it tries to create a segment starting from that digit. If the current digit is '0', it includes a space in the segment to account for zero-padded segments. If the current digit is even, it creates a segment and recursively explores the rest of the number to find further segments.

Program Solution

// C program for
// Split a large Even number into small Even number
#include <stdio.h>
#include <string.h>

// Check whether given character is form of even or not
int isEvenDigit(char ch)
{
    if (ch == '0' || ch == '2' || 
        ch == '4' || ch == '6' || ch == '8')
    {
        // Yes
        return 1;
    }
    // No
    return 0;
}
void partition(char *num, char result[], int index, int position, int k)
{
    if (position == k)
    {
        // Display calculated result
        result[index] = '\0';
        printf("\n %s", result);
        return;
    }
    int count = 0;
    for (int i = position; i < k; ++i)
    {
        result[index + count] = num[i];

        if (count == 0 && num[i] == '0')
        {
            // When if number starts with zero
            // Include space
            result[index + count + 1] = ' ';
            count++;
        }
        else if (isEvenDigit(num[i]) == 1)
        {
            // Include space
            result[index + count + 1] = ' ';
            // Find next even number
            partition(num, result, index + count + 2, i + 1, k);
        }
        count++;
    }
}
void splitIntoEvenNumber(char *num)
{
    int k = strlen(num);
    if (k == 0 || !isEvenDigit(num[k - 1]))
    {
        return;
    }
    // Collect result
    char result[(k *2) + 1];
    printf("\n Given number : %s", num);
    partition(num, result, 0, 0, k);
}
int main()
{
    // Test 
    splitIntoEvenNumber("12341872017226");
    return 0;
}

Output

 Given number : 12341872017226
 12 34 18 72 0 172 2 6
 12 34 18 72 0 172 26
 12 34 18 72 0 1722 6
 12 34 18 72 0 17226
 12 34 18 720 172 2 6
 12 34 18 720 172 26
 12 34 18 720 1722 6
 12 34 18 720 17226
 12 34 18 720172 2 6
 12 34 18 720172 26
 12 34 18 7201722 6
 12 34 18 72017226
 12 34 1872 0 172 2 6
 12 34 1872 0 172 26
 12 34 1872 0 1722 6
 12 34 1872 0 17226
 12 34 18720 172 2 6
 12 34 18720 172 26
 12 34 18720 1722 6
 12 34 18720 17226
 12 34 18720172 2 6
 12 34 18720172 26
 12 34 187201722 6
 12 34 1872017226
 12 3418 72 0 172 2 6
 12 3418 72 0 172 26
 12 3418 72 0 1722 6
 12 3418 72 0 17226
 12 3418 720 172 2 6
 12 3418 720 172 26
 12 3418 720 1722 6
 12 3418 720 17226
 12 3418 720172 2 6
 12 3418 720172 26
 12 3418 7201722 6
 12 3418 72017226
 12 341872 0 172 2 6
 12 341872 0 172 26
 12 341872 0 1722 6
 12 341872 0 17226
 12 3418720 172 2 6
 12 3418720 172 26
 12 3418720 1722 6
 12 3418720 17226
 12 3418720172 2 6
 12 3418720172 26
 12 34187201722 6
 12 341872017226
 1234 18 72 0 172 2 6
 1234 18 72 0 172 26
 1234 18 72 0 1722 6
 1234 18 72 0 17226
 1234 18 720 172 2 6
 1234 18 720 172 26
 1234 18 720 1722 6
 1234 18 720 17226
 1234 18 720172 2 6
 1234 18 720172 26
 1234 18 7201722 6
 1234 18 72017226
 1234 1872 0 172 2 6
 1234 1872 0 172 26
 1234 1872 0 1722 6
 1234 1872 0 17226
 1234 18720 172 2 6
 1234 18720 172 26
 1234 18720 1722 6
 1234 18720 17226
 1234 18720172 2 6
 1234 18720172 26
 1234 187201722 6
 1234 1872017226
 123418 72 0 172 2 6
 123418 72 0 172 26
 123418 72 0 1722 6
 123418 72 0 17226
 123418 720 172 2 6
 123418 720 172 26
 123418 720 1722 6
 123418 720 17226
 123418 720172 2 6
 123418 720172 26
 123418 7201722 6
 123418 72017226
 12341872 0 172 2 6
 12341872 0 172 26
 12341872 0 1722 6
 12341872 0 17226
 123418720 172 2 6
 123418720 172 26
 123418720 1722 6
 123418720 17226
 123418720172 2 6
 123418720172 26
 1234187201722 6
 12341872017226
/*
    Java program
    Split a large Even number into small Even number
*/
public class Splitting
{
    // Check whether given character is form of even or not
    public boolean isEvenDigit(char ch)
    {
        if (ch == '0' || ch == '2' || ch == '4' || 
            ch == '6' || ch == '8')
        {
            // Yes
            return true;
        }
        // No
        return false;
    }
    public void displayResult(char[] result, int n)
    {
        for (int i = 0; i < n; ++i)
        {
            System.out.print(result[i]);
        }
        System.out.print("\n");
    }
    public void partition(String num, char[] result, 
                         int index, int position, int k)
    {
        if (position == k)
        {
            displayResult(result, index);
            return;
        }
        int count = 0;
        for (int i = position; i < k; ++i)
        {
            result[index + count] = num.charAt(i);
            if (count == 0 && num.charAt(i) == '0')
            {
                // When if number starts with zero
                // Include space
                result[index + count + 1] = ' ';
                count++;
            }
            else if (isEvenDigit(num.charAt(i)))
            {
                // Include space
                result[index + count + 1] = ' ';
                // Find next even number
                partition(num, result, index + count + 2, i + 1, k);
            }
            count++;
        }
    }
    public void splitIntoEvenNumber(String num)
    {
        int k = num.length();
        if (k == 0 || !isEvenDigit(num.charAt(k - 1)))
        {
            return;
        }
        // Collect result
        char[] result = new char[(k * 2) + 1];
        System.out.println("\n Given number : " + num);
        partition(num, result, 0, 0, k);
    }
    public static void main(String[] args)
    {
        Splitting task = new Splitting();
        // Test
        task.splitIntoEvenNumber("12341872017226");
    }
}

Output

 Given number : 12341872017226
12 34 18 72 0 172 2 6
12 34 18 72 0 172 26
12 34 18 72 0 1722 6
12 34 18 72 0 17226
12 34 18 720 172 2 6
12 34 18 720 172 26
12 34 18 720 1722 6
12 34 18 720 17226
12 34 18 720172 2 6
12 34 18 720172 26
12 34 18 7201722 6
12 34 18 72017226
12 34 1872 0 172 2 6
12 34 1872 0 172 26
12 34 1872 0 1722 6
12 34 1872 0 17226
12 34 18720 172 2 6
12 34 18720 172 26
12 34 18720 1722 6
12 34 18720 17226
12 34 18720172 2 6
12 34 18720172 26
12 34 187201722 6
12 34 1872017226
12 3418 72 0 172 2 6
12 3418 72 0 172 26
12 3418 72 0 1722 6
12 3418 72 0 17226
12 3418 720 172 2 6
12 3418 720 172 26
12 3418 720 1722 6
12 3418 720 17226
12 3418 720172 2 6
12 3418 720172 26
12 3418 7201722 6
12 3418 72017226
12 341872 0 172 2 6
12 341872 0 172 26
12 341872 0 1722 6
12 341872 0 17226
12 3418720 172 2 6
12 3418720 172 26
12 3418720 1722 6
12 3418720 17226
12 3418720172 2 6
12 3418720172 26
12 34187201722 6
12 341872017226
1234 18 72 0 172 2 6
1234 18 72 0 172 26
1234 18 72 0 1722 6
1234 18 72 0 17226
1234 18 720 172 2 6
1234 18 720 172 26
1234 18 720 1722 6
1234 18 720 17226
1234 18 720172 2 6
1234 18 720172 26
1234 18 7201722 6
1234 18 72017226
1234 1872 0 172 2 6
1234 1872 0 172 26
1234 1872 0 1722 6
1234 1872 0 17226
1234 18720 172 2 6
1234 18720 172 26
1234 18720 1722 6
1234 18720 17226
1234 18720172 2 6
1234 18720172 26
1234 187201722 6
1234 1872017226
123418 72 0 172 2 6
123418 72 0 172 26
123418 72 0 1722 6
123418 72 0 17226
123418 720 172 2 6
123418 720 172 26
123418 720 1722 6
123418 720 17226
123418 720172 2 6
123418 720172 26
123418 7201722 6
123418 72017226
12341872 0 172 2 6
12341872 0 172 26
12341872 0 1722 6
12341872 0 17226
123418720 172 2 6
123418720 172 26
123418720 1722 6
123418720 17226
123418720172 2 6
123418720172 26
1234187201722 6
12341872017226
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
    C++ program
    Split a large Even number into small Even number
*/
class Splitting
{
    public:
        // Check whether given character is form of even or not
        bool isEvenDigit(char ch)
        {
            if (ch == '0' || ch == '2' || ch == '4' || 
                ch == '6' || ch == '8')
            {
                // Yes
                return true;
            }
            // No
            return false;
        }
    void displayResult(char result[], int n)
    {
        for (int i = 0; i < n; ++i)
        {
            cout << result[i];
        }
        cout << "\n";
    }
    void partition(string num, char result[], 
                    int index, int position, int k)
    {
        if (position == k)
        {
            this->displayResult(result, index);
            return;
        }
        int count = 0;
        for (int i = position; i < k; ++i)
        {
            result[index + count] = num[i];
            if (count == 0 && num[i] == '0')
            {
                // When if number starts with zero
                // Include space
                result[index + count + 1] = ' ';
                count++;
            }
            else if (this->isEvenDigit(num[i]))
            {
                // Include space
                result[index + count + 1] = ' ';
                // Find next even number
                this->partition(num, result, index + count + 2, i + 1, k);
            }
            count++;
        }
    }
    void splitIntoEvenNumber(string num)
    {
        int k = num.length();
        if (k == 0 || !this->isEvenDigit(num[k - 1]))
        {
            return;
        }
        // Collect result
        char result[(k *2) + 1];
        cout << "\n Given number : " << num << endl;
        this->partition(num, result, 0, 0, k);
    }
};
int main()
{
    Splitting *task = new Splitting();
    // Test
    task->splitIntoEvenNumber("12341872017226");
    return 0;
}

Output

 Given number : 12341872017226
12 34 18 72 0 172 2 6
12 34 18 72 0 172 26
12 34 18 72 0 1722 6
12 34 18 72 0 17226
12 34 18 720 172 2 6
12 34 18 720 172 26
12 34 18 720 1722 6
12 34 18 720 17226
12 34 18 720172 2 6
12 34 18 720172 26
12 34 18 7201722 6
12 34 18 72017226
12 34 1872 0 172 2 6
12 34 1872 0 172 26
12 34 1872 0 1722 6
12 34 1872 0 17226
12 34 18720 172 2 6
12 34 18720 172 26
12 34 18720 1722 6
12 34 18720 17226
12 34 18720172 2 6
12 34 18720172 26
12 34 187201722 6
12 34 1872017226
12 3418 72 0 172 2 6
12 3418 72 0 172 26
12 3418 72 0 1722 6
12 3418 72 0 17226
12 3418 720 172 2 6
12 3418 720 172 26
12 3418 720 1722 6
12 3418 720 17226
12 3418 720172 2 6
12 3418 720172 26
12 3418 7201722 6
12 3418 72017226
12 341872 0 172 2 6
12 341872 0 172 26
12 341872 0 1722 6
12 341872 0 17226
12 3418720 172 2 6
12 3418720 172 26
12 3418720 1722 6
12 3418720 17226
12 3418720172 2 6
12 3418720172 26
12 34187201722 6
12 341872017226
1234 18 72 0 172 2 6
1234 18 72 0 172 26
1234 18 72 0 1722 6
1234 18 72 0 17226
1234 18 720 172 2 6
1234 18 720 172 26
1234 18 720 1722 6
1234 18 720 17226
1234 18 720172 2 6
1234 18 720172 26
1234 18 7201722 6
1234 18 72017226
1234 1872 0 172 2 6
1234 1872 0 172 26
1234 1872 0 1722 6
1234 1872 0 17226
1234 18720 172 2 6
1234 18720 172 26
1234 18720 1722 6
1234 18720 17226
1234 18720172 2 6
1234 18720172 26
1234 187201722 6
1234 1872017226
123418 72 0 172 2 6
123418 72 0 172 26
123418 72 0 1722 6
123418 72 0 17226
123418 720 172 2 6
123418 720 172 26
123418 720 1722 6
123418 720 17226
123418 720172 2 6
123418 720172 26
123418 7201722 6
123418 72017226
12341872 0 172 2 6
12341872 0 172 26
12341872 0 1722 6
12341872 0 17226
123418720 172 2 6
123418720 172 26
123418720 1722 6
123418720 17226
123418720172 2 6
123418720172 26
1234187201722 6
12341872017226
// Include namespace system
using System;
/*
    Csharp program
    Split a large Even number into small Even number
*/
public class Splitting
{
    // Check whether given character is form of even or not
    public Boolean isEvenDigit(char ch)
    {
        if (ch == '0' || ch == '2' || ch == '4' || 
            ch == '6' || ch == '8')
        {
            // Yes
            return true;
        }
        // No
        return false;
    }
    public void displayResult(char[] result, int n)
    {
        for (int i = 0; i < n; ++i)
        {
            Console.Write(result[i]);
        }
        Console.Write("\n");
    }
    public void partition(String num, char[] result, 
        int index, int position, int k)
    {
        if (position == k)
        {
            this.displayResult(result, index);
            return;
        }
        int count = 0;
        for (int i = position; i < k; ++i)
        {
            result[index + count] = num[i];
            if (count == 0 && num[i] == '0')
            {
                // When if number starts with zero
                // Include space
                result[index + count + 1] = ' ';
                count++;
            }
            else if (this.isEvenDigit(num[i]))
            {
                // Include space
                result[index + count + 1] = ' ';
                // Find next even number
                this.partition(num, result, index + count + 2, i + 1, k);
            }
            count++;
        }
    }
    public void splitIntoEvenNumber(String num)
    {
        int k = num.Length;
        if (k == 0 || !this.isEvenDigit(num[k - 1]))
        {
            return;
        }
        // Collect result
        char[] result = new char[(k * 2) + 1];
        Console.WriteLine("\n Given number : " + num);
        this.partition(num, result, 0, 0, k);
    }
    public static void Main(String[] args)
    {
        Splitting task = new Splitting();
        // Test
        task.splitIntoEvenNumber("12341872017226");
    }
}

Output

 Given number : 12341872017226
12 34 18 72 0 172 2 6
12 34 18 72 0 172 26
12 34 18 72 0 1722 6
12 34 18 72 0 17226
12 34 18 720 172 2 6
12 34 18 720 172 26
12 34 18 720 1722 6
12 34 18 720 17226
12 34 18 720172 2 6
12 34 18 720172 26
12 34 18 7201722 6
12 34 18 72017226
12 34 1872 0 172 2 6
12 34 1872 0 172 26
12 34 1872 0 1722 6
12 34 1872 0 17226
12 34 18720 172 2 6
12 34 18720 172 26
12 34 18720 1722 6
12 34 18720 17226
12 34 18720172 2 6
12 34 18720172 26
12 34 187201722 6
12 34 1872017226
12 3418 72 0 172 2 6
12 3418 72 0 172 26
12 3418 72 0 1722 6
12 3418 72 0 17226
12 3418 720 172 2 6
12 3418 720 172 26
12 3418 720 1722 6
12 3418 720 17226
12 3418 720172 2 6
12 3418 720172 26
12 3418 7201722 6
12 3418 72017226
12 341872 0 172 2 6
12 341872 0 172 26
12 341872 0 1722 6
12 341872 0 17226
12 3418720 172 2 6
12 3418720 172 26
12 3418720 1722 6
12 3418720 17226
12 3418720172 2 6
12 3418720172 26
12 34187201722 6
12 341872017226
1234 18 72 0 172 2 6
1234 18 72 0 172 26
1234 18 72 0 1722 6
1234 18 72 0 17226
1234 18 720 172 2 6
1234 18 720 172 26
1234 18 720 1722 6
1234 18 720 17226
1234 18 720172 2 6
1234 18 720172 26
1234 18 7201722 6
1234 18 72017226
1234 1872 0 172 2 6
1234 1872 0 172 26
1234 1872 0 1722 6
1234 1872 0 17226
1234 18720 172 2 6
1234 18720 172 26
1234 18720 1722 6
1234 18720 17226
1234 18720172 2 6
1234 18720172 26
1234 187201722 6
1234 1872017226
123418 72 0 172 2 6
123418 72 0 172 26
123418 72 0 1722 6
123418 72 0 17226
123418 720 172 2 6
123418 720 172 26
123418 720 1722 6
123418 720 17226
123418 720172 2 6
123418 720172 26
123418 7201722 6
123418 72017226
12341872 0 172 2 6
12341872 0 172 26
12341872 0 1722 6
12341872 0 17226
123418720 172 2 6
123418720 172 26
123418720 1722 6
123418720 17226
123418720172 2 6
123418720172 26
1234187201722 6
12341872017226
package main
import "fmt"
/*
    Go program
    Split a large Even number into small Even number
*/
type Splitting struct {}
func getSplitting() * Splitting {
    var me *Splitting = &Splitting {}
    return me
}
// Check whether given character is form of even or not
func(this Splitting) isEvenDigit(ch byte) bool {
    if ch == '0' || ch == '2' || ch == '4' || ch == '6' || ch == '8' {
        // Yes
        return true
    }
    // No
    return false
}
func(this Splitting) displayResult(result[] byte, n int) {
    for i := 0 ; i < n ; i++ {
        fmt.Printf("%c",result[i])
    }
    fmt.Print("\n")
}
func(this Splitting) partition(num string, result[] byte, index int, 
    position int, k int) {
    if position == k {
        this.displayResult(result, index)
        return
    }
    var count int = 0
    for i := position ; i < k ; i++ {
        result[index + count] = num[i]
        if count == 0 && num[i] == '0' {
            // When if number starts with zero
            // Include space
            result[index + count + 1] = ' '
            count++
        } else if this.isEvenDigit(num[i]) {
            // Include space
            result[index + count + 1] = ' '
            // Find next even number
            this.partition(num, result, index + count + 2, i + 1, k)
        }
        count++
    }
}
func(this Splitting) splitIntoEvenNumber(num string) {
    var k int = len(num)
    if k == 0 || !this.isEvenDigit(num[k - 1]) {
        return
    }
    // Collect result
    var result = make([] byte, (k * 2) + 1)
    fmt.Println("\n Given number : ", num)
    this.partition(num, result, 0, 0, k)
}
func main() {
    var task * Splitting = getSplitting()
    // Test
    task.splitIntoEvenNumber("12341872017226")
}

Output

 Given number : 12341872017226
12 34 18 72 0 172 2 6
12 34 18 72 0 172 26
12 34 18 72 0 1722 6
12 34 18 72 0 17226
12 34 18 720 172 2 6
12 34 18 720 172 26
12 34 18 720 1722 6
12 34 18 720 17226
12 34 18 720172 2 6
12 34 18 720172 26
12 34 18 7201722 6
12 34 18 72017226
12 34 1872 0 172 2 6
12 34 1872 0 172 26
12 34 1872 0 1722 6
12 34 1872 0 17226
12 34 18720 172 2 6
12 34 18720 172 26
12 34 18720 1722 6
12 34 18720 17226
12 34 18720172 2 6
12 34 18720172 26
12 34 187201722 6
12 34 1872017226
12 3418 72 0 172 2 6
12 3418 72 0 172 26
12 3418 72 0 1722 6
12 3418 72 0 17226
12 3418 720 172 2 6
12 3418 720 172 26
12 3418 720 1722 6
12 3418 720 17226
12 3418 720172 2 6
12 3418 720172 26
12 3418 7201722 6
12 3418 72017226
12 341872 0 172 2 6
12 341872 0 172 26
12 341872 0 1722 6
12 341872 0 17226
12 3418720 172 2 6
12 3418720 172 26
12 3418720 1722 6
12 3418720 17226
12 3418720172 2 6
12 3418720172 26
12 34187201722 6
12 341872017226
1234 18 72 0 172 2 6
1234 18 72 0 172 26
1234 18 72 0 1722 6
1234 18 72 0 17226
1234 18 720 172 2 6
1234 18 720 172 26
1234 18 720 1722 6
1234 18 720 17226
1234 18 720172 2 6
1234 18 720172 26
1234 18 7201722 6
1234 18 72017226
1234 1872 0 172 2 6
1234 1872 0 172 26
1234 1872 0 1722 6
1234 1872 0 17226
1234 18720 172 2 6
1234 18720 172 26
1234 18720 1722 6
1234 18720 17226
1234 18720172 2 6
1234 18720172 26
1234 187201722 6
1234 1872017226
123418 72 0 172 2 6
123418 72 0 172 26
123418 72 0 1722 6
123418 72 0 17226
123418 720 172 2 6
123418 720 172 26
123418 720 1722 6
123418 720 17226
123418 720172 2 6
123418 720172 26
123418 7201722 6
123418 72017226
12341872 0 172 2 6
12341872 0 172 26
12341872 0 1722 6
12341872 0 17226
123418720 172 2 6
123418720 172 26
123418720 1722 6
123418720 17226
123418720172 2 6
123418720172 26
1234187201722 6
12341872017226
<?php
/*
    Php program
    Split a large Even number into small Even number
*/
class Splitting
{
    // Check whether given character is form of even or not
    public  function isEvenDigit($ch)
    {
        if ($ch == '0' || $ch == '2' || $ch == '4' ||
            $ch == '6' || $ch == '8')
        {
            // Yes
            return true;
        }
        // No
        return false;
    }
    public  function displayResult($result, $n)
    {
        for ($i = 0; $i < $n; ++$i)
        {
            echo($result[$i]);
        }
        echo("\n");
    }
    public  function partition($num, $result, $index, $position, $k)
    {
        if ($position == $k)
        {
            $this->displayResult($result, $index);
            return;
        }
        $count = 0;
        for ($i = $position; $i < $k; ++$i)
        {
            $result[$index + $count] = $num[$i];
            if ($count == 0 && $num[$i] == '0')
            {
                // When if number starts with zero
                // Include space
                $result[$index + $count + 1] = ' ';
                $count++;
            }
            else if ($this->isEvenDigit($num[$i]))
            {
                // Include space
                $result[$index + $count + 1] = ' ';
                // Find next even number
                $this->partition($num, $result, 
                                 $index + $count + 2, $i + 1, $k);
            }
            $count++;
        }
    }
    public  function splitIntoEvenNumber($num)
    {
        $k = strlen($num);
        if ($k == 0 || !$this->isEvenDigit($num[$k - 1]))
        {
            return;
        }
        // Collect result
        $result = array_fill(0, ($k * 2) + 1, ' ');
        echo("\n Given number : ".$num.
            "\n");
        $this->partition($num, $result, 0, 0, $k);
    }
}

function main()
{
    $task = new Splitting();
    // Test
    $task->splitIntoEvenNumber("12341872017226");
}
main();

Output

 Given number : 12341872017226
12 34 18 72 0 172 2 6
12 34 18 72 0 172 26
12 34 18 72 0 1722 6
12 34 18 72 0 17226
12 34 18 720 172 2 6
12 34 18 720 172 26
12 34 18 720 1722 6
12 34 18 720 17226
12 34 18 720172 2 6
12 34 18 720172 26
12 34 18 7201722 6
12 34 18 72017226
12 34 1872 0 172 2 6
12 34 1872 0 172 26
12 34 1872 0 1722 6
12 34 1872 0 17226
12 34 18720 172 2 6
12 34 18720 172 26
12 34 18720 1722 6
12 34 18720 17226
12 34 18720172 2 6
12 34 18720172 26
12 34 187201722 6
12 34 1872017226
12 3418 72 0 172 2 6
12 3418 72 0 172 26
12 3418 72 0 1722 6
12 3418 72 0 17226
12 3418 720 172 2 6
12 3418 720 172 26
12 3418 720 1722 6
12 3418 720 17226
12 3418 720172 2 6
12 3418 720172 26
12 3418 7201722 6
12 3418 72017226
12 341872 0 172 2 6
12 341872 0 172 26
12 341872 0 1722 6
12 341872 0 17226
12 3418720 172 2 6
12 3418720 172 26
12 3418720 1722 6
12 3418720 17226
12 3418720172 2 6
12 3418720172 26
12 34187201722 6
12 341872017226
1234 18 72 0 172 2 6
1234 18 72 0 172 26
1234 18 72 0 1722 6
1234 18 72 0 17226
1234 18 720 172 2 6
1234 18 720 172 26
1234 18 720 1722 6
1234 18 720 17226
1234 18 720172 2 6
1234 18 720172 26
1234 18 7201722 6
1234 18 72017226
1234 1872 0 172 2 6
1234 1872 0 172 26
1234 1872 0 1722 6
1234 1872 0 17226
1234 18720 172 2 6
1234 18720 172 26
1234 18720 1722 6
1234 18720 17226
1234 18720172 2 6
1234 18720172 26
1234 187201722 6
1234 1872017226
123418 72 0 172 2 6
123418 72 0 172 26
123418 72 0 1722 6
123418 72 0 17226
123418 720 172 2 6
123418 720 172 26
123418 720 1722 6
123418 720 17226
123418 720172 2 6
123418 720172 26
123418 7201722 6
123418 72017226
12341872 0 172 2 6
12341872 0 172 26
12341872 0 1722 6
12341872 0 17226
123418720 172 2 6
123418720 172 26
123418720 1722 6
123418720 17226
123418720172 2 6
123418720172 26
1234187201722 6
12341872017226
/*
    Node JS program
    Split a large Even number into small Even number
*/
class Splitting
{
    // Check whether given character is form of even or not
    isEvenDigit(ch)
    {
        if (ch == '0' || ch == '2' || ch == '4' || 
            ch == '6' || ch == '8')
        {
            // Yes
            return true;
        }
        // No
        return false;
    }
    displayResult(result, n)
    {
        for (var i = 0; i < n; ++i)
        {
            process.stdout.write(result[i]);
        }
        process.stdout.write("\n");
    }
    partition(num, result, index, position, k)
    {
        if (position == k)
        {
            this.displayResult(result, index);
            return;
        }
        var count = 0;
        for (var i = position; i < k; ++i)
        {
            result[index + count] = num.charAt(i);
            if (count == 0 && num.charAt(i) == '0')
            {
                // When if number starts with zero
                // Include space
                result[index + count + 1] = ' ';
                count++;
            }
            else if (this.isEvenDigit(num.charAt(i)))
            {
                // Include space
                result[index + count + 1] = ' ';
                // Find next even number
                this.partition(num, result, 
                               index + count + 2, i + 1, k);
            }
            count++;
        }
    }
    splitIntoEvenNumber(num)
    {
        var k = num.length;
        if (k == 0 || !this.isEvenDigit(num.charAt(k - 1)))
        {
            return;
        }
        // Collect result
        var result = Array((k * 2) + 1).fill(' ');
        console.log("\n Given number : " + num);
        this.partition(num, result, 0, 0, k);
    }
}

function main()
{
    var task = new Splitting();
    // Test
    task.splitIntoEvenNumber("12341872017226");
}
main();

Output

 Given number : 12341872017226
12 34 18 72 0 172 2 6
12 34 18 72 0 172 26
12 34 18 72 0 1722 6
12 34 18 72 0 17226
12 34 18 720 172 2 6
12 34 18 720 172 26
12 34 18 720 1722 6
12 34 18 720 17226
12 34 18 720172 2 6
12 34 18 720172 26
12 34 18 7201722 6
12 34 18 72017226
12 34 1872 0 172 2 6
12 34 1872 0 172 26
12 34 1872 0 1722 6
12 34 1872 0 17226
12 34 18720 172 2 6
12 34 18720 172 26
12 34 18720 1722 6
12 34 18720 17226
12 34 18720172 2 6
12 34 18720172 26
12 34 187201722 6
12 34 1872017226
12 3418 72 0 172 2 6
12 3418 72 0 172 26
12 3418 72 0 1722 6
12 3418 72 0 17226
12 3418 720 172 2 6
12 3418 720 172 26
12 3418 720 1722 6
12 3418 720 17226
12 3418 720172 2 6
12 3418 720172 26
12 3418 7201722 6
12 3418 72017226
12 341872 0 172 2 6
12 341872 0 172 26
12 341872 0 1722 6
12 341872 0 17226
12 3418720 172 2 6
12 3418720 172 26
12 3418720 1722 6
12 3418720 17226
12 3418720172 2 6
12 3418720172 26
12 34187201722 6
12 341872017226
1234 18 72 0 172 2 6
1234 18 72 0 172 26
1234 18 72 0 1722 6
1234 18 72 0 17226
1234 18 720 172 2 6
1234 18 720 172 26
1234 18 720 1722 6
1234 18 720 17226
1234 18 720172 2 6
1234 18 720172 26
1234 18 7201722 6
1234 18 72017226
1234 1872 0 172 2 6
1234 1872 0 172 26
1234 1872 0 1722 6
1234 1872 0 17226
1234 18720 172 2 6
1234 18720 172 26
1234 18720 1722 6
1234 18720 17226
1234 18720172 2 6
1234 18720172 26
1234 187201722 6
1234 1872017226
123418 72 0 172 2 6
123418 72 0 172 26
123418 72 0 1722 6
123418 72 0 17226
123418 720 172 2 6
123418 720 172 26
123418 720 1722 6
123418 720 17226
123418 720172 2 6
123418 720172 26
123418 7201722 6
123418 72017226
12341872 0 172 2 6
12341872 0 172 26
12341872 0 1722 6
12341872 0 17226
123418720 172 2 6
123418720 172 26
123418720 1722 6
123418720 17226
123418720172 2 6
123418720172 26
1234187201722 6
12341872017226
#    Python 3 program
#    Split a large Even number into small Even number
class Splitting :
    #  Check whether given character is form of even or not
    def isEvenDigit(self, ch) :
        if (ch == '0'
            or ch == '2'
            or ch == '4'
            or ch == '6'
            or ch == '8') :
            #  Yes
            return True
        
        #  No
        return False
    
    def displayResult(self, result, n) :
        i = 0
        while (i < n) :
            print(result[i], end = "")
            i += 1
        
        print(end = "\n")
    
    def partition(self, num, result, index, position, k) :
        if (position == k) :
            self.displayResult(result, index)
            return
        
        count = 0
        i = position
        while (i < k) :
            result[index + count] = num[i]
            if (count == 0 and num[i] == '0') :
                #  When if number starts with zero
                #  Include space
                result[index + count + 1] = ' '
                count += 1
            elif (self.isEvenDigit(num[i])) :
                #  Include space
                result[index + count + 1] = ' '
                #  Find next even number
                self.partition(num, result, 
                               index + count + 2, i + 1, k)
            
            count += 1
            i += 1
        
    
    def splitIntoEvenNumber(self, num) :
        k = len(num)
        if (k == 0 or not self.isEvenDigit(num[k - 1])) :
            return
        
        #  Collect result
        result = [ ' ' ] * ((k * 2) + 1)
        print("\n Given number : ", num)
        self.partition(num, result, 0, 0, k)
    

def main() :
    task = Splitting()
    #  Test
    task.splitIntoEvenNumber("12341872017226")

if __name__ == "__main__": main()

Output

 Given number :  12341872017226
12 34 18 72 0 172 2 6
12 34 18 72 0 172 26
12 34 18 72 0 1722 6
12 34 18 72 0 17226
12 34 18 720 172 2 6
12 34 18 720 172 26
12 34 18 720 1722 6
12 34 18 720 17226
12 34 18 720172 2 6
12 34 18 720172 26
12 34 18 7201722 6
12 34 18 72017226
12 34 1872 0 172 2 6
12 34 1872 0 172 26
12 34 1872 0 1722 6
12 34 1872 0 17226
12 34 18720 172 2 6
12 34 18720 172 26
12 34 18720 1722 6
12 34 18720 17226
12 34 18720172 2 6
12 34 18720172 26
12 34 187201722 6
12 34 1872017226
12 3418 72 0 172 2 6
12 3418 72 0 172 26
12 3418 72 0 1722 6
12 3418 72 0 17226
12 3418 720 172 2 6
12 3418 720 172 26
12 3418 720 1722 6
12 3418 720 17226
12 3418 720172 2 6
12 3418 720172 26
12 3418 7201722 6
12 3418 72017226
12 341872 0 172 2 6
12 341872 0 172 26
12 341872 0 1722 6
12 341872 0 17226
12 3418720 172 2 6
12 3418720 172 26
12 3418720 1722 6
12 3418720 17226
12 3418720172 2 6
12 3418720172 26
12 34187201722 6
12 341872017226
1234 18 72 0 172 2 6
1234 18 72 0 172 26
1234 18 72 0 1722 6
1234 18 72 0 17226
1234 18 720 172 2 6
1234 18 720 172 26
1234 18 720 1722 6
1234 18 720 17226
1234 18 720172 2 6
1234 18 720172 26
1234 18 7201722 6
1234 18 72017226
1234 1872 0 172 2 6
1234 1872 0 172 26
1234 1872 0 1722 6
1234 1872 0 17226
1234 18720 172 2 6
1234 18720 172 26
1234 18720 1722 6
1234 18720 17226
1234 18720172 2 6
1234 18720172 26
1234 187201722 6
1234 1872017226
123418 72 0 172 2 6
123418 72 0 172 26
123418 72 0 1722 6
123418 72 0 17226
123418 720 172 2 6
123418 720 172 26
123418 720 1722 6
123418 720 17226
123418 720172 2 6
123418 720172 26
123418 7201722 6
123418 72017226
12341872 0 172 2 6
12341872 0 172 26
12341872 0 1722 6
12341872 0 17226
123418720 172 2 6
123418720 172 26
123418720 1722 6
123418720 17226
123418720172 2 6
123418720172 26
1234187201722 6
12341872017226
#    Ruby program
#    Split a large Even number into small Even number
class Splitting 
    #  Check whether given character is form of even or not
    def isEvenDigit(ch) 
        if (ch == '0' || ch == '2' || ch == '4' || 
            ch == '6' || ch == '8') 
            #  Yes
            return true
        end

        #  No
        return false
    end

    def displayResult(result, n) 
        i = 0
        while (i < n) 
            print(result[i])
            i += 1
        end

        print("\n")
    end

    def partition(num, result, index, position, k) 
        if (position == k) 
            self.displayResult(result, index)
            return
        end

        count = 0
        i = position
        while (i < k) 
            result[index + count] = num[i]
            if (count == 0 && num[i] == '0') 
                #  When if number starts with zero
                #  Include space
                result[index + count + 1] = ' '
                count += 1
            elsif (self.isEvenDigit(num[i])) 
                #  Include space
                result[index + count + 1] = ' '
                #  Find next even number
                self.partition(num, result, index + count + 2, i + 1, k)
            end

            count += 1
            i += 1
        end

    end

    def splitIntoEvenNumber(num) 
        k = num.length
        if (k == 0 || !self.isEvenDigit(num[k - 1])) 
            return
        end

        #  Collect result
        result = Array.new((k * 2) + 1) { ' '
        }
        print("\n Given number : ", num, "\n")
        self.partition(num, result, 0, 0, k)
    end

end

def main() 
    task = Splitting.new()
    #  Test
    task.splitIntoEvenNumber("12341872017226")
end

main()

Output

 Given number : 12341872017226
12 34 18 72 0 172 2 6 
12 34 18 72 0 172 26 
12 34 18 72 0 1722 6 
12 34 18 72 0 17226 
12 34 18 720 172 2 6 
12 34 18 720 172 26 
12 34 18 720 1722 6 
12 34 18 720 17226 
12 34 18 720172 2 6 
12 34 18 720172 26 
12 34 18 7201722 6 
12 34 18 72017226 
12 34 1872 0 172 2 6 
12 34 1872 0 172 26 
12 34 1872 0 1722 6 
12 34 1872 0 17226 
12 34 18720 172 2 6 
12 34 18720 172 26 
12 34 18720 1722 6 
12 34 18720 17226 
12 34 18720172 2 6 
12 34 18720172 26 
12 34 187201722 6 
12 34 1872017226 
12 3418 72 0 172 2 6 
12 3418 72 0 172 26 
12 3418 72 0 1722 6 
12 3418 72 0 17226 
12 3418 720 172 2 6 
12 3418 720 172 26 
12 3418 720 1722 6 
12 3418 720 17226 
12 3418 720172 2 6 
12 3418 720172 26 
12 3418 7201722 6 
12 3418 72017226 
12 341872 0 172 2 6 
12 341872 0 172 26 
12 341872 0 1722 6 
12 341872 0 17226 
12 3418720 172 2 6 
12 3418720 172 26 
12 3418720 1722 6 
12 3418720 17226 
12 3418720172 2 6 
12 3418720172 26 
12 34187201722 6 
12 341872017226 
1234 18 72 0 172 2 6 
1234 18 72 0 172 26 
1234 18 72 0 1722 6 
1234 18 72 0 17226 
1234 18 720 172 2 6 
1234 18 720 172 26 
1234 18 720 1722 6 
1234 18 720 17226 
1234 18 720172 2 6 
1234 18 720172 26 
1234 18 7201722 6 
1234 18 72017226 
1234 1872 0 172 2 6 
1234 1872 0 172 26 
1234 1872 0 1722 6 
1234 1872 0 17226 
1234 18720 172 2 6 
1234 18720 172 26 
1234 18720 1722 6 
1234 18720 17226 
1234 18720172 2 6 
1234 18720172 26 
1234 187201722 6 
1234 1872017226 
123418 72 0 172 2 6 
123418 72 0 172 26 
123418 72 0 1722 6 
123418 72 0 17226 
123418 720 172 2 6 
123418 720 172 26 
123418 720 1722 6 
123418 720 17226 
123418 720172 2 6 
123418 720172 26 
123418 7201722 6 
123418 72017226 
12341872 0 172 2 6 
12341872 0 172 26 
12341872 0 1722 6 
12341872 0 17226 
123418720 172 2 6 
123418720 172 26 
123418720 1722 6 
123418720 17226 
123418720172 2 6 
123418720172 26 
1234187201722 6 
12341872017226 
import scala.collection.mutable._;
/*
    Scala program
    Split a large Even number into small Even number
*/
class Splitting()
{
    // Check whether given character is form of even or not
    def isEvenDigit(ch: Char): Boolean = {
        if (ch == '0' || ch == '2' || ch == '4' || 
            ch == '6' || ch == '8')
        {
            // Yes
            return true;
        }
        // No
        return false;
    }
    def displayResult(result: Array[Char], n: Int): Unit = {
        var i: Int = 0;
        while (i < n)
        {
            print(result(i));
            i += 1;
        }
        print("\n");
    }
    def partition(num: String, result: Array[Char], 
      index: Int, position: Int, k: Int): Unit = {
        if (position == k)
        {
            displayResult(result, index);
            return;
        }
        var count: Int = 0;
        var i: Int = position;
        while (i < k)
        {
            result(index + count) = num.charAt(i);
            if (count == 0 && num.charAt(i) == '0')
            {
                // When if number starts with zero
                // Include space
                result(index + count + 1) = ' ';
                count += 1;
            }
            else if (isEvenDigit(num.charAt(i)))
            {
                // Include space
                result(index + count + 1) = ' ';
                // Find next even number
                partition(num, result, index + count + 2, i + 1, k);
            }
            count += 1;
            i += 1;
        }
    }
    def splitIntoEvenNumber(num: String): Unit = {
        var k: Int = num.length();
        if (k == 0 || !isEvenDigit(num.charAt(k - 1)))
        {
            return;
        }
        // Collect result
        var result: Array[Char] = Array.fill[Char]((k * 2) + 1)(' ');
        println("\n Given number : " + num);
        partition(num, result, 0, 0, k);
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var task: Splitting = new Splitting();
        // Test
        task.splitIntoEvenNumber("12341872017226");
    }
}

Output

 Given number : 12341872017226
12 34 18 72 0 172 2 6
12 34 18 72 0 172 26
12 34 18 72 0 1722 6
12 34 18 72 0 17226
12 34 18 720 172 2 6
12 34 18 720 172 26
12 34 18 720 1722 6
12 34 18 720 17226
12 34 18 720172 2 6
12 34 18 720172 26
12 34 18 7201722 6
12 34 18 72017226
12 34 1872 0 172 2 6
12 34 1872 0 172 26
12 34 1872 0 1722 6
12 34 1872 0 17226
12 34 18720 172 2 6
12 34 18720 172 26
12 34 18720 1722 6
12 34 18720 17226
12 34 18720172 2 6
12 34 18720172 26
12 34 187201722 6
12 34 1872017226
12 3418 72 0 172 2 6
12 3418 72 0 172 26
12 3418 72 0 1722 6
12 3418 72 0 17226
12 3418 720 172 2 6
12 3418 720 172 26
12 3418 720 1722 6
12 3418 720 17226
12 3418 720172 2 6
12 3418 720172 26
12 3418 7201722 6
12 3418 72017226
12 341872 0 172 2 6
12 341872 0 172 26
12 341872 0 1722 6
12 341872 0 17226
12 3418720 172 2 6
12 3418720 172 26
12 3418720 1722 6
12 3418720 17226
12 3418720172 2 6
12 3418720172 26
12 34187201722 6
12 341872017226
1234 18 72 0 172 2 6
1234 18 72 0 172 26
1234 18 72 0 1722 6
1234 18 72 0 17226
1234 18 720 172 2 6
1234 18 720 172 26
1234 18 720 1722 6
1234 18 720 17226
1234 18 720172 2 6
1234 18 720172 26
1234 18 7201722 6
1234 18 72017226
1234 1872 0 172 2 6
1234 1872 0 172 26
1234 1872 0 1722 6
1234 1872 0 17226
1234 18720 172 2 6
1234 18720 172 26
1234 18720 1722 6
1234 18720 17226
1234 18720172 2 6
1234 18720172 26
1234 187201722 6
1234 1872017226
123418 72 0 172 2 6
123418 72 0 172 26
123418 72 0 1722 6
123418 72 0 17226
123418 720 172 2 6
123418 720 172 26
123418 720 1722 6
123418 720 17226
123418 720172 2 6
123418 720172 26
123418 7201722 6
123418 72017226
12341872 0 172 2 6
12341872 0 172 26
12341872 0 1722 6
12341872 0 17226
123418720 172 2 6
123418720 172 26
123418720 1722 6
123418720 17226
123418720172 2 6
123418720172 26
1234187201722 6
12341872017226
import Foundation;
/*
    Swift 4 program
    Split a large Even number into small Even number
*/
class Splitting
{
    // Check whether given character is form of even or not
    func isEvenDigit(_ ch: Character) -> Bool
    {
        if (ch == "0" || ch == "2" || ch == "4" || 
            ch == "6" || ch == "8")
        {
            // Yes
            return true;
        }
        // No
        return false;
    }
    func displayResult(_ result: [Character], _ n: Int)
    {
        var i: Int = 0;
        while (i < n)
        {
            print(result[i], terminator: "");
            i += 1;
        }
        print(terminator: "\n");
    }
    func partition(_ num: [Character], _ result: inout[Character], 
      _ index: Int, _ position: Int, _ k: Int)
    {
        if (position == k)
        {
            self.displayResult(result, index);
            return;
        }
        var count: Int = 0;
        var i: Int = position;
        while (i < k)
        {
            result[index + count] = num[i];
            if (count == 0 && num[i] == "0")
            {
                // When if number starts with zero
                // Include space
                result[index + count + 1] = " ";
                count += 1;
            }
            else if (self.isEvenDigit(num[i]))
            {
                // Include space
                result[index + count + 1] = " ";
                // Find next even number
                self.partition(num, &result, 
                               index + count + 2, i + 1, k);
            }
            count += 1;
            i += 1;
        }
    }
    func splitIntoEvenNumber(_ number: String)
    {   
        let num = Array(number);
        let k: Int = num.count;
        if (k == 0 || !self.isEvenDigit(num[k - 1]))
        {
            return;
        }
        // Collect result
        var result: [Character] = Array(repeating: " ", count: (k * 2) + 1);
        print("\n Given number : ", number);
        self.partition(num, &result, 0, 0, k);
    }
}
func main()
{
    let task: Splitting = Splitting();
    // Test
    task.splitIntoEvenNumber("12341872017226");
}
main();

Output

 Given number :  12341872017226
12 34 18 72 0 172 2 6
12 34 18 72 0 172 26
12 34 18 72 0 1722 6
12 34 18 72 0 17226
12 34 18 720 172 2 6
12 34 18 720 172 26
12 34 18 720 1722 6
12 34 18 720 17226
12 34 18 720172 2 6
12 34 18 720172 26
12 34 18 7201722 6
12 34 18 72017226
12 34 1872 0 172 2 6
12 34 1872 0 172 26
12 34 1872 0 1722 6
12 34 1872 0 17226
12 34 18720 172 2 6
12 34 18720 172 26
12 34 18720 1722 6
12 34 18720 17226
12 34 18720172 2 6
12 34 18720172 26
12 34 187201722 6
12 34 1872017226
12 3418 72 0 172 2 6
12 3418 72 0 172 26
12 3418 72 0 1722 6
12 3418 72 0 17226
12 3418 720 172 2 6
12 3418 720 172 26
12 3418 720 1722 6
12 3418 720 17226
12 3418 720172 2 6
12 3418 720172 26
12 3418 7201722 6
12 3418 72017226
12 341872 0 172 2 6
12 341872 0 172 26
12 341872 0 1722 6
12 341872 0 17226
12 3418720 172 2 6
12 3418720 172 26
12 3418720 1722 6
12 3418720 17226
12 3418720172 2 6
12 3418720172 26
12 34187201722 6
12 341872017226
1234 18 72 0 172 2 6
1234 18 72 0 172 26
1234 18 72 0 1722 6
1234 18 72 0 17226
1234 18 720 172 2 6
1234 18 720 172 26
1234 18 720 1722 6
1234 18 720 17226
1234 18 720172 2 6
1234 18 720172 26
1234 18 7201722 6
1234 18 72017226
1234 1872 0 172 2 6
1234 1872 0 172 26
1234 1872 0 1722 6
1234 1872 0 17226
1234 18720 172 2 6
1234 18720 172 26
1234 18720 1722 6
1234 18720 17226
1234 18720172 2 6
1234 18720172 26
1234 187201722 6
1234 1872017226
123418 72 0 172 2 6
123418 72 0 172 26
123418 72 0 1722 6
123418 72 0 17226
123418 720 172 2 6
123418 720 172 26
123418 720 1722 6
123418 720 17226
123418 720172 2 6
123418 720172 26
123418 7201722 6
123418 72017226
12341872 0 172 2 6
12341872 0 172 26
12341872 0 1722 6
12341872 0 17226
123418720 172 2 6
123418720 172 26
123418720 1722 6
123418720 17226
123418720172 2 6
123418720172 26
1234187201722 6
12341872017226
/*
    Kotlin program
    Split a large Even number into small Even number
*/
class Splitting
{
    // Check whether given character is form of even or not
    fun isEvenDigit(ch: Char): Boolean
    {
        if (ch == '0' || ch == '2' || ch == '4' || 
            ch == '6' || ch == '8')
        {
            // Yes
            return true;
        }
        // No
        return false;
    }
    fun displayResult(result: Array < Char > , n: Int): Unit
    {
        var i: Int = 0;
        while (i < n)
        {
            print(result[i]);
            i += 1;
        }
        print("\n");
    }
    fun partition(num: String, result: Array < Char > , 
                  index: Int, position: Int, k: Int): Unit
    {
        if (position == k)
        {
            this.displayResult(result, index);
            return;
        }
        var count: Int = 0;
        var i: Int = position;
        while (i < k)
        {
            result[index + count] = num.get(i);
            if (count == 0 && num.get(i) == '0')
            {
                // When if number starts with zero
                // Include space
                result[index + count + 1] = ' ';
                count += 1;
            }
            else if (this.isEvenDigit(num.get(i)))
            {
                // Include space
                result[index + count + 1] = ' ';
                // Find next even number
                this.partition(num, result, 
                               index + count + 2, 
                               i + 1, k);
            }
            count += 1;
            i += 1;
        }
    }
    fun splitIntoEvenNumber(num: String): Unit
    {
        val k: Int = num.length;
        if (k == 0 || !this.isEvenDigit(num.get(k - 1)))
        {
            return;
        }
        // Collect result
        val result: Array < Char > = Array((k * 2) + 1)
        {
            ' '
        };
        println("\n Given number : " + num);
        this.partition(num, result, 0, 0, k);
    }
}
fun main(args: Array < String > ): Unit
{
    val task: Splitting = Splitting();
    // Test
    task.splitIntoEvenNumber("12341872017226");
}

Output

 Given number : 12341872017226
12 34 18 72 0 172 2 6
12 34 18 72 0 172 26
12 34 18 72 0 1722 6
12 34 18 72 0 17226
12 34 18 720 172 2 6
12 34 18 720 172 26
12 34 18 720 1722 6
12 34 18 720 17226
12 34 18 720172 2 6
12 34 18 720172 26
12 34 18 7201722 6
12 34 18 72017226
12 34 1872 0 172 2 6
12 34 1872 0 172 26
12 34 1872 0 1722 6
12 34 1872 0 17226
12 34 18720 172 2 6
12 34 18720 172 26
12 34 18720 1722 6
12 34 18720 17226
12 34 18720172 2 6
12 34 18720172 26
12 34 187201722 6
12 34 1872017226
12 3418 72 0 172 2 6
12 3418 72 0 172 26
12 3418 72 0 1722 6
12 3418 72 0 17226
12 3418 720 172 2 6
12 3418 720 172 26
12 3418 720 1722 6
12 3418 720 17226
12 3418 720172 2 6
12 3418 720172 26
12 3418 7201722 6
12 3418 72017226
12 341872 0 172 2 6
12 341872 0 172 26
12 341872 0 1722 6
12 341872 0 17226
12 3418720 172 2 6
12 3418720 172 26
12 3418720 1722 6
12 3418720 17226
12 3418720172 2 6
12 3418720172 26
12 34187201722 6
12 341872017226
1234 18 72 0 172 2 6
1234 18 72 0 172 26
1234 18 72 0 1722 6
1234 18 72 0 17226
1234 18 720 172 2 6
1234 18 720 172 26
1234 18 720 1722 6
1234 18 720 17226
1234 18 720172 2 6
1234 18 720172 26
1234 18 7201722 6
1234 18 72017226
1234 1872 0 172 2 6
1234 1872 0 172 26
1234 1872 0 1722 6
1234 1872 0 17226
1234 18720 172 2 6
1234 18720 172 26
1234 18720 1722 6
1234 18720 17226
1234 18720172 2 6
1234 18720172 26
1234 187201722 6
1234 1872017226
123418 72 0 172 2 6
123418 72 0 172 26
123418 72 0 1722 6
123418 72 0 17226
123418 720 172 2 6
123418 720 172 26
123418 720 1722 6
123418 720 17226
123418 720172 2 6
123418 720172 26
123418 7201722 6
123418 72017226
12341872 0 172 2 6
12341872 0 172 26
12341872 0 1722 6
12341872 0 17226
123418720 172 2 6
123418720 172 26
123418720 1722 6
123418720 17226
123418720172 2 6
123418720172 26
1234187201722 6
12341872017226

Time Complexity

The time complexity of this algorithm depends on the number of valid segmentations that can be formed. In the worst case, the algorithm might explore all possible combinations of digits with spaces, resulting in an exponential time complexity. Specifically, it's O(2^n), where n is the number of digits in the input number. However, the actual number of valid segmentations might be significantly smaller than all possible combinations, so the effective time complexity might be lower.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment