w3resource

C Exercises: Check whether the sequence of numbers 1, 2, 3 appears in a given array of integers somewhere

C-programming basic algorithm: Exercise-14 with Solution

Write a C program to check whether the sequence of numbers 1, 2, 3 appears in a given array of integers somewhere.

C Code:

#include <stdio.h> // Include standard input/output library
#include <stdlib.h> // Include standard library for additional functions

// Function declaration for 'test' with an array of integers and its size as parameters
int test(int nums[], int arr_size);

int main(void){
    int arr_size; // Declare an integer variable to store the size of the array

    // Initialize an integer array 'array1' with given values
    int array1[] = {1,1,2,3,1};
    arr_size = sizeof(array1)/sizeof(array1[0]); // Calculate the size of 'array1'
    printf("%d",test(array1, arr_size)); // Call the 'test' function with 'array1' and its size, and print the result

    // Initialize another integer array 'array2' with given values
    int array2[] = {1,1,2,4,1};
    arr_size = sizeof(array2)/sizeof(array2[0]); // Calculate the size of 'array2'
    printf("\n%d",test(array2, arr_size)); // Call the 'test' function with 'array2' and its size, and print the result

    // Initialize yet another integer array 'array3' with given values
    int array3[] = {1,2,2,1,2,3};
    arr_size = sizeof(array3)/sizeof(array3[0]); // Calculate the size of 'array3'
    printf("\n%d",test(array3, arr_size)); // Call the 'test' function with 'array3' and its size, and print the result
}

// Function definition for 'test'
int test(int nums[], int arr_size)
{
    // Iterate through the elements of the array 'nums'
    for (int i = 0; i < arr_size-1; i++)
    {
        // Check if the current element, the next element, and the element after that form the sequence 1, 2, 3
        if (nums[i] == 1 && nums[i + 1] == 2 && nums[i + 2] == 3)
            return 1; // If the sequence is found, return 1
    }
    return 0; // If the sequence is not found, return 0
}

Sample Output:

1
0
1

Explanation:

int test(int nums[], int arr_size) {
  for (int i = 0; i < arr_size - 1; i++) {
    if (nums[i] == 1 && nums[i + 1] == 2 && nums[i + 2] == 3)
      return 1;
  }
  return 0;
}

The above function takes two integers x and y as input and returns 1 if their last digits are equal, otherwise 0. The function first computes the remainder of x and y when divided by 10 using the modulus operator %. Then, it takes the absolute value of the remainders using the abs() function to make sure that the result is positive. Finally, it compares the remainders and returns the result of the comparison.

Time complexity and space complexity:

Time complexity of the function is O(1), as it performs a fixed number of operations regardless of the input size.

The Space complexity of the function is O(1) as the function uses a constant amount of space.

Pictorial Presentation:

C Programming Algorithm: Check whether the sequence of numbers 1, 2, 3 appears in a given array of integers somewhere

Flowchart:

C Programming Algorithm Flowchart: Check whether the sequence of numbers 1, 2, 3 appears in a given array of integers somewhere

C Programming Code Editor:

Previous: Write a C program to check if two given non-negative integers have the same last digit.
Next: Write a C program to count the number of two 5's are next to each other in an array of integers. Also count the situation where the second 5 is actually a 6.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.