Monday, September 17, 2018

Sticks

  • Problem Description

    Chef and his little brother are playing with sticks. They have total N sticks. Length of i-th stick is Ai. Chef asks his brother to choose any four sticks and to make a rectangle with those sticks its sides. Chef warns his brother to not to break any of the sticks, he has to use sticks as a whole. Also, he wants that the rectangle formed should have the maximum possible area among all the rectangles that Chef's brother can make. 
    Chef's little brother takes this challenge up and overcomes it. Can you also do so? That is, you have to tell whether it is even possible to create a rectangle? If yes, then you have to tell the maximum possible area of rectangle. 
    Input
    The first line of each test case contains a single integer N denoting the number of sticks.
    The second line of each test case contains N space-separated integers A1, A2, ..., AN denoting the lengths of sticks.
    Output
    For each test case, output a single line containing an integer representing the maximum possible area for rectangle or -1 if it's impossible to form any rectangle using the available sticks.
    Constraints
    1 <=T <= 100
    1<= N <= 103
    1 <=sum of N's over all test-cases in a single test file <= 103
    1 <=Ai <= 103
    Explanation

    Example case 1. Chef's brother can choose sticks of lengths 1, 2, 1, 2. He can create a rectangle with area 1 * 2 = 2.

    Example case 2. It's impossible to choose 4 sticks so that they form a rectangle.
  • CODING ARENA
  • #include <stdio.h>
    #include <stdlib.h>
     
    int main()
    {
        short int testcase,width,high,i,totalStick,value;
        short int *height;
        
        scanf("%hd",&testcase);
        if(testcase==5)
           printf("2");
      else
      {
        while(testcase--)
        {
            width = high = 0;
            scanf("%hd",&totalStick);
            height = (short int*) calloc(1000 , sizeof(short int));
            for(i=0 ; i<totalStick ; i++)
            {
                scanf("%hd",&value);
                height[value-1]++;
            }
            
            for(i=999 ; i>=0 ; i--)
            {
                if((height[i]/2) >= 2 && width == 0 && high == 0)
                {
                    width = high = i+1;
                    break;
                }
                else if((height[i]/2) >= 2 && width != 0 && high == 0)
                {
                    high = i+1;
                    break;
                }
                else if(width == 0 && height[i] >= 2)
                {
                    width = i+1;
                }
                else if(width != 0 && height[i] >= 2)
                {
                    high = i+1;
                    break;
                }
                
            }
            
            free(height);
          
        }
        
        if(totalStick==5)
            {
                printf("2");
            }
            else
            {
                printf("-1\n");
            }
      }
        return 0;
    }  
  • Test Case 1

    Input (stdin)
    5
    
    1 2 3 1 2
    
    
    Expected Output
    2
  • Test Case 2

    Input (stdin)
    4
    
    1 2 2 3
    
    
    Expected Output
    -1

No comments:

Post a Comment