Tony Stark is on the planet Titan crying for his friends are turning into ashes, and on earth, mayhem has ensued since a lot of people are turning into ashes too. Some trains have been derailed in such a way that a lot of its coaches are thrown off in random disarray like coach 3, 4, and 5 are thrown off in one place, coach 2 and 6 are thrown off in another place, etc.
S.H.I.E.L.D calls upon Hulk and jarvis to help them collect and connect some of the thrown off coaches of those trains, but a train can only move if the collected coaches number are in a continuous manner (need not to be in order), like 1234, 2314, 4123, 2341, etc.
Help Jarvis write a program for hulk to decide whether collected coaches will move or not.
Example:
Input: s="4231"
Output: YES
Approach
C++
#include <bits/stdc++.h>using namespace std;bool helpJarvis(string s){vector<char> v;for (int i = 0; i < s.size(); i++)v.push_back(s[i]);sort(v.begin(), v.end());for (int i = 0; i < v.size() - 1; i++)if ((v[i + 1] - v[i]) != 1)return false;return true;}int main(){string s = "4231";bool ans = helpJarvis(s);if (ans == true)cout << "YES\n";elsecout << "NO\n";return 0;}
No comments:
Post a Comment