Sort by

recency

|

733 Discussions

|

  • + 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

    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";
    

    }

  • + 0 comments

    string fairRations(vector B) {

     int res = 0;
     // size1 = sizeof(B)/sizeof(B[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";
    

    }

  • + 0 comments

    Haskell

    module Main where
    
    solve :: [Int] -> Maybe Int
    solve bs = go bs 0
      where
        go [] acc = Just acc
        go [x] acc = if even x then Just acc else Nothing
        go (x : y : xs) acc
            | even x = go (y : xs) acc
            | otherwise = go (y + 1 : xs) (acc + 2)
    
    main :: IO ()
    main = do
        n <- readLn :: IO Int
        bs <- map read . words <$> getLine :: IO [Int]
        case solve bs of
            Just acc -> print acc
            Nothing -> putStrLn "NO"