An element is called frequent if it occurs at least 2n3 time
Solution
Solution a)
int main()
{
int my_arr[100], freqency[100];
int n, i, j, counter;
/*
* Read size of array and elements in array
*/
printf(\"Enter size of array: \");
scanf(\"%d\", &n);
printf(\"Enter elements in the array: \");
for(i=0; i<n; i++)
{
scanf(\"%d\", &my_arr[i]);
freqency[i] = -1;
}
/*
* Counts frequency of each element
*/
for(i=0; i<n; i++)
{
counter = 1;
for(j=i+1; j<n; j++)
{
if(my_arr[i]==my_arr[j])
{
counter++;
freqency[j] = 0;
}
}
if(freqency[i]!=0)
{
freqency[i] = counter;
}
}
/*
* check frequency of any element equals to 2n/3
*/
for(i=0; i<n; i++)
{
if(freqency[i]==(2*n)/3)
{
temp=1
}
}
if(temp==1)
return \"frequent element\";
else
return \"No frequent element\";
}
Solution b)
running time of this algorithm is 0(n2)
![An element is called frequent if it occurs at least 2n/3 times in an array of size n. For example, if given input [7, 3, 7, 7, 5, 7] your algorithm should retu An element is called frequent if it occurs at least 2n/3 times in an array of size n. For example, if given input [7, 3, 7, 7, 5, 7] your algorithm should retu](/WebImages/44/an-element-is-called-frequent-if-it-occurs-at-least-2n3-time-1136353-1761608333-0.webp)
![An element is called frequent if it occurs at least 2n/3 times in an array of size n. For example, if given input [7, 3, 7, 7, 5, 7] your algorithm should retu An element is called frequent if it occurs at least 2n/3 times in an array of size n. For example, if given input [7, 3, 7, 7, 5, 7] your algorithm should retu](/WebImages/44/an-element-is-called-frequent-if-it-occurs-at-least-2n3-time-1136353-1761608333-1.webp)