
- 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 all bulbs can be turned on or not
Suppose we have a number m and a nested list A with n sub-lists. Consider there are m bulbs, initially all are turned off. There are n buttons and each of them is connected to some set of bulbs. So A[i] is the set of bulbs that can be turned on by pressing ith switch. We have to check whether we can lit up all the bulbs or not.
So, if the input is like A = [[1, 4], [1, 3, 1], [2]]; m = 4, then the output will be True, because by pressing all switches we can turn on all four bulbs.
Steps
To solve this, we will follow these steps −
Define one set s for initialize i := 0, when i < size of A, update (increase i by 1), do: for initialize j := 0, when j < size of A[i], update (increase j by 1), do: insert A[i, j] into s if size of s is same as m, then: return true Otherwise return false
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; bool solve(vector<vector<int>> A, int m){ set<int> s; for (int i = 0; i < A.size(); i++){ for (int j = 0; j < A[i].size(); j++){ s.insert(A[i][j]); } } if (s.size() == m) return true; else return false; } int main(){ vector<vector<int>> A = { { 1, 4 }, { 1, 3, 1 }, { 2 } }; int m = 4; cout <<solve(A, m) << endl; }
Input
{ { 1, 4 }, { 1, 3, 1 }, { 2 } }, 4
Output
1
- Related Articles
- C++ code to check array can be formed from Equal Not-Equal sequence or not
- C++ code to check grasshopper can reach target or not
- C++ code to check water pouring game has all winner or not
- Program to check all tasks can be executed using given server cores or not in Python
- C++ code to check string is diverse or not
- C++ Program to check string can be reduced to 2022 or not
- C++ code to check pattern is center-symmetrical or not
- C++ code to check given matrix is good or not
- C++ code to check given flag is stripped or not
- Program to check whether we can take all courses or not in Python
- Program to check whether we can unlock all rooms or not in python
- Program to check whether all can get a seat or not in Python
- Program to check subarrays can be rearranged from arithmetic sequence or not in Python
- C++ Program to check if two stacks of letters can be emptied or not
- C++ Program to check given candies can be split with equal weights or not

Advertisements