ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

As a programmer : In an array 1-100 numbers are stored, one number is missing how do you find it?

user-image
تم إضافة السؤال من قبل Afaf Abdullah , خريجة , جامعة صنعاء
تاريخ النشر: 2014/11/29
vihar jadhav
من قبل vihar jadhav , Developer , Eis Solutions pvt.ltd

    static void Main(string[] args)

        {

 

            int[] arr = {1,2,3,4,5,6,7,8,9,10 };

 

            int sum = 0,i;

 

            for (i = 0; i <10; i++)

 

            { 

                sum=sum+1;

 

 

                if (arr[i] == sum)

                {

 

                    continue;

                   

                }

             else 

 

                {

                   Console.WriteLine("missing no {0}", sum);

                   break;

                

                }

 

                

            }

            Console.WriteLine("nothing is missing");   

            Console.ReadLine();

 

 

}

Gayasuddin Mohammed
من قبل Gayasuddin Mohammed , Advocate , Practicing Law before High Court at Hyderabad

below c program will find the missing number in the array.

the same logic can be implemented in the language you are interested.

another option is sort the array in ascending or descending order, keep increasing the array index and check for index number and value at that index are equal ( in c language index number == index number +1, as the index starts in C language from0), in case of not equal, you can print your missing number, in case c language, your index+1 is the missing number.

 

void main()

{

int array1[100], array2[100], flag;

// i assume array1 has got your data which need to be check for the missing number

for (int i=1; i<=100; i=i++)

     flag=0;

     

      for (int j=0; j<100; j++)

       {

           if ( array[j] ==i)

               flag=1;

        }

        if (flag !=1)

       {

          printf("missing number is %d", i); 

          break;

        }

}

pardeep kumar
من قبل pardeep kumar , Senior R & D Engineer , Mavenir Systems

This one is simple. 

1. You can calculate the sum of array using loop say sumOfArray

2. Calculate the sum of number from1 to100 using formula n*(n+1)/2 call it sum

3. missing number  = sum - sumOfArray

 

 

Aun Al Ali
من قبل Aun Al Ali , engineering , Yokogawa

array int a; //contain the100 sort numbers

for(int i =1;i<100; i++){

   if(i != a[i-1]){

        System.out.println(i + " is the miss number");

        i =100;   

   }

}

Abdulelah Fallatah
من قبل Abdulelah Fallatah , Computer Operator , شركة الحاسب الآلي العربي

 

Create a for loop that traverses all of the elements of the array. If one of them is 'null', then it is missing a value ;).

 

R E G A R D S

 

aishwarya kabde
من قبل aishwarya kabde

Here we go with the answer as furnished below:

Ans:

public class OneMissingNumber { /** * @param args */ public static void main(String[] args) { int[] arr = new int[99]; for (int x =0; x < arr.length; x++) if (x !=45){ //skip1 element6894+45 =6939 is skipped arr[x] = x +6894; } getMissing(arr); } static void getMissing(int a[]) { int arraySum =0; for (int x =0; x <99; x++) { arraySum += a[x]; } int realSum = (a[a.length -1] * (a[a.length -1] +1) /2); realSum -= (a[0] * (a[0] -1) /2); System.out.println("missing number is = "+(realSum - arraySum)); } }

المزيد من الأسئلة المماثلة

هل تحتاج لمساعدة في كتابة سيرة ذاتية تحتوي على الكلمات الدلالية التي يبحث عنها أصحاب العمل؟