Check if List is Alternating Values

1k views Asked by At

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;
}
3

There are 3 answers

0
Jim Rogers On

This is an interesting problem to translate into Ada:

with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
   type Bit_Array is array (Positive range <>) of Boolean with
      Pack;
   function Is_Alternating (Item : Bit_Array) return Boolean with
      Pre => Item'Length >= 2
   is
      Value : Boolean  := True;
      I     : Positive := Item'First;
   begin
      loop
         Value := Item (I) xor Item (I + 1);
         exit when Value = False or I = Item'Last - 1;
         I := I + 1;
      end loop;
      return I = Item'Last - 1 and Value = True;
   end Is_Alternating;

   A : Bit_Array := (True, False, True, False, True, False);
   B : Bit_Array := (False, True, False, True, False, True, False, True);
   C : Bit_Array := (True, True, False, True, True);
begin
   Put_Line ("A : " & Boolean'Image (Is_Alternating (A)));
   Put_Line ("B : " & Boolean'Image (Is_Alternating (B)));
   Put_Line ("C : " & Boolean'Image (Is_Alternating (C)));
end Main;

The output of this program is:

A : TRUE
B : TRUE
C : FALSE

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.

6
MikeCAT On
  • You cannot use sizeof to determine size of passed array, so you have to pass that. (for more information, see C sizeof a passed array - Stack Overflow)
  • Multiplying -1 will turn 1 into -1 and 0 into 0. It cannot switch between 1 and 0, so the condition is wrong.
  • Your isArrayAlternating has 2 arguments, but you passed only one when you call it.

Code with these points applied:

int isArrayAlternating(int ints[], int N) {
    if (ints == NULL || N % 2 != 0) {
        return false;

    }
    for (int i = 0; i < N - 1; i++) {
        if (ints[i] != 1 - ints[i + 1]) {
            return false;
        }
    }
    return true;
}

int main() {

    int ints[] = {1, 0, 1, 0};
    isArrayAlternating(ints, sizeof(ints) / sizeof(*ints));
}

The next step may be adding some code to print the result.

0
Vlad from Moscow On

For starters the first function parameter should have the qualifier const because the passed array is not changed in the function. Also it is not a good idea to use the identifier ints for the array name because it makes the function less readable. And do not use identifiers for variables that consist from all upper case letters.

The function declaration could look like

int isArrayAlternating( const int a[], size_t n ) {

Secondly you are not using the second parameter within the function body. And you forgot to supply the corresponding argument. Instead of it you are using the expression

sizeof(ints)

that gives the size of a pointer because function parameter having an array type is implicitly converted by the compiler tp pointer to the array element type. That is these two function declarations

int isArrayAlternating( const int a[], size_t n );

and

int isArrayAlternating( const int *a, size_t n );

declare the same one function.

Within the function you should not check whether the passed pointer is equal to NULL or whether the number of elements is even. For example in your question you are using an array of an odd number of elements

1,1,0,1,1

And the condition in the if statement

if (ints[i] != ints[i + 1] * (-1))

does not make a sense.

The function definition can look like

int isArrayAlternating( const int a[], size_t n ) 
{
    size_t i = 0;

    if ( n )
    {
        int value = a[0];

        for ( ; i < n && a[i] == value; i++ )
        {
            value ^= 1;
        }
    }

    return i == n;
}

And the function can be called like

int alternative = isArrayAlternating(ints, N );