Need help to solve this using divide and conquer approche please. Add comments o
ID: 3756881 • Letter: N
Question
Need help to solve this using divide and conquer approche please. Add comments on the code please. Also analyze the complexity of it. Thanks
Given an array of size n, find the majority element. The majority element is the element that appears more than You may assume that the array is non-empty and the majority element always exist in the array. Example 1: n/2 J times. Input: [3,2,3] Output: 3 Example 2 Input: [2,2,1,1,1,2,2] output: 2 Seen this question in a real interview before? Yes No 1 int majorityElement(int* nums, int numsSize)Explanation / Answer
# include <stdio.h>
# include <stdbool.h>
bool majority_ele(int arry[], int n, int x)
{
int x;
int lastIndex = n%2? (n/2+1): (n/2);
for (x = 0; x < lastIndex; x++)
{
if (arry[x] == x && arry[x+n/2] == x)
return 1;
}
return 0;
}
int main()
{
int arry[] ={1, 2, 3, 4, 4, 4, 4};
int n = sizeof(arry)/sizeof(arry[0]);
int x = 4;
if (majority_ele(arry, n, x))
printf("%d appears more than %d times in arry[]",
x, n/2);
else
printf("%d does not appear more than %d times in arry[]",
x, n/2);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.