FInd the element which appears more than n/2 times C++
C++
/* C++ Program for finding out
majority element in an array */
#include <bits/stdc++.h>
using namespace std;
/* Function to find the candidate for Majority */
int findCandidate(int a[], int size)
{
int maj_index = 0, count = 1;
for (int 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];
}
/* Function to check if the candidate
occurs more than n/2 times */
bool isMajority(int a[], int size, int cand)
{
int count = 0;
for (int i = 0; i < size; i++)
if (a[i] == cand)
count++;
if (count > size/2)
return 1;
else
return 0;
}
/* Function to print Majority Element */
void printMajority(int a[], int size)
{
/* Find the candidate for Majority*/
int cand = findCandidate(a, size);
/* Print the candidate if it is Majority*/
if (isMajority(a, size, cand))
cout << " " << cand << " ";
else
cout << "No Majority Element";
}
/* Driver function to test above functions */
int main()
{
int a[] = {1, 3, 3, 1, 2};
int size = (sizeof(a))/sizeof(a[0]);
// Function calling
printMajority(a, size);
return 0;
}
Also in C++:
- Title
- binary tree deletion
- Category
- C++
- Title
- how can make string value in cpp
- Category
- C++
- Title
- string to vector c++
- Category
- C++
- Title
- c++ declare char
- Category
- C++
- Title
- power in c++
- Category
- C++
- Title
- c++ function return pointer to itself
- Category
- C++
- Title
- Html tab
- Category
- C++
- Title
- inserting an element in an set c++
- Category
- C++
- Title
- c++ program to input and print text using Dynamic Memory Allocation.loop
- Category
- C++
- Title
- find in set of pairs using first value cpp
- Category
- C++
- Title
- range based for loop c++
- Category
- C++
- Title
- matrix eigen c++ example
- Category
- C++
- Title
- how to find the mode of a vector c++
- Category
- C++
- Title
- c++ argv
- Category
- C++
- Title
- remove from unordered_set c++
- Category
- C++
- Title
- c++ loop through array
- Category
- C++
- Title
- modular exponentiation c++
- Category
- C++
- Title
- Read multiple files(.txt) c++
- Category
- C++
- Title
- modulo c++
- Category
- C++
- Title
- c++ typeid get type name
- Category
- C++
- Title
- create new file c++
- Category
- C++
- Title
- how to convert string into number
- Category
- C++
- Title
- min coin change problem dp
- Category
- C++
- Title
- make an x using asterisk c++
- Category
- C++
- Title
- rosrun actionlib_msgs genaction.py
- Category
- C++
- Title
- maximum subarray sum equal with K in c++
- Category
- C++
- Title
- how to execute c++ program in cmd
- Category
- C++
- Title
- how to switch to another branch in git
- Category
- C++
- Title
- c++ passing two dimensional array to function
- Category
- C++
- Title
- c++ string to vector int
- Category
- C++
- Title
- string comparison in c++
- Category
- C++
- Title
- euler phi gfg
- Category
- C++
- Title
- statement that causes a function to end in c++
- Category
- C++
- Title
- pionter in c++
- Category
- C++
- Title
- how to check sqrt of number is integer c++
- Category
- C++
- Title
- longest common subsequence
- Category
- C++
- Title
- binary representation differ in bits
- Category
- C++
- Title
- heap in cpp stl
- Category
- C++
- Title
- checking an int in c++
- Category
- C++
- Title
- pass vector by reference c++
- Category
- C++
- Title
- leveling system c++
- Category
- C++
- Title
- c++ initialization list
- Category
- C++
- Title
- zeros of array c++
- Category
- C++
- Title
- insertion c++
- Category
- C++
- Title
- how to sort in descending order c++
- Category
- C++
- Title
- qt make widget ignore mouse events
- Category
- C++
- Title
- c++ vector iterator
- Category
- C++
- Title
- two sum problem in c++
- Category
- C++
- Title
- error: ‘memset’ was not declared in this scope in cpp
- Category
- C++
- Title
- object slicing in c++
- Category
- C++
- Title
- Qt asynchronous HTTP request
- Category
- C++
- Title
- c++ overload operator
- Category
- C++
- Title
- how to swap string characters in c++
- Category
- C++
- Title
- time function c++
- Category
- C++
- Title
- accept the noun and the output of plural c++
- Category
- C++
- Title
- c++ variable arguments
- Category
- C++
- Title
- restting a queue stl
- Category
- C++
- Title
- bfs in C++
- Category
- C++
- Title
- error: redefinition of ‘class Customer’
- Category
- C++
- Title
- inconsequential meaning
- Category
- C++
- Title
- c++ unittest in ros
- Category
- C++
- Title
- how to get last element of set in c++
- Category
- C++
- Title
- lisy stl C++
- Category
- C++
- Title
- iterate through unordered_map c++ in reverse order
- Category
- C++
- Title
- string to number in c++
- Category
- C++
- Title
- c++ reading string
- Category
- C++
- Title
- cin.fail()
- Category
- C++
- Title
- msdn parse command line
- Category
- C++
- Title
- how to make a 2d vector in c++
- Category
- C++
- Title
- Given an undirected graph, count the number of connected components.
- Category
- C++
- Title
- select elements from array C++
- Category
- C++
- Title
- substr in c++
- Category
- C++
- Title
- how to get input from the console in c++
- Category
- C++
- Title
- do while loop c++
- Category
- C++
- Title
- iterate 2d array c++
- Category
- C++
- Title
- error: invalid conversion from 'Node*' to 'int'
- Category
- C++
- Title
- how to convert int to string c++
- Category
- C++
- Title
- pair in c++
- Category
- C++
- Title
- printf c++
- Category
- C++
- Title
- All palindromic substrings
- Category
- C++
- Title
- substr c++
- Category
- C++
- Title
- initialise 2d vector in c++
- Category
- C++
- Title
- peak in c++
- Category
- C++
- Title
- c++ char print align
- Category
- C++
- Title
- append string to another string c++
- Category
- C++
- Title
- check for bst
- Category
- C++
- Title
- appending a double to a string c++
- Category
- C++
- Title
- mingw32/bin/ld.exe: C:\Users\mfrom\AppData\Local\Temp\ccSKcRks.o:PizzaPi.cpp:(.text$_ZN5PizzaC2Ev[__ZN5PizzaC2Ev]+0xa): undefined reference to `vtable for Pizza' collect2.exe: error: ld returned 1 exit status
- Category
- C++
- Title
- c++ sql
- Category
- C++
- Title
- how to make a heap using stl in c++
- Category
- C++
- Title
- cout console
- Category
- C++
- Title
- c++ sort array of ints
- Category
- C++
- Title
- c++ max of array
- Category
- C++
- Title
- accumulate in cpp
- Category
- C++
- Title
- how do you add two random numbers in c++
- Category
- C++
- Title
- how to create a vector in c++
- Category
- C++
- Title
- subtracting two large numbers
- Category
- C++
- Title
- runtime array size c++
- Category
- C++
- Title
- c++ char to string
- Category
- C++
- Title
- sleep system function linux c++
- Category
- C++