-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab 3_Prog 2.c
71 lines (69 loc) · 1.24 KB
/
Lab 3_Prog 2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//Q2 TO CHECK WHETHER ANY ITEM OCCURS N/2 TIMES
#include<stdio.h>
#define bool int
void naive(int *a,int n)
{
int i;
for(i=0;i<n;i++)
{
int count = 0;
for (int j=i+1; j<n;j++)
{
if(a[j]==a[i])
count++;
}
if(count>n/2)
{
printf("\n%d is repeating more than %d times.",a[i],n/2);
return;
}
}
if(i==n)
printf("\n No Result");
}
int findCandidate(int a[], int size)
{
int maj_index = 0, count = 1;
int i;
for (i = 1; i < size; i++) {
if (a[maj_index] == a[i])
count++;
else
count--;
if (count == 0) {
maj_index = i;
count = 1;
}
}
return a[maj_index];
}
bool isMajority(int a[], int size, int cand)
{
int i, count = 0;
for (i = 0; i < size; i++){
if (a[i] == cand)
count++;}
if (count > size / 2)
return 1;
else
return 0;
}
void efficient(int a[], int size)
{
int cand = findCandidate(a, size);
if (isMajority(a, size, cand))
printf(" %d ", cand);
else
printf("No Majority Element");
}
int main()
{
int n;
printf("Enter the number of values:- ");
scanf("%d",&n);
int a[n];
printf("Enter the values:- ");
for(int i=0; i<n;i++)
scanf("%d",&a[i]);
efficient(a,n);
}