aboutsummaryrefslogtreecommitdiff
path: root/counting_bits.c
blob: 359d3a9cd664094517a9c1fbdae723aa67beb403 (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
// 338. Counting Bits
#include <stdio.h>
#include <stdlib.h>

/*
 * given an integer 'n', return an array 'ans' of length 'n + 1' such that for
 * each 'i (0 <= i <= n)', 'ans[i]' is the number of 1's inthe binary
 * representation of 'i'
 */

int *countBits(int n, int *return_size) {
  *return_size = n + 1;
  int *ans = (int *)malloc((n + 1) * sizeof(int));
  int org = 1, dis = 1, ptr = 2, post = 3, idx = 4;
  ans[0] = 0;
  if (!n)
    return ans;
  ans[1] = 1;
  if (n == 1)
    return ans;
  ans[2] = 1;
  if (n == 2)
    return ans;
  ans[3] = 2;
  if (n == 3)
    return ans;
  while (idx <= n) {
    dis = org = org * 2;
    while (ptr != post) {
      for (int i = ptr; i <= post; i++) {
        ans[idx] = ans[i];
        if (idx == n)
          return ans;
        idx++;
      }
      dis /= 2;
      ptr += dis;
    }
    ans[idx] = ans[idx - 1];
    if (idx == n)
      break;
    idx++;
    ans[idx] = ans[idx - 1] + 1;
    if (idx == n)
      break;
    idx++;
    ptr = post + 1;
    post = idx - 1;
  }
  return ans;
}

int main() {
  int rs1[] = {}, rs2[] = {};
  int *cb1 = countBits(2, rs1), *cb2 = countBits(5, rs2);
  for (int i = 0; i < 3; i++)
    printf("%d ", cb1[i]); // expect: 0 1 1
  printf("\n");
  for (int i = 0; i < 6; i++)
    printf("%d ", cb2[i]); // expect: 0 1 1 2 1 2
  printf("\n");
}