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

Can c++ be faster than c?

0 Answers  


What header file is needed for exit(); a) stdlib.h b) conio.h c) dos.h

0 Answers  


iam a fresher to Qt(GUI a c++ based framework software). i need to develop the basic applications on designer by drag and dropping mechanism...so pls send me the procedure to design applications?

1 Answers  


Why Pointers are not used in C++?

0 Answers   Global Logic,


Which operator can not be overloaded in C++?

1 Answers  






What is the rule of three?

0 Answers  


What are the extraction and insertion operators in c++? Explain with examples.

0 Answers  


Shall we use 'free' to free memory assigned by new, What are the further consequences??

5 Answers   Symphony,


Can we declare a base-class destructor as virtual?

0 Answers  


What is a virtual destructor? Explain the use of it?

0 Answers  


What is the latest version on c++?

0 Answers  


structure that describe a hotel with name, address,rooms and number of rooms

2 Answers  


Categories