Thursday, October 30, 2014

Number summation problem

Very recently, I have come across an interesting programming problem which is shown below.

Problem :: We have an array of various numbers. From that array we need to find those numbers which can be written as sum of 2 or more consecutive numbers existing within the same array.

Say for example, in an array of numbers (5,1,3,2,4,6), the first number "5" can be written as "3+2" but not "1+4" as "1" and "4" are not appearing consecutively. Similarly "6" can be written as "5+1" and "2+4" etc. 

So, we need to find out all the numbers which are sum of other consecutive numbers and print them.

1. So, will be requiring a loop which would inspect every number in the array (starting from 5,1 and so on). Let's examine the case of "5".

2. Inside the above loop, we start with adding-numbers (which would be added to produce "5") "1" and start checking if consecutive numbers "3", "2" (i.e 1+3+2+...) and so on can produce the sum "5".
   So, we need a second loop for this. This would find the starting point of adding-numbers. A successful series may be "1+3+..." or "3+2+.." or "2+4+.." etc. So, 1,3,2 etc are starting numbers from which we start to add consecutive numbers.

3. Now, let's consider a case where we have such a starting number say "1". We need a third loop which would run through rest of the array to add consecutive numbers "3", "2" etc  to "1" and produce a sum of "5". This third loop will produce the actual sum.

4. We keep a track whether minimum 2 numbers are added to produce the final result "5" through a counter.

Let's check the code part to understand our approach to solve the problem. The inspection of number "5" have been discussed in comments in the code below.

<?php
// Define the array
$arr = array(5,1,3,2,4,6);

// Here we have the FIRST loop to inspect 
// each number in the array
for($i=0;$i<count($arr);$i++)
{
  // Store the Current number we are inspecting
  // Let's examine the case of 5
  $current_number = $arr[$i];
  
  // Second Loop
  for($j=0;$j<count($arr);$j++)
  {
      // Set $sum variable to hold SUM
      $sum = 0;
      $str = "";
 
      // $count variable makes sure that minimum
      // two numbers are added
      $count = 0;
 
      // We would ignore the Number we are inspecting
      // while Addint. We won't add "5" to $sum 
      // (initially set to 0) to product "5". 
      // Hence we just ignore the situation
      if($j == $i) continue;  
 
      // Otherwise, We move ahead
      if($j != $i) 
      {
  
        // Third Loop, it keeps on Adding to $sum from 
        // the starting point say "1" or "3" or "2" and so on.
        for( $k=$j;$k<count($arr);$k++)
{

          // Avoid the chance of adding the Number
           // which is being inspected (5 in our test case)
          if($k == $i) break;
 
       // Add to SUM ($sum variable)
          $sum += $arr[$k];
  
   // When we add, we increase the counter
   // We need to make sure that minimum 2 numbers
   // are added to produce "5"
   $count++;
  
   // We build a String of numbers we are adding
   $str .= $arr[$k] . ",";
  
    // If summation is already higher than 
           // the Number being inspected, we discard 
           // that combination. This way we can 
           // discard "1+3+2" resulting 6 etc and 
    // save some precious CPU time
    if($sum>$current_number) 
           { 
              break; 
           }
  
          // If minimum 2 digits are added
           // and Sum is "5" let's PRINT the result 
           // and break. This means if we find that 
           // "3+2" is acceptable, then we need to 
           // move to next starting point say "2" and                          // check combinations like "2+4+..." etc
          if($count >= 2 && $sum == $current_number) 
          { 
            // Show Result
            echo "<br> ($i position)" .
                 " $current_number is summation" .
                 " of numbers ". rtrim($str,","); 
            break; 
          } 
      } // Third Loop Ends
    
     }// If ends
 
  } // Second Loop ends

} // First Loop Ends

?>

The above code is explained within the accompanied comments. 

Now, Check the output below :

(0 position) 5 is summation of numbers 3,2
(4 position) 4 is summation of numbers 1,3
(5 position) 6 is summation of numbers 5,1
(5 position) 6 is summation of numbers 1,3,2
(5 position) 6 is summation of numbers 2,4

No comments: