
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if Array Can Be Formed from Equal/Not Equal Sequence in C++
Suppose we have a string S of length . Consider there are n numbers and they are arranged in a circle. We do not know the values of these numbers but if S[i] = 'E' it indicates ith and (i+1)th numbers are same, but if that is 'N' then they are different. From S we have to check whether we can recreate the sequence or not.
So, if the input is like S = "ENNEENE", then the output will be True, because we can assign values like [15,15,4,20,20,20,15].
Steps
To solve this, we will follow these steps −
if S has single 'N', then: return false return true
Example
Let us see the following implementation to get better understanding −
#include<bits/stdc++.h> using namespace std; bool solve(string S){ if (count(S.begin(), S.end(), 'N') == 1) return false; return true; } int main(){ string S = "ENNEENE"; cout << solve(S) << endl; }
Input
"ENNEENE"
Output
1
Advertisements