fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Solution {
  5. public:
  6. string util(string str, int currLevel, int finalLevel) {
  7. if(currLevel==finalLevel) return str;
  8.  
  9. string newString="";
  10. for(char& each: str) {
  11. if(each=='0')
  12. newString+="01";
  13. if(each=='1')
  14. newString+="10";
  15. }
  16.  
  17. return util(newString, currLevel+1, finalLevel);
  18. }
  19.  
  20. int kthGrammar(int N, int K) {
  21. string ans = util("0", 1, N);
  22.  
  23. return ans[K-1]-'0';
  24. }
  25. };
  26.  
  27. int main() {
  28. // your code goes here
  29. cout<<kthGrammar(2, 2);
  30. return 0;
  31. }
Success #stdin #stdout 0s 4384KB
stdin
Standard input is empty
stdout
Standard output is empty