Sort by

recency

|

735 Discussions

|

  • + 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);
    }
    
  • + 0 comments

    SWIFT

    func fairRations(B: [Int]) -> String {
        // Write your code here
        var oddIndex: [Int] = [Int]()
        var oddDict: [Int: Int] = [:]
        var steps: Int = 0
        
        for index in 0 ... B.count - 1 {
            if B[index] % 2 == 1 {
                oddIndex.append(index)
                oddDict[index] = index
            }
        }
        
        if oddIndex.count % 2 == 1 { return "NO" }    
        
        while(oddIndex.count > 1) {
          var currentOddIndex: Int = oddIndex.first!
          steps += 2
          let nextOddIndex: Int = currentOddIndex + 1
            if oddDict[nextOddIndex] == nil {
                oddIndex[0] = nextOddIndex
                oddDict.removeValue(forKey: currentOddIndex)
                oddDict[nextOddIndex] = nextOddIndex
                continue
            }
              
            while(currentOddIndex <= nextOddIndex) {
                oddIndex.removeFirst()
                oddDict.removeValue(forKey: currentOddIndex)
                currentOddIndex += 1
            }
        }
            
        return String(steps)
    }
    
  • + 0 comments

    string fairRations(vector B) { int result=0; int n=B.size(); for (int i=0;i

    }
    return B[B.size() - 1] % 2 == 0 ? to_string(result*2) : "NO";
    

    }