-
Notifications
You must be signed in to change notification settings - Fork 1
/
061MajEleArr.cpp
75 lines (63 loc) · 1.58 KB
/
061MajEleArr.cpp
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
72
73
74
75
// c++ program to find the majority element in an array(appearing more than N/2 times)
#include <iostream>
using namespace std;
int main()
{
int size;
cout << "Enter the size of the array: ";
cin >> size;
if (size <= 0)
{
cout << "Invalid array size. Please enter a positive integer." << endl;
return 1; // Exit with an error code
}
int arr[size];
// Input elements into the array
cout << "Enter elements of the array:" << endl;
for (int i = 0; i < size; i++)
{
cin >> arr[i];
if (arr[i]==0)
{
cout << "Invalid element. Please enter a positive integer." << endl;
return 1; //Exit with an error code
}
}
// Display the elements of the array
cout << "Elements in the array: |";
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << "|";
cout << endl;
int majorityElement = -1;
int count = 0;
// Find the majority element (if it exists)
for (int i = 0; i < size; i++)
{
int currentElement = arr[i];
count = 0;
for (int j = 0; j < size; j++)
{
if (arr[j] == currentElement)
{
count++;
}
}
if (count > size / 2)
{
majorityElement = currentElement;
break;
}
}
if (majorityElement != -1)
{
cout << "The majority element is: " << majorityElement << endl;
}
else
{
cout << "No majority element found in the array." << endl;
}
return 0;
}