we have an array of integer numbers with size of n all eleme

we have an array of integer numbers with size of n. all elements are between 1 and n2.

we call (A) a heavy number if it is repeated in the array n/2 times or more.

I want an efficient algorithm to find this heavy number in the array(if it exist).

Solution

int findHeavyNumber(int arr[])
{

int n=arr.length();
int heavy_num=0;//0 indiactes there is no heavy //number
//for loop over 1 to n power2
for(int j=1;j<=n*n;j++)
{
int count=0;
for(int i=0;i<n;i++)
{
if(arr[i]==j)
count++;
}
if(count>=n/2)
heavy_num=j;
}
return heavy_num;
}

we have an array of integer numbers with size of n. all elements are between 1 and n2. we call (A) a heavy number if it is repeated in the array n/2 times or mo

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site