w3resource

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


Check Sequence 1, 2, 3 in Array

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

Sample Solution:

C++ Code :

#include <iostream>
using namespace std;

// Function to check if an array contains a sequence of 1, 2, 3
bool test(int nums[], int arr_length)
{
    // Loop through the array elements (except the last two elements) to find the sequence 1, 2, 3
    for (int i = 0; i < arr_length - 2; i++)
    {
        // Check if the current, next, and next-next elements form the sequence 1, 2, 3
        if (nums[i] == 1 && nums[i + 1] == 2 && nums[i + 2] == 3)
            return true; // Return true if the sequence is found
    }
    return false; // Return false if the sequence is not found
}

int main() 
{
    int arr_length; // Variable to store the length of the array

    // Test cases with different arrays
    int nums1[] = {1, 1, 2, 3, 1};    // Array 1
    arr_length = sizeof(nums1) / sizeof(nums1[0]); // Calculate the length of array 1
    cout << test(nums1, arr_length) << endl; // Check if array 1 contains the sequence 1, 2, 3

    int nums2[] = {1, 1, 2, 4, 1};    // Array 2
    arr_length = sizeof(nums2) / sizeof(nums2[0]); // Calculate the length of array 2
    cout << test(nums2, arr_length) << endl; // Check if array 2 contains the sequence 1, 2, 3

    int nums3[] = {1, 1, 2, 1, 2, 3}; // Array 3
    arr_length = sizeof(nums3) / sizeof(nums3[0]); // Calculate the length of array 3
    cout << test(nums3, arr_length) << endl; // Check if array 3 contains the sequence 1, 2, 3

    return 0; // Return 0 to indicate successful completion
}

Sample Output:

1
0
1

Visual Presentation:

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

Flowchart:

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

For more Practice: Solve these Related Problems:

  • Write a C++ program to scan an array of integers and check if the consecutive sequence 1, 2, 3 appears anywhere.
  • Write a C++ program that iterates through an array to determine if it contains the subsequence 1, 2, 3 in order.
  • Write a C++ program to search a list of integers for the specific ordered pattern 1, 2, 3 and return a boolean result.
  • Write a C++ program that examines an array and prints true if the sequence 1, 2, 3 is found consecutively within it.

Go to:


PREV : Count Substrings of Length 2.
NEXT : Compare Strings for Same Length 2 Substrings.

C++ Code Editor:



Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.