Sort by

recency

|

751 Discussions

|

  • + 0 comments

    Solution in go

    func fairRations(B []int32) string {
        oddIndex := -1
        totalBreads := int32(0)
        
        for i := 0; i < len(B); i++ {
            if B[i]%2 > 0 {
                if oddIndex < 0 {
                    oddIndex = i
                    continue
                }
                
                totalBreads += int32((i - oddIndex) * 2)
                oddIndex = -1
            }
        }
        if oddIndex >= 0 {
            return "NO"
        }
        
        return fmt.Sprintf("%d", totalBreads)
    }
    
  • + 0 comments

    My Rust solution: -

    fn is_odd(number: &i32) -> bool {
        number % 2 != 0
    }
    
    
    fn fairRations(B: &[i32]) -> String {
        let mut b_vec = B.to_vec();
        let mut counter = 0;
        for i in 0..(b_vec.len() - 1) {
            if is_odd(&b_vec[i]) {
                b_vec[i + 1] += 1;
                counter += 2;
            }
        }
        
        if b_vec.last().unwrap() % 2 == 0 && b_vec.len() > 2 {
            return counter.to_string() 
        }
        return "NO".to_string()
    }
    
  • + 0 comments

    Fair Rations has always been about providing everyday essentials with care and consistency, making sure businesses feel supported in the small details that matter most. Whether it’s stocking supplies or ensuring comfort, reliability plays a huge role in keeping operations smooth. A simple service like clean linens for your business can make a noticeable difference in presentation and customer satisfaction. This kind of thoughtful approach helps maintain standards while keeping everything stress-free.

  • + 0 comments

    JAVA

    public static String fairRations(List B) { // Write your code here

    int count = 0;
    for(int i=0;i<B.size();i++){
        int num = B.get(i);
        if(B.get(i)%2!=0){
            B.set(i,num+1);
            count++;
            if(i==B.size()-1){
                B.set(i-1,B.get(i-1)+1);
                count++;
            }else{
                B.set(i+1,B.get(i+1)+1);
                count++;
            }
        }
    }
    boolean flag = true;
    for(int i=0;i<B.size();i++){
        if(B.get(i)%2!=0){
            flag = false;
            break;
        }
    }
    String result = ""+count;
    if(flag==true){
        return result;
    }else{
        return "NO";
    }
    
    }
    
  • + 0 comments
    def fairRations(B):
        # Write your code here
        c = 0
        n = len(B)
        for i in range(n - 1):
            if B[i] % 2 != 0:
                B[i + 1] += 1
                c += 2
                
        return "NO" if B[-1] % 2 != 0 else str(c)