Everyone Is An Achiever

Tuesday, January 13, 2015

[improved] Finding the missing numbers in a series

4:56 AM Posted by Unknown 4 comments


Hello everyone, today will improve the finding missing numbers program. In this improvements we will:

  •  find the missing elements and store them 
  •  capture the wrong entered elements 


So, I've been thinking about the ways I can handle this. Storing all numbers is not a big deal since you can use a list of integers and it will shrink or stretch automatically, the problem was in figuring out the wrong numbers.
I tried to develop some algorithms to handle it but I came across many possibilities and for each one I had to modify the code and add extra lines over and over again. So I thought: There might be a better way? Then I found a much more simpler way. What I did is that I tried to simulate what the user might want to do.
The user for example wants to enter a series that has 8 as a difference, lets say this series:
1 9 10 11 12 17 18 19 20 21 26 27 28 29 30 35 42

At first glance on the series you'll notice how mixed up it is. Since the first 2 numbers are always supposed to be correct, I assumed that the user is trying to get a series that has 8 difference between each two numbers and the maximum number cannot be more than 42, therefore, the series we're looking for is:
1 9 17 25 33 41

So, what I did is that I generated a new list apart from the original one containing the correct elements! that's it. You may say: What? but as I told you if you tried by yourself to design an algorithm to find each wrong number it will get really complicated and there will be tens of possibilities that you need to take into consideration, So, keep it simple :)

The drawback in this way is that it'll take extra space since you will use a new list. You can  optimize it by generating the numbers and over write the previous data on the same list.

In my program I maintained the original list and actually switched the original list to be a list of all wrong numbers and stored the correct series in a new list. here is the full CSharp code:

using System;

using System.Collections;

using System.Collections.Generic;

using System.Linq;

using System.Text;



namespace FindMissingNumbers

{

    class Program

    {

        static void Main(string[] args)

        {

            List originalSeries = new List();

            List newSeries = new List();



            Console.WriteLine("Enter the series initial size:");

            int n = int.Parse(Console.ReadLine());



            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 < n; i++)

                originalSeries.Add(int.Parse(Console.ReadLine()));



            Console.WriteLine("Your current series:");

            for (int i = 0; i < originalSeries.Count; i++)

                Console.Write(originalSeries[i] + " ");



          

            int difference = originalSeries[1] - originalSeries[0];

            int x = originalSeries[1] + difference;

          

            //setting the first two locations in the new series with the first two numbers from the original series since they're always correct

            newSeries.Add(originalSeries[0]);

            newSeries.Add(originalSeries[1]);



            // generating a correct list based on the difference and the maximum number entered in the series, mostly the last elements

            // we keep on generating untill the value of x become as the value (or greater) of the last number in the list (supposing it is the greatest number)

            for (int i = 0; x <= originalSeries[originalSeries.Count -1] ; i++)

            {

                newSeries.Add(x);

                x += difference;

            }



            //removing the correct numbers from the original series so that the original series will has all wrong numbers

            for (int i = 0; i < newSeries.Count; i++)

            {

               if(originalSeries.Contains(newSeries[i]))

                   originalSeries.Remove(newSeries[i]);

            }



            Console.WriteLine("\nCorrect series:");

            for (int i = 0; i < newseries.count; i++)                Console.Write(newSeries[i] + " ");



             Console.WriteLine("\nWrong elements:");

             for (int i = 0; i < originalSeries.Count; i++)

                 Console.Write(originalSeries[i] + " ");



            Console.ReadLine();

        }

      

    }

}


4 comments:

  1. Way too bloated code. Should rather use Linq.

    List input = new List { 2, 9, 10, 11, 12, 17, 18, 19, 20, 21, 26, 27, 28, 29, 30, 35, 42 };

    int start = input[0];
    int diff = input[1] - input[0];

    var correctNumbers = Enumerable.Repeat(start, input.Max() / diff).Select((val, i) => val + i * diff);
    var wrongNumbers = input.Except(correctNumbers);

    Console.Out.WriteLine("Correct numbers: " + string.Join(",", correctNumbers));
    Console.Out.WriteLine("Wrong numbers: " + string.Join(",", wrongNumbers));
    Console.In.ReadLine();

    ReplyDelete
    Replies
    1. I'm still new to the concept of LINQ that's why I went with the regular way

      Delete
  2. Good fuctionnal skills :)

    ReplyDelete