fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int getValidStrings(vector<int> &arr){
  4. int n=arr.size();
  5. int ans=1; //assume to have atleast one possible ans
  6. for(int i=0;i<=n-2;i++){
  7. int diff=abs(arr[i]-arr[i+1]);
  8. if(arr[i]==arr[i+1]){ //observation 1
  9. ans=ans*2;
  10. }
  11. else if(diff>1){
  12. return 0;
  13. }
  14. else { //obervation 2
  15. ans=ans*1;
  16. }
  17. }
  18. return ans;
  19.  
  20. }
  21. int main() {
  22. // your code goes here
  23. vector<int>arr={1,2,3,2,1};
  24. cout<<"The count of valid strings that satisfy the conitions are:"<<getValidStrings(arr);
  25. return 0;
  26. }
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
The count of valid strings that satisfy the conitions are:1