Check if an Array is made up of Subarrays of continuous repetitions of every distinct element | C ++ Program to implement the above problem ; Function to check if the array is made up of subarrays of repetitions ; Base Case ; Stores the size of current subarray ; If a different element is encountered ; If the previous subarray was a single element ; Reset to new subarray ; Increase size of subarray ; If last element differed from the second last element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool ContinuousElements ( int a [ ] , int n ) { if ( n == 1 ) return false ; int curr = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] != a [ i - 1 ] ) { if ( curr == 1 ) return false ; else curr = 0 ; } curr ++ ; } if ( curr == 1 ) return false ; return true ; } int main ( ) { int a [ ] = { 1 , 1 , 2 , 2 , 1 , 3 , 3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; if ( ContinuousElements ( a , n ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }