Skip to main content

Hackerrank CounterCode 2015 Campers

Problem Statement

Time is running out. You have a final match to play as a counter terrorist. You have N players each having a distinct ID from 1 to N. You have to choose some players on your team from these N players such that no two chosen players have consecutive numbers (as they tend to kill each other). Also you definitely have to choose some K players whose numbers are given. They are the snipers. Find the maximum number of players that you can choose.

Input Format

 The first line contains 2 space-separated integers, N and K, where N is the total number of players and K is the number of players that have to be definitely in the team (the snipers).
The second line contains K space-separated integers that are the IDs of the snipers.

NOTE: There are no two snipers with consecutive numbers.

Constraints

2N2×106
1KN/2
1 ID of each sniper N

Output Format

You need to print the maximum number of players that you can have in your team.

Sample Input
 
8 2
6 2

Sample Output
 
4

Explanation

There are 8 players in total, among which you have to definitely choose players with ID 2 and 6.

To maximize the number of players in the team, you will choose the players with IDs 4 and 8, so that you will have a total of 4 players.

Camper: A player in a professional team dedicated to using the AWP sniper rifle.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int compare(const void * a, const void * b)
{
   return (*(long long int*)a>*(long long int*)b);
}
int main()
{
   long long int n,k,i,d,sum=0,e;
    scanf("%lld %lld",&n,&k);
   long long int s[k];
   for(i=0;i<k;i++) 
   {
      scanf("%lld",&s[i]);
   }
   qsort(s,k,sizeof(long long int),compare);
   
    d=s[0]-0;
    if((d-2)%2>0){
        e = ((d-2)%2); 
        sum += e;
    }
    if((d-2)/2>0){
        e=((d-2)/2);
        sum += e;
    }
    
   d=n-s[k-1];
    
    if((d-1)%2>0){
        e=((d-1)%2);
        sum += e;
    }
    if((d-1)/2>0){
        e=((d-1)/2); 
        sum += e;
    }
    
   for(i=1;i<k;i++){
       d=s[i]-s[i-1];
       if((d-3)/2>0){
            e=((d-3)/2);
            sum += e;
       }
       if((d-3)%2>0){
       e=((d-3)%2);
       sum += e;
       }
   }
    
   printf("%lld",sum+k);
   return 0;
}

Comments

Popular posts from this blog

ORACLE 9i practice solutions

Created by BCL easyConverter SDK 3 (HTML Version)

Hackerrank > SQL > Basic Select

Select
01-Select All
Given a City table, whose fields are described as +-------------+----------+ | Field       | Type     | +-------------+----------+ | ID          | int(11)  | | Name        | char(35) | | CountryCode | char(3)  | | District    | char(20) | | Population  | int(11)  | +-------------+----------+
write a query that will fetch all columns for every row in the table.

My Solution
SELECT*FROM city;
---------------------------------------------------------------------------------
02-Select by ID
Given a City table, whose fields are described as

Zoho Puzzle Questions With Answers

Measuring Time Logic Puzzle You are given with two ropes with variable width. However if we start burning both the ropes, they will burn at exactly same time i.e. an hour. The ropes are non-homogeneous in nature. You are asked to measure 45 minutes by using these two ropes.

How can you do it?

Please note that you can’t break the rope in half as it is being clearly stated that the ropes are non-homogeneous in nature.
Answer & Explanation Solution: 45 minutes

Explanation :
All you have to do is burn the first rope from both the ends and the second rope from one end only simultaneously. The first rope will burn in 30 minutes (half of an hour since we burned from both sides) while the other rope would have burnt half. At this moment, light the second rope from the other end as well. Where, the second rope would have taken half an hour more to burn completely, it will take just 15 minutes as we have lit it from the other end too.

Thus you have successfully calculated 30+15 = 45 minutes …