forked from WaderManasi/Knowing-DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45676d4
commit 40c8b3c
Showing
1 changed file
with
14 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,33 @@ | ||
//Given an integer array and another integer element. | ||
//The task is to find if the given element is present in array or not. | ||
//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. | ||
//solution: | ||
|
||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
int search(int arr[], int N, int X) | ||
int majorityElement(int a[], int size) | ||
{ | ||
for(int i=0;i<N;i++) | ||
int n=size/2; | ||
int v[100]={0}; | ||
int i=0; | ||
for(i=0;i<size;i++) | ||
{ | ||
if(arr[i]==X) | ||
return i; | ||
v[a[i]]++; | ||
} | ||
for(i=0;i<size;i++) | ||
{ | ||
if(v[a[i]]>n) | ||
return a[i]; | ||
} | ||
return -1; | ||
} | ||
int main() | ||
{ | ||
int n,key; | ||
cin>>n; | ||
cin>>key; | ||
|
||
int arr[n]; | ||
for(int i=0;i<n;i++) | ||
cin>>arr[i]; | ||
cout<<search(arr,n,key)<<endl; | ||
cout<<majorityElement(arr,n)<<endl; | ||
return 0; | ||
} |