From c81b016b088e02d27f9bd5ca79ae92337b930d2e Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Tue, 4 Jun 2024 01:32:39 +0530 Subject: [PATCH] Create 4 June Binary representation of next number --- 4 June Binary representation of next number | 38 ++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 4 June Binary representation of next number diff --git a/4 June Binary representation of next number b/4 June Binary representation of next number new file mode 100644 index 00000000..72248588 --- /dev/null +++ b/4 June Binary representation of next number @@ -0,0 +1,38 @@ +class Solution { + public: + string binaryNextNumber(string s) + { + // may2021 + + string result=""; + + int zerofound=0; + for(int i=s.size()-1 ; i>=0 ; i--) + { + if(s[i]=='1' && zerofound==0 ) + { + result='0'+result; + } + else if(s[i]=='0' && zerofound==0) + { + result='1'+result; + zerofound=1; + } + else + result=s[i]+result; + } + + if(zerofound==0) result='1'+result; + + int j=0; + + while(result[j]=='0') + { + j++; + } + result=result.substr(j, result.size()); + + return result; + + } +};