So I have an array of N size. I want to first check if the array is of alternating sequence (We are assuming that the numbers are positive and the values are either 1 or 0)
101010 would return true
110101 would return false
After I check this I want to know how many of these numbers I need to change for it to be an alternating sequence for example:
1,1,0,1,1
would return 2
because you can reverse the first and fifth index to achieve 0,1,0,1,0
Currently I am checking for the alternating sequence like so:
#include <stdio.h> // For I/O operations
#include <stdbool.h> // for 'true' and 'false'
int isArrayAlternating(int ints[], size_t N) {
if (ints == NULL || sizeof(ints) % 2 != 0)
return false;
for (int i = 0; i < sizeof(ints) - 1; i++)
if (ints[i] != ints[i + 1] * (-1))
return false;
return true;
}
int main(void) {
int ints[] = {1, 0, 1, 0};
size_t N = sizeof(ints) / sizeof(ints[0]);
isArrayAlternating(ints);
return 0;
}
This is an interesting problem to translate into Ada:
The output of this program is:
This solution creates a packed array of boolean values. A packed array of boolean results in an array of bits, each bit representing a single boolean value. The array type is unconstrained, allowing each instance of Bit_Array to have a different length. Thus, the instances labled A, B, and C have different lengths.