
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ code to check given flag is stripped or not
Suppose we have a matrix of size n x m. Each cell will hold one value from 0 to 9. There is a flag should be striped: each horizontal row of the flag should contain squares of the same color, and the colors of the adjacent horizontal rows should be different. We have to check given matrix is valid flag or not.
So, if the input is like
0 | 0 | 0 |
1 | 1 | 1 |
3 | 3 | 3 |
Steps
To solve this, we will follow these steps −
n := row count of matrix m := column count of matrix l := 'm' res := 1 for initialize i := 0, when i < n, update (increase i by 1), do: f := matrix[i, 0] for initialize j := 0, when j < m, update (increase j by 1), do: if matrix[i, j] is not equal to f, then: res := 0 if l is same as f, then: res := 0 l := f return (if res is non-zero, then true, otherwise false)
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; bool solve(vector<vector<int>> matrix){ int n = matrix.size(); int m = matrix[0].size(); char l = 'm'; bool res = 1; for (int i = 0; i < n; i++){ char f = matrix[i][0]; for (int j = 0; j < m; j++){ if (matrix[i][j] != f) res = 0; } if (l == f) res = 0; l = f; } return res ? true : false; } int main(){ vector<vector<int>> matrix = { { 0, 0, 0 }, { 1, 1, 1 }, { 3, 3, 3 } }; cout << solve(matrix) << endl; }
Input
{ { 0, 0, 0 }, { 1, 1, 1 }, { 3, 3, 3 } }
Output
1
- Related Articles
- C++ code to check given matrix is good or not
- C++ code to check string is diverse or not
- C++ code to check pattern is center-symmetrical or not
- C++ Program to check joke programming code is generating output or not
- C++ code to check grasshopper can reach target or not
- C++ program to check whether given string is bad or not
- C++ Program to check whether given password is strong or not
- C++ code to check all bulbs can be turned on or not
- C++ code to check water pouring game has all winner or not
- Check given matrix is magic square or not in C++
- C# program to check whether a given string is Heterogram or not
- C program to check if a given string is Keyword or not?
- C++ code to check array can be formed from Equal Not-Equal sequence or not
- Check if a given matrix is sparse or not in C++
- Check if a given number is sparse or not in C++

Advertisements