Saturday 16 September 2017

In an array 1-100 numbers are stored, one number is missing how do you find it



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestAlgo
{
   public class ArrayCls
    {
       public int counterTimes=0;
       public int FindMissingNumberInArray(int[] intArr)
       {
           int len;
           
           len = (intArr.Count() % 2 == 0) ? (intArr.Count() / 2) : (intArr.Count() / 2 + 1);
           

           
           int missNo=-1;
           for (int i = 1; i <= intArr.Count()+1; i++)
           {
               int counter = 0;
               int k = intArr.Count() - 1;
               for (int j = 0; j < len; j++)
               {
                   if ((intArr.ElementAt(j) == i) || (intArr.ElementAt(k ) == i))
                   {
                       counter++;
                       break;
                   }
                   k--;

                   counterTimes++;
               }
               if (counter == 0)
               {
                   missNo = i;
                   break;
               }
                   

           }

           return missNo;


       }
    }
}


----------------


class Program
    {
        static void Main(string[] args)
        {
            ArrayCls objArraycls= new ArrayCls();

            int[] intArr = new int[] {6, 2, 1, 5, 3 };
            Console.WriteLine(objArraycls.FindMissingNumberInArray(intArr) );
            Console.WriteLine("No of times = " + objArraycls.counterTimes);
}
}

No comments:

Post a Comment