Friday, August 17, 2018

DOG AND CAT

  • Problem Description

    Chef is a farmer and a pet lover. He has a lot of his favorite pets cats and dogs in the barn. He does not know their exact count. But he knows that there are C cats and D dogs in the barn. Also, one day went to field and found that there were L legs of the animals touching the ground. Chef knows that cats love to ride on the dogs. So, they might ride on the dogs, and their legs wont touch the ground and Chef would miss counting their legs. Chefs dogs are strong enough to ride at max two cats on their back.

    It was a cold foggy morning, when Chef did this counting. So he is now wondering whether he counted the legs properly or not. Specifically, he is wondering is there a some possibility of his counting being correct. Please help Chef in finding it.
    Input

    First line of the input contains an integer T denoting number of test cases. T test cases follow.

    The only line of each test case contains three space separated integers C, D, L denoting number of the cats, number of the dogs and number of legs of animals counted by Chef, respectively.
    Output

    For each test case, output a single line containing a string ""yes"" or ""no"" (both without quotes) according to the situation.
    Constraints
    1<=T<=105
    0<=C, D, L<=109

    Subtasks

    Subtask #1 (20 points)

    1<=T<=104
    0<=C, D<=102

    Subtask #2 (30 points)

    1<=T<=105
    0<=C, D<=103
  • CODING ARENA
  • #include <stdio.h>
    int main()
    {
      int t,i,d,c,l;
      scanf("%d",&t);
      for(i=0;i<t;i++)
      {
        scanf("%d%d%d",&c,&d,&l);
        if(l%4==0)
        {
          if(c<=2*d)
          {
            if(l>=4*d && l<=(c+d)*4)
            {
              printf("yes\n");
            }
            else
            {
              printf("no");
            }
          }
          else if(c>2*d)
          {
            if(l>=(d+c-2*d) && l<=4*(c+d))
            {
              printf("yes\n");
            }
            else
            {
              printf("no\n");
            }
          }
        }
        else
        {
          printf("no\n");
        }
      }
      return 0;
    }
  • Test Case 1

    Input (stdin)
    3
    
    1 1 8
    
    1 1 4
    
    1 2 3
    
    
    Expected Output
    yes
    
    yes
    
    no
  • Test Case 2

    Input (stdin)
    2
    
    2 2 16
    
    4 3 28
    
    
    Expected Output
    yes
    
    yes

1 comment:

  1. import java.util.Scanner;

    class CatsAndDogs {
    Scanner scanner = new Scanner(System.in);

    public static void main(String args[]) {
    CatsAndDogs obj = new CatsAndDogs();
    long[] numbers = obj.ioOperations();
    for (int i = 0; i < numbers.length; i += 3) {
    if (obj.areLegsPossible(numbers[i], numbers[i + 1], numbers[i + 2]))
    System.out.println("yes");
    else
    System.out.println("no");
    }
    }

    long[] ioOperations() {
    int inputSize = getIntFromUser();
    long input[] = new long[inputSize * 3];
    for (int i = 0; i < input.length; i++) {
    input[i] = getLongFromUser();
    }
    return input;
    }

    boolean areLegsPossible(long cats, long dogs, long legsOnGround) {
    long maxTotalLegs = (cats + dogs) * 4;

    if (legsOnGround % 4 != 0 || legsOnGround < dogs * 4
    || legsOnGround > maxTotalLegs)
    return false;

    if ((cats + dogs) * 4 == legsOnGround)
    return true;

    long totalCatLegs = cats * 4;// legsOnGround - totalDogLegs;
    long totalDogLegs = dogs * 4;

    long maxPossiblCatLegsOnDogs = totalDogLegs * 2;// 2CatsOnDog
    long possibleCatLegsOnGround = totalCatLegs - maxPossiblCatLegsOnDogs;

    if (possibleCatLegsOnGround % 4 != 0
    || possibleCatLegsOnGround > legsOnGround - totalDogLegs)
    return false;
    if (possibleCatLegsOnGround < 0 || possibleCatLegsOnGround % 4 == 0)
    return true;

    return false;
    }

    int getIntFromUser() {
    return scanner.nextInt();
    }

    long getLongFromUser() {
    return scanner.nextLong();
    }
    }


    ReplyDelete