Sort by

recency

|

739 Discussions

|

  • + 0 comments
    def fairRations(B):
        total_loaves = 0
        for i in range(len(B) - 1):
            if B[i] % 2 != 0:
                B[i] += 1
                B[i + 1] += 1
                total_loaves += 2
        if any(b % 2 != 0 for b in B):
            return "NO"
        return str(total_loaves)
    
  • + 0 comments

    Here is my Python solution!

    def fairRations(B):
        loaves = 0
        if sum(B) % 2 == 1:
            return "NO"
        for person in range(len(B) - 1):
            if B[person] % 2 == 1:
                B[person + 1] += 1
                loaves += 2
        return str(loaves)
    
  • + 1 comment

    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
    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)