//How to find majority element in array using backtracking method.
#include<stdio.h>
main()
{
int *p,n,i,j,c=0,f,fl=0;
printf("Enter the number of elments you want in your array list: ");
scanf("%d",&n);
p=(int *)malloc(n*sizeof(int));
printf("\n Enter the value of array elements \n");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
for(i=0;i<=n/2;i++)
{
f=0;
c=0;
for(j=0;j<i;j++)
{
if(p[i]==p[j])
{
f=1;
break;
}
}
if(f==1)
continue;
c=1;
for(j=i+1;j<n;j++)
{
if(p[i]==p[j])
{
c=c+1;
}
}
if(c>(n/2))
{
fl=1;
printf("%d is majority element",p[i]);
break;
}
}
if(fl==0)
printf("no majority element is found");
}
#include<stdio.h>
main()
{
int *p,n,i,j,c=0,f,fl=0;
printf("Enter the number of elments you want in your array list: ");
scanf("%d",&n);
p=(int *)malloc(n*sizeof(int));
printf("\n Enter the value of array elements \n");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
for(i=0;i<=n/2;i++)
{
f=0;
c=0;
for(j=0;j<i;j++)
{
if(p[i]==p[j])
{
f=1;
break;
}
}
if(f==1)
continue;
c=1;
for(j=i+1;j<n;j++)
{
if(p[i]==p[j])
{
c=c+1;
}
}
if(c>(n/2))
{
fl=1;
printf("%d is majority element",p[i]);
break;
}
}
if(fl==0)
printf("no majority element is found");
}