1. 2015
    Feb
    5

    Problem Solving: Join 2 numbers in sequence

    Posted By



    Write a solution that takes two separate numbers and combine them together to make a new number. The number should be joined together in such a way that if the first number is “1234” and the second number is “67”. the first number in the first number becomes the first number of the new number; the first number in the second number becomes the second number of the new number; the second number in the first number becomes the third number of the new number; and so on and so forth. So the new number will look like “162734”.

    Note: The number cannot exceed 10000000, if it does return -1

    
    
    	function solution($A, $B) {
                    if(empty($A) || empty($B)) {
                        return -1;
                    } 
    	    	$A_array = str_split($A);
      	    	$B_array = str_split($B);
    		$B_count = count($B_array);
    	    	$A_count = count($A_array);
    		$max = $A_count + $B_count;
    		$max_allowed = 10000000;
    		$counter = $A_counter = $B_counter = 0;
    		$result = "";
    		if($max > $max_allowed) {
    			return -1;
    		}
    		while($counter < $max) {
    			if($A_counter < $A_count) {
    				$result .= $A_array[$A_counter];
    				$A_counter++;
    				$counter++;
    			}
    			if($B_counter < $B_count) {
    				$result .= $B_array[$B_counter];
    				$B_counter++;
    			    	$counter++;
    			}	
    		}
    		return $result;
    	}
    
    

  2. About Emeka Echeruo

    Emeka Echeruo

    I love sports, football which I refuse to call soccer, and the outdoor especially walks in park. Software development is my passion, there is a beauty in creating something out of nothing but algebra that ends up becomes a part of a persons daily life. I love kids, dogs, nightlife and art because it finds you and moves you emotionally!

  3. Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.