If there are 1 to 100 Numbers in array of 101 elements.
Which is the easy way to find repeated number?

Answers were Sorted based on User's Feedback



If there are 1 to 100 Numbers in array of 101 elements. Which is the easy way to find repeated numb..

Answer / vadivelt

1.Get i/p of 101 elements and add all the nos.And say the
result of that Addition is 'sum'.

2.We all know that n(n+1)/2 is formula to calculate the
addition of 'n' numbers.

if n = 100 then n(n+1)/2 give addtion of 1....100.
so say total = n(n+1)/2;

3.Now 'sum' holds the addition of 1....101 nos
And 'total' holds the addition of 1....100 nos

So the repeated no would be.,
Result = sum - total;

Equalent Code is.,

#include<stdio.h>
main()
{
int i, n, sum = 0, a[150], Total;
printf("ENTER SIZE OF ARRAY:\n");
scanf("%d", &n);
printf("\nENTER ELEMENTS OF ARRAY:\n");
for(i = 0; i<n; i++)
{
scanf("%d", &a[i]);
sum = sum + a[i];
}
n = n-1;
Total = (n *(n+1) /2);
printf("\nREPEATED NO: %d",sum - Total);
getch();
}

Is This Answer Correct ?    38 Yes 6 No

If there are 1 to 100 Numbers in array of 101 elements. Which is the easy way to find repeated numb..

Answer / rahul

Binary Tree. But their exists better solution

Is This Answer Correct ?    2 Yes 2 No

Post New Answer

More C++ General Interview Questions

Define a constructor - what it is and how it might be called (2 methods)?

0 Answers  


What are the conditions that have to be met for a condition to be an invariant of the class?

1 Answers  


Difference between pointer to constant and constant pointer to a constant. Give example.

0 Answers   HAL,


CAN U SAY WHICH PROGRAMING LANGUAGE IS USED BY DOCTORS....?

1 Answers  


Do inline functions improve performance?

0 Answers  






Will rust take over c++?

0 Answers  


What is the difference between structures and unions?

0 Answers  


Write a single instruction that will store an EVEN random integer between 54 and 212 inclusive in the variable myran. (NOTE only generate EVEN random numbers)

0 Answers  


What is the difference between a definition and a declaration?

0 Answers  


What does floor mean in c++?

0 Answers  


What is size of string in c++?

0 Answers  


Is c++ double?

0 Answers  


Categories