chore(java): add majority element (#354)

pull/356/head
Aayush 2021-06-13 14:36:53 +05:30 committed by GitHub
parent 550b3fb9b6
commit c3b77367a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 0 deletions

View File

@ -6,6 +6,7 @@
2. [Kadanes Algorithm](arrays/kadanes-algorithm.java) 2. [Kadanes Algorithm](arrays/kadanes-algorithm.java)
3. [Left Rotation](arrays/left-rotation.java) 3. [Left Rotation](arrays/left-rotation.java)
4. [Unique Digits of Large Number](arrays/unique-digits-of-large-number.java) 4. [Unique Digits of Large Number](arrays/unique-digits-of-large-number.java)
5. [Majority Element](arrays/majority-element.java)
## Graphs ## Graphs

View File

@ -0,0 +1,70 @@
//Given an array A of N elements. Find the majority element in the array.
//A majority element in an array A of size N is an element that appears more than N/2 times in the array
// Moores Voting Algorithm => Time Complexity: O(n), Space Complexity: O(1)
import java.util.*;
import java.io.*;
import java.lang.*;
class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0) {
int n =sc.nextInt();
int arr[] = new int[n];
for(int i = 0; i < n; i++)
arr[i] = sc.nextInt();
System.out.println(new Solution().majorityElement(arr, n));
}
}
}
class Solution
{
static int majorityElement(int a[], int size){
int cand = findCand(a, size);
if(isMajority(a, size, cand))
return cand;
return -1;
}
static int findCand(int arr[], int n){
int max_index=0; int count=1;
for(int i=1; i<n; i++){
if(arr[max_index]==arr[i])
count++;
else count--;
if(count==0){
max_index=i;
count=1;
}
}
return arr[max_index];
}
static boolean isMajority(int arr[], int n, int cand){
int count=0;
for(int i=0; i<n; i++){
if(cand==arr[i])
count++;
}
if(count > n/2) return true;
return false;
}
}
/* Test Case:
Input:
N = 5 A[] = {3,1,3,3,2}
Output: 3
Explanation:
Since, 3 is present more
than N/2 times, so it is
the majority element. */