Showing posts with label Code Challenge. Show all posts
Showing posts with label Code Challenge. Show all posts

Monday, 13 November 2017

Code Challenge Solutions 4


1. Hanyu’s Kung Fu Match

Hanyu is glad as his academy of martial arts is chosen to present a warm-up match of Kung Fu at the inaugural ceremony of the Rio Olympics. 
There are 'n' Kung Fu fighters in Hanyu’s academy. 

Hanyu is very particular in selecting two fierce Kung Fu fighters from his academy who are going to put up a thrilling close competitive performance at the ceremony. 
  
For this purpose, he fetches the details of the winning points of all 'n' fighters in his academy and stores it in an array. 
He should now select the two fighters who have scored almost the same win percentages so far, as they would make the audience exhilarating till the end.  
  
Your aim is to help Hanyu find the difference in win percentages between two fighters chosen for the warm-up match. 
                                
Examples: 
Input  :  If n = 4 and winPoints[] = {30,5,20,9} 
Output :  4 

Input Format: 
The first line of the input consists of an integer n, which corresponds to the number of fighters in Hanyu’s academy. Assume that the maximum number of input elements in the array is 50. 
The next 'n' lines in the input corresponding to the winning points of each of the fighter. 
Output Format: 
The output is an integer – difference between two fighters chosen for the warm-up match. 

Refer sample input and output for formatting specifications. 

Sample Input1: 
19 
18 
25 
Sample Output1: 


Program


#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j,n,diff,d;
int a[100];
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
diff=abs(a[0]-a[1]);
for(i=0;i<n-1;i++)

for(j=i+1;j<n;j++)
{
d=abs(a[i]-a[j]);
if(d<diff)
diff=d;
}

printf("%d",diff);
return 0;
}


2. Bhageera And Mowgli


Mowgli Bagheera is teaching mathematics to Mowgli. He has given N integer numbers to Mowgli and asked him “What is the sum of these N numbers?”Mowgli told him that he is not good in numbers yet and he can’t perform this addition operation but if Bagheera allow him to convert each integer into its nearest 10’s multiple, he can tell the sum of those given numbers. For e.g. if the last digit of the number is 5 or more than 5, he will convert it into nearest 10’s multiple that is greater than the given number. If last digit is less than 5, he will convert it into nearest 10’s multiple that is less than the number. After that he will count the sum of all these numbers and tell Bagheera.  Input Format:  First input is an integer N>=0 which is number of elements given. Next input is an array which will hold N number.  Output Format:  Output is also an integer which will hold the sum.  [All text in bold corresponds to input and the rest corresponds to output] Sample Input and Output:  5 11   16   15   24   4 70


Program:

#include <Stdio.h>
#include <conio.h>
main()
{
   int n,u,i,arr[100];
   int sum=0,temp=0;
   scanf ("%d", &n) ;
   for(i=0;i<n;i++)
   {
       scanf("%d", &arr[i]);
   }

   for(i=0;i<n;i++)
   {
       u = arr[i] % 10;
       if(u>=5)
       {
           temp=arr[i]+(10-u);
       }
       else{
        temp=arr[i]-u;
       }
       sum+=temp;
   }
   printf("\n%d",sum);
   return 0;

}





Sunday, 12 November 2017

Code Challenge Solutions 3

1. Mahirl and Educational Fair


To make her Event Management company “Amphi Events and Decors” popular among the younger generation, Mahirl planned to conduct “The Great India Education Fair’. This Fair has proved to be a strong and cost-effective marketing platform for the Universities, Colleges, Schools and Education Institutions to reach out very cost-effectively to a larger number of parents and students.

              
There are many popular magazines that review these kinds of educational fairs and rate them.
They rate the fair as “Good” if the total number of people attending the fair is greater than 7000.
They rate the fair as “Outstanding” if both the number of parents attending the fair and the number of students attending the fair is at least 5000. However, if the number of students attending the fair is at least double the number of parents attending the fair, they rate the fair as “Great”.
If the total number of people attending the fair is less than 7001, they rate the fair as “Bad”.
Can you please help them in rating the fair?
Input Format:
Input consists of integers. The first integer corresponds to the number of parents who have attended the fair. The second integer corresponds to the number of students who have attended the fair.
Output Format:
The output consists of a string --- “Bad” or “Good” or “Outstanding” or “Great” or “Invalid Input”.
If any of the 2 input integers are negative, print “Invalid Input”.
Sample Input :
6000
5600
Sample Output :
Outstanding


