-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathisPow.c
More file actions
53 lines (52 loc) · 1.2 KB
/
isPow.c
File metadata and controls
53 lines (52 loc) · 1.2 KB
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
/*
* Complete the 'isPower' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts INTEGER_ARRAY arr as parameter.
*/
/*
* To return the integer array from the function, you should:
* - Store the size of the array to be returned in the result_count variable
* - Allocate the array statically or dynamically
*
* For example,
* int* return_integer_array_using_static_allocation(int* result_count) {
* *result_count = 5;
*
* static int a[5] = {1, 2, 3, 4, 5};
*
* return a;
* }
*
* int* return_integer_array_using_dynamic_allocation(int* result_count) {
* *result_count = 5;
*
* int *a = malloc(5 * sizeof(int));
*
* for (int i = 0; i < 5; i++) {
* *(a + i) = i + 1;
* }
*
* return a;
* }
*
*/
int *isPower(int arr_count, int *arr, int *result_count)
{
int *result = malloc(arr_count * sizeof(int));
*result_count = 0;
for (int i = 0; i < arr_count; i++)
{
int n = arr[i];
if ((n & (n - 1)) == 0)
{
result[*result_count] = 1;
}
else
{
result[*result_count] = 0;
}
(*result_count)++;
}
return result;
}