LM10xLeoMessi's blog

By LM10xLeoMessi, history, 5 weeks ago, In English

i was writing this code and faced this unknown behavior.

void solve(){
    string s = "a";
    for(int i = 0; i < s.size() - 1; i++){
        cout << "1st for loop\n";
        if(s[i] == s[i + 1]){
            cout << s[i] << s[i + 1] << '\n';
            return;
        }
    }
    for(int i = 0; i < s.size() - 2; i++){
        cout << "2nd for loop " << i << '\n';
        if(s[i] != s[i + 1] and s[i] != s[i + 2] and s[i + 1] != s[i + 2]){
            cout << s[i] << s[i + 1] << s[i + 2] << '\n';
            return; 
        }
    }
    cout << -1;
}

shouldn't it print -1? then why it enters into second for loop as the size of my string is 1? Can anyone help me to understand this behavior?

Full text and comments »

  • Vote: I like it
  • +5
  • Vote: I do not like it

By LM10xLeoMessi, history, 2 months ago, In English

can anyone help me to find two node that are not adjacent to each other but has highest degree.

suppose node 1 with 4 degree, node 5 with 4 degree but node 1 and 5 are adjacent so we cant choose node 1 and 5. another node 8 has a degree of 3. and all other node has degree <= 3. also node 5 and 8 are not adjacent, so we can choose node 1 and 8, or 5 and 8. I did not come up with any idea so seeking for help

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it