Program:

#include<stdio.h>
int main()
{
int s,p;
scanf("%d%d",&p,&s);
if((s>=0)&&(p>=0))
{
if(s>=(2*p))
printf("Great");
else if(s>=5000&&p>=5000)
printf("Outstanding");
else if((p+s)>=7000)
printf("Good");
else
printf("Bad");
}
else
printf("Invalid Input");
return 0;
}


2. Watermelon Problem

[A variation of Code Forces Problem]

 
One hot summer day Peter and his friend Billy decided to buy a watermelon. They chose the biggest and the ripest one, in their opinion. After that, the watermelon was weighed, and the scales showed w kilos. They rushed home, dying of thirst, and decided to divide the berry, however they faced a hard problem.
 
Peter and Billy are great fans of even numbers, that's why they want to divide the watermelon in such a way that each of the two parts weighs the even number of kilos, at the same time it is not obligatory that the parts are equal. But the difference between the 2 parts should be minimal. The boys are extremely tired and want to start their meal as soon as possible, that's why you should help them and find out if they can divide the watermelon in the way they want. For sure, each of them should get a part of the positive weight.
 
Input Format
The first (and the only) input line contains integer number w (1 = w = 100) — the weight of the watermelon bought by the boys.
 
Output Format
If the input value is not within the range, print "Invalid Input".
In the first line of the output, print YES, if the boys can divide the watermelon into two parts, each of them weighing an even number of kilos; and NO in the opposite case.
If the first line of the output is YES, the next line of the output consists of 2 integers separated by a space. In case of distinct integers, the smallest number should appear first.
 
Sample Input 1
8
 
Sample Output 1
YES
4 4
 
Sample Input 2
11
 
Sample Output 2
NO
 
Sample Input 3
124
 
Sample Output 3
Invalid Input


Program:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int w,c;
    scanf("%d",&w);
    if((w>=1)&&(w<=100))
    {
        if(w%4==0)
        {

            c=w/2;
            printf("YES");
            printf("\n%d%d",c,c);
        }
        else if(w%2==0)
        {
            c=w/2;
            printf("YES");
            printf("\n%d%d",c+1,c-1);
        }
        else{printf("NO");}
    }
    else
    {
      printf("Invalid Input\n");
    }

    return 0;
}



Code Challenge Solutions 1:

Code Challenge Solutions 2

Code Challenge Solutions 2

1. Emily’s Lucky Awards

Madam Emily often conducts surprise tests for her ‘n’ kids. 
That day too it was a shocking surprise for the kids as Emily announced it no sooner the kids were back from their vacation. 
Evaluating the performances of the kids Emily found the overall result to be very inconsistent as there were positive as well negative scores from the kids. 

However Emily wished to give away lucky awards to the kids in a means to motivate them. She is much fond of number ‘f’. But she couldn’t find any student among ‘n’ students who has scored f marks. 

Hence she decided to give away the awards to the pair of students whose scores sum is f. 

Examples: 
Input  :  If n = 4, scores[] = {1, 5, 7, -1} and Emily’s fond number f = 6. 
Output :  2 
Pair with sum 6 are (1, 5) and (7, -1) 
Let us help Emily determine those pairs of scores.  
  
Input Format: 
The first line of the input consists of an integer, n that corresponds to the number of kids. Assume that the maximum number of input elements in the array is 50. 
The next 'n' lines in the input consists of an integer, which correspond to the scores of kids. 
The next line of the input consists of an integer f, which corresponds to Emily’s fond number. 

Output Format: 
Output is an integer – number of pairs whose sum is equal to fond number. 
Refer sample input and output for formatting specifications. 

Sample Input1: 
-1 
Sample Output1: 
  
Sample Input2: 
Sample Output2: 


Program

#include<string.h>
#include<stdio.h>
int main()
{
int n,f,a[100],c=0,i,j;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
scanf("%d",&f);
for(i=1;i<n;i++)
{
for(j=1;j<n;j++)
{
if((a[i]+a[j])==f)
{
c++;
}
}}
printf("%d",c);
return 0;
}


2. Special Name Problem

