aboutsummaryrefslogtreecommitdiff
path: root/majority_element2.c
blob: 57a1e824c9de970463cef054ce893dad419d051e (plain)
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
// 229. Majority Element II
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

/*
 * given an integer array of size 'n', find all elements that appear more than n
 * / 3 times (floored)
 */

int *majorityElement(int *nums, int nums_size, int *return_size) {
  int one = INT_MIN, two = INT_MIN, cnt1 = 0, cnt2 = 0, n = nums_size;
  for (int i = 0; i < n; i++) {
    if (nums[i] == one)
      cnt1++;
    else if (nums[i] == two)
      cnt2++;
    else {
      if (!cnt1) {
        one = nums[i];
        cnt1 = 1;
      } else if (!cnt2) {
        two = nums[i];
        cnt2 = 1;
      } else {
        cnt1--;
        cnt2--;
      }
    }
  }
  cnt1 = 0;
  cnt2 = 0;
  for (int i = 0; i < n; i++) {
    if (nums[i] == one)
      cnt1++;
    else if (nums[i] == two)
      cnt2++;
  }
  int *ans;
  if (cnt1 > n / 3 && cnt2 > n / 3) {
    ans = malloc(2 * sizeof(int));
    ans[0] = one;
    ans[1] = two;
    *return_size = 2;
    return ans;
  } else if (cnt1 > n / 3) {
    ans = malloc(sizeof(int));
    ans[0] = one;
    *return_size = 2;
    return ans;
  } else if (cnt1 > n / 3) {
    ans = malloc(sizeof(int));
    ans[0] = two;
    *return_size = 1;
    return ans;
  } else {
    *return_size = 0;
    return ans;
  }
}

int main() {
  int n1[] = {3, 2, 3}, n2[] = {1}, n3[] = {1, 2};
  int *me1 = majorityElement(n1, 3, NULL);
  int *me2 = majorityElement(n2, 1, NULL);
  int *me3 = majorityElement(n3, 2, NULL);
  printf("%d\n", me1[0]); // expect: 3
  printf("%d\n", me2[0]); // expect: 1
  printf("%d", me3[0]);   // expect: 1
  printf("%d\n", me3[1]); // expect: 2
}