Everyone Is An Achiever

Sunday, January 4, 2015

Finding the missing numbers in a series

9:34 PM Posted by Unknown 2 comments


Hello everyone, Today's tutorial is fun. We'll be given a series of numbers that has the EXACT difference, and we will find the missing numbers between them.
Lets see an example. If we entered a series of initial 10 numbers as:
10 20 60 70 130 140 150 180 200 210
Since the difference between every two numbers is the same throughout the series, in this example we're missing 11 numbers:
10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 210

Before going to the program part lets talk little bit in theory.
1) The difference between every two numbers is exactly equal thru the series. This is an important note since it will help us finding all the missing numbers

2) The number of missing numbers differs, which mean we need to create a mechanism to figure out how many missing numbers are there? What are they?. This is the core of our program

3) The first two numbers in the series must be present correctly. From them well figure out the missing number and achieve all the requirements

So, lets do the program. This is the full CSharp code, beneath it is the line-by-line explanation:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace TestConsole
{

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the series size:");
            int n = int.Parse(Console.ReadLine());
            int[] numbers = new int[n];

            
            Console.WriteLine("Note:\n1- The series has equal difference between any two numbers, therefore,\n2-enter the first 2 elements correctly since they will be the key to find all missing elements");

            Console.WriteLine("Enter {0} elements:", n);
            for (int i = 0; i < numbers.Length; i++)
                numbers[i] = int.Parse(Console.ReadLine());

            Console.WriteLine("Your series:");
            for (int i = 0; i < numbers.Length; i++)
                Console.Write(numbers[i] + " ");

            Console.WriteLine();

            int differenece = 0;
            int numberOFmissingEelements = 0;
            int x = 0;
          
            differenece = numbers[1] - numbers[0];
            for (int i = 1; i < numbers.Length; i++)
            {
                if (i == numbers.Length - 1)
                    break;

                if (numbers[i + 1] - numbers[i] != differenece)
                {
                    numberOFmissingEelements = (numbers[i + 1] - numbers[i] - differenece) / differenece;
                    x = numbers[i] + differenece; // take the current value in the series and add the difference to it which will be the first missing elements
                    for (int j = 0; j < numberOFmissingEelements; j++)
                    {
                        Console.WriteLine("Number {0} is missing", x);
                        x = x + differenece; // each time add the differene to the value of x

                    }
                }
            }
            Console.ReadLine();
        }
    }
}


  • We start by asking the user how much is the initial length of the series and we prompt a message to the user about the program. Then we ask the user to enter the numbers and we display them. Nothing serious here:

          
Console.WriteLine("Enter the series size:");
            int n = int.Parse(Console.ReadLine());
            int[] numbers = new int[n];

            
   Console.WriteLine("Note:\n1- The series has equal difference between any two numbers, therefore,\n2-enter the first 2 elements correctly since they will be the key to find all missing elements");

            Console.WriteLine("Enter {0} elements:", n);
            for (int i = 0; i < numbers.Length; i++)
                numbers[i] = int.Parse(Console.ReadLine());

            Console.WriteLine("Your series:");
            for (int i = 0; i < numbers.Length; i++)
                Console.Write(numbers[i] + " ");

            Console.WriteLine();


  • Now, lets convert the theory part into practice. We declared 3 integers, the first to hold the difference, the second to hold the number of missing numbers and third one will hold a missing number.

int differenece = 0;
            int numberOFmissingEelements = 0;
            int x = 0;


  • We first figure out the difference and this is pretty simple since we only subtract the second number from the first number.

differenece = numbers[1] - numbers[0];


  • Now we iterate thru a loop, in each iteration we check if the number[i+1] – number[i] is equal to the difference? If yes then there are NO missing numbers if it's NOT equal then there are a missing number or numbers. 

e.g.,
since the first two numbers are presented correctly, in the first iteration we check the difference between the 2nd and the 3rd numbers and if its equal to the initial difference calculated or not:
20 60
if (numbers[i + 1] - numbers[i] != differenece)

And since the difference is not equal to the initial difference which is 10, then there are missing numbers.
We then calculate How many missing numbers are there?
Consider our example, between 20 and 60 there are 3 missing numbers. To figure how many of them we will apply the following equation:
numberOFmissingEelements = (numbers[i + 1] - numbers[i] - differenece) / differenece;

applying it to our example:
numberOFmissingEelements = (60 – 20 – 10) / 10 = (30) / 10 = 3

 another example: 70 130
numberOFmissingEelements = (130 – 70 – 10) / 10 = (50) / 10 =

and so forth.


  • The last thing is to print those number. The first missing number will be the current number + the difference, then we go through a loop to print them all.

                    x = numbers[i] + differenece;
                    for (int j = 0; j < numberOFmissingEelements; j++)
                    {
                        Console.WriteLine("Number {0} is missing", x);
                        x = x + differenece; // each time add the differene to the value of x

                    }

output:



That's it. In the improved version of this program, we will:
• Figure out all the missing numbers and store them
• Pick the wrong elements in the series. For example: 1 3 4 9 10 15 -> the program must pick the wrong entered numbers

 See you soon :)

2 comments:

  1. Seems extremely complicated, why not just use a remainder and a list of missing numbers.

    var numbers = new List { 10, 20, 60, 70, 130, 140, 150, 180, 200, 210 };

    var missingNumbers = Enumerable.Range(numbers.Min(), numbers.Max()).Except(numbers);

    var numbersWithInterval = missingNumbers.Where(x => Math.IEEERemainder(x, 10) == 0);

    Voilá :)

    ReplyDelete
  2. I see what you did is brilliant. but this will only work for this series:
    { 10, 20, 60, 70, 130, 140, 150, 180, 200, 210 }

    if you changed the difference in the series in won't work.

    e.g.,
    the difference is 2 as:
    1 3 5 9 11 15 => missing numbers: 7 13 (won't work)

    or the difference is 4 as:
    1 5 9 13 25 29 => missing numbers: 17 21 (won't work)

    (and I just discoverd a small bug in my program which is the program will work for the series that has and EVEN number as a difference)

    ReplyDelete