Arvind believes in Numerology a lot and when his first child was born, he consulted the leading numerologist in the city about selecting a name for his child. The numerologist suggested that the name that would be lucky for his child should have the following characteristics --- It should not start with a vowel and it should not end with a vowel and it should contain exactly 1 vowel.
Can you help Arvind in shortlisting the names for his child?
Assume that the maximum length of the name is 30.
Input and Output Format:
Input consists of a string that corresponds to one of the names.
The output consists of a single string that is either yes or no. Output yes if the name can be shortlisted based on the suggested criteria.
Sample Input 1:
Sam
Sample Output 1:
yes
Sample Input 2:
Bame
Sample Output 2:
no

Program


#include<string.h>
#include<stdio.h>
int main()
{
int l,i,a=0;
char name[30];
scanf("%s",name);
l=strlen(name);
for(i=1;i<l-1;i++)
{
if((name[i]=='a')||(name[i]=='e')||(name[i]=='i')||(name[i]=='o')||(name[i]=='u'))
{
a++;
}
}
if((name[0]=='a')||(name[0]=='e')||(name[0]=='i')||(name[0]=='o')||(name[0]=='u'))
printf("\nno");
else if((name[l-1]=='a')||(name[l-1]=='e')||(name[l-1]=='i')||(name[l-1]=='o')||(name[l-1]=='u'))
printf("\nno");
else
if(a==1)
printf("\nyes");
else
printf("\nno");
return 0;
}




More Code challenge Problems and solutions:


Code Challenge Solutions

  1. Seat Belt Problem


Dr. Dexter's car's seat belt detectors have been corrupted. So he wants you to build a module which checks if all the seat belts are worn. If and only if all the seat belts are worn Xinyou must start driving
Write a C program module that checks if all the seat belts have been worn.


Input and Output Format:

Input consists of n+1 lines.

The first line of input is an integer 'n' corresponding to the number of seats. 0 < n < 32767

The next n lines consist of strings 'yes' or 'no' corresponding to whether the seat belts have been worn or not.

Output consists of a single string 'Yes I can drive', 'No I can't drive' or 'Invalid Input'

Assume that all inputs consist of lowercase characters only.

All text in bold corresponds to input and the rest to output


Sample Input1:
2
no
no
Sample Output 1:
No I can't drive


Sample Input 2:
3
yes
yes
yes
Sample Output 2:
Yes I can drive

Sample Input 3:
3
No
Yes
Yes
Sample Output 3:
Invalid Input


Program

#include<stdio.h>
#include<string.h>
int main()
{
char a[100];
int n,i,count=0;
scanf("%d",&n);
if( n>0  )
{
for(   i=0;i<n;i++   )
{
scanf("%s",a);
if(strcmp(a,"yes") == 0)
{
count++;
}
else if( strcmp(a,"No") == 0 || strcmp(a,"Yes") == 0)
{
printf("\nInvalid Input");
return 0;
}
}
if(   count == n     )
printf("Yes I can drive");
else
printf("No I can't drive");
}
else
printf("Invalid Input");
return 0;
}


2.Petrol Variety Problem


Dr. Dexter is now in the gas bunk.

 He has perfectly planned his trip. 
He has been working keenly on the budget so that the trip goes on well.
 Now, he finds that there are two varieties of fuel – Ordinary and Speed.
 Speed fuel yields him a fairly better performance than its ordinary counterpart. 
He wants Xinyou to find out as to which variety of fuel he must opt for to stick to the budget.
Write a C program to find out the variety of fuel.
Input and Output Format:
Input consists of 5 lines.
The first line of input is a floating point number 'm' corresponding to the mileage Dr.Dexter's car yields.
The second line and third lines of input consists of floating point numbers 'o' and 's'
 corresponding to the costs of ordinary fuel and speed fuel respectively.
The fourth line of input is an integer 'd' corresponding to the distance between the bunk and Shinyao.
The fifth line of input is a floating point number 'b' corresponding to the budget for the fuel.
Output consists of a string 'Ordinary' or 'Speed'.
Sample Input 1: 
25 = m
75 = o
100 = s
3 = d
500 = b
Sample Output 1:
Speed
Sample Input 2: 
25
75
100
25
80
Sample Output 2:
Ordinary

Program

#include<stdio.h> 
int main() 
int m,o,s,d,b,i=0; 
scanf("%d%d%d%d%d",&m,&o,&s,&d,&b); 
if(d <= m) 
i++; 
else 
{while( d>m) 
d-=m; 
i++; 
if(b>=(i*s)) 
printf("\nSpeed"); 
else 
if(  b < (i*s)) 
printf("\nOrdinary"); 
}return 0;