Sort by

recency

|

737 Discussions

|

  • + 0 comments
    int count = 0;
        if(B.size() <= 1) return "NO";
        else {
            for(int i = 0; i< B.size(); i++){
                if(B[i]%2 == 0) continue;
                if (i + 1 < B.size()) {
                    B[i]++;
                    B[i + 1]++;
                    count += 2;
                }
            }
        }
        
        return B[B.size()-1]%2 == 0? to_string(count) : "NO";
    
  • + 0 comments

    Solution from my side

    count = 0
        for i in range(len(B)-1):
            if(B[i]%2 ==1 ):
                B[i]+= 1
                B[i+1]+=1
                count +=2
        for j in range(len(B)):
            if(B[j]%2 == 1):
                return 'NO'
        return str(count)
    
  • + 0 comments

    string fairRations(vector B) { string ans; int cnt=0; int n=B.size();

    for (int i=0;i<n;i++){
        if(i==n-1 && B[i]%2!=0){
            ans="NO";
            return ans;
    
    
        }
        if(B[i]%2!=0){
    
           B[i]++;
           B[i+1]++;
           cnt+=2;
    
        }
    }
    ans=to_string(cnt);
    return ans; ;
    

    }

  • + 0 comments

    Here is my c++ solution: you can watch the explanation here : https://youtu.be/pAzUgM52d60

    string fairRations(vector<int> B) {
        int res = 0;
        for(int i = 0; i < B.size() - 1; i++){
            if(B[i] % 2 == 1){
                B[i+1]++;
                res+=2;
            }
        }
        return B[B.size() - 1] % 2 == 0 ? to_string(res) : "NO";
    }
    

    Whithout the if

    string fairRations(vector<int> B) {
        int res = 0;
        for(int i = 0; i < B.size() - 1; i++){
            B[i+1] += B[i] % 2;
            res += B[i] % 2;
        }
        return B[B.size() - 1] % 2 == 0 ? to_string(res*2) : "NO";
    }
    
  • + 0 comments

    C++ Solution:

    string fairRations(vector<int> B) {
      int res = 0;
      char pattern[3];
    
      for(size_t i = 0; i < B.size() - 1; i++) {
        pattern[0] = B[i] % 2 == 0 ? 'E' : 'O';
        pattern[1] = B[i + 1] % 2 == 0 ? 'E' : 'O';
        pattern[2] = 0;
    
        if(((i + 1) == (B.size() - 1)) &&
           (strcmp(pattern, "OE") == 0 ||
            strcmp(pattern, "EO") == 0)) {
          return "NO";
        }
    
        if (strcmp(pattern, "OO") == 0 || 
            strcmp(pattern, "OE") == 0) {
          B[i] += 1;
          B[i + 1] += 1;
          res+=2;
        } 
      }
      
      return to_string(res);
    }