Monday, April 29, 2019

Pool

  • Problem Description

    You are planning to go for swimming classes. You would prefer to enroll in the center which has the swimming pool of a greater area. In the first centre that you visit, the swimming pool is a circular shape(radius-r). In the next centre that you visit, the swimming pool is of a square shape (side-S). Write a program that will help you to make the choice of the swimming pool. 

    Input : 
    Input consists of 2 integers. The first integer correspond to the radius (r) of the circular swimming pool, The second integer corresponds to the side (S) of the square swimming pool. 
  • Test Case 1

    Input (stdin)
    4 4

    Expected Output
    I prefer centre 1
  • Test Case 2

    Input (stdin)
    4 8

    Expected Output
    I prefer centre 2
Solution
#include <stdio.h>
const double pi=3.141;
int main ()
{
int r,s;
scanf ("%d%d",&r,&s);
if ((pi*r*r)>(s*s))
{
printf ("I prefer centre 1");
}
else
{
printf ("I prefer centre 2");
}
return 0;
}


Square of a Number

  • Problem Description

    Write a C program to find the square of a number
  • Test Case 1

    Input (stdin)
    2

    Expected Output
    4
  • Test Case 2

    Input (stdin)
    5

    Expected Output
    25
Solution

#include <stdio.h>
int main()
{
int a,squ;
  scanf ("%d%d",&a,&squ);
  squ=a*a;
  printf ("%d",squ);
return 0;
}

Printing Alphabets

  • Problem Description

    Remove Characters in the given String Except Alphabets
  • Test Case 1

    Input (stdin)
    t28lkj

    Expected Output
    tlkj
  • Test Case 2

    Input (stdin)
    p2r66o@gram84iz./@@@@@@@@@@@

    Expected Output
    programiz
Solution


#include<stdio.h>
int main()
{
    char line[150];

    int i, j;
    scanf ("%[^\n]s",line);
    for(i = 0; line[i] != '\0'; ++i)
    {
        while (!( (line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z') || line[i] == '\0') )
        {
            for(j = i; line[j] != '\0'; ++j)
            {
                line[j] = line[j+1];
            }
            line[j] = '\0';
        }
    }
    puts(line);
    return 0;
}

Length of the String

  • Problem Description

    Program which will accept string from the user and find the length of the string
  • Test Case 1

    Input (stdin)
    welcome

    Expected Output
    7
  • Test Case 2

    Input (stdin)
    input

    Expected Output
    5
Solution

#include <stdio.h>
#include <string.h>
 
int main()
{
  char a[100];
  int length;
  scanf ("%[^\n]s",a); 
  length = strlen(a); 
  printf("%d\n", length); 
  return 0;
}

Good Joke!

Vadim and Roman like discussing challenging problems with each other. One day Vadim told his friend following problem:
Given N points on a plane. Each point p is defined by it's two integer coordinates � px and py. The distance between points a and b is min(|ax - bx|, |ay - by|). You should choose a starting point and make a route visiting every point exactly once, i.e. if we write down numbers of points in order you visit them we should obtain a permutation. Of course, overall distance walked should be as small as possible. The number of points may be up to 40.
"40? Maybe 20? Are you kidding?" � asked Roman. "No, it's not a joke" � replied Vadim. So Roman had nothing to do, but try to solve this problem. Since Roman is really weak in problem solving and you are the only friend, except Vadim, with whom Roman can discuss challenging tasks, he has nobody else to ask for help, but you!

Input

Input description.
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.The first line of each test case contains a single integer N denoting the number of points on a plane. The following N lines contain two space-separated integers each � coordinates of points.

Output

Output description.
Output the answer for every test case in a separate line. The answer for every test case is a permutation of length N. In case there are several solutions that lead to minimal distance walked, you should choose the lexicographically smallest one. Let P denote such permutation. To make output smaller, you should output H(P)H(P) = P1 xor P2 xor ... xor PN. Have a look at the example and it's explanation for better understanding.

Constraints

  • 1 = T = 10
  • 1 = N = 40
  • 0 = absolute value of each coordinate = 1000
  • 1 = sum over all N in a single test file = 120

Example

Input:
2
2
1 2
0 0
3
3 3
0 0
0 3
Output:
3
0

Solution:

#include<stdio.h>
int main()
{
int T,i,j;
scanf("%d",&T);
int p[40][2];
int N;
int out[T];
for(i=0;i<T;i++)
{
out[i] = 0;
scanf("%d",&N);
for(j=0;j<N;j++)
{
scanf("%d %d",&p[j][0],&p[j][1]);
out[i] = (out[i]^(j+1));
}
}
for(i=0;i<T;i++)
printf("%d\n",out[i]);
return 0;
}


Sunday, April 28, 2019

Sorting

Alice uses the following pseudocode when she needs to sort a permutation of Npositive integers:

procedure Sort(list A) defined as: 
   list lessgreater
   if length(A) <= 1 then return A
   pivot := A(length(A)+1) / 2
   for i := 1 to length(A) do:
      Increment(comparison_count)
      if Ai < pivot then append Ai to less else if Ai > pivot append Ai to greater
      end if
   end for
   return concatenate(Sort(less), pivot, Sort(greater) )

And now we are interested in the number of comparisons that will be made during the sorting of the given permutation of integers A with the provided code. So, we ask you to find the value of the variable comparison_count after such a sorting.

Input

The first line of input consists of a single integer N. The second line of input contains a permutation of N numbers.

Output

Output the number of comparisons on the first and only line of the output.

Example

Input:
5
4 3 5 1 2

Output:
11

Solution

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <time.h>
#include <limits.h>

int comparison_count;

void debug_array(int *array, int array_size) {
printf("\n");
printf("[");
int i;
for (i = 0; i < array_size; i++) {
printf("%3d", array[i]);
}
printf("]");
printf("\n");
}

unsigned long long bigrand() {
unsigned long long big_rand = 0;
big_rand = rand();
big_rand = big_rand << 32;
big_rand |= rand();
return big_rand;
}

int randint(int start_range, int end_range) {
return (bigrand() % ((end_range + 1) - start_range)) + start_range;
}

void concatenate(int *concatenated,
int concatenated_size,
int *less,
int less_size,
int pivot,
int *greater,
int greater_size) {
int concatenated_idx = 0;
int i;
for (i = 0; i < less_size; i++) {
concatenated[concatenated_idx++] = less[i];
}
concatenated[concatenated_idx++] = pivot;
for (i = 0; i < greater_size; i++) {
concatenated[concatenated_idx++] = greater[i];
}
}

int *sort(int *array, int array_size) {
if (array_size <= 1) {
return array;
}
int pivot = array[(array_size - 1) / 2];
int *less = calloc(array_size, sizeof(int));
int less_idx = 0;
int *greater = calloc(array_size, sizeof(int));
int greater_idx = 0;
memset(less, -1, array_size * sizeof(int));
memset(greater, -1, array_size * sizeof(int));
int i;
for (i = 0; i < array_size; i++) {
comparison_count++;
if(array[i] < pivot) {
less[less_idx++] = array[i];
} else if (array[i] > pivot) {
greater[greater_idx++] = array[i];
}
}

int concatenated_size = less_idx + greater_idx + 1;
int *concatenated = calloc(concatenated_size, sizeof(int));
concatenate(concatenated,
concatenated_size,
sort(less, less_idx),
less_idx,
pivot,
sort(greater, greater_idx),
greater_idx);
return concatenated;
}

float calc_elapsed_time(clock_t time_s, clock_t time_e) {
return (((float)time_e - (float)time_s) / CLOCKS_PER_SEC) * 1000;
}

int main(int argc, const char * argv[])
{

int N;
scanf("%d", &N);
int *A = calloc(N, sizeof(int));
int i;
for (i = 0; i < N; i++) {
scanf("%d", &A[i]);
}
comparison_count = 0;

int *sorted = sort(A, N);
free(sorted);


printf("%d\n", comparison_count);
return 0;
}


Chef And his Cake

Chef�s girlfriend's birthday is near, so he wants to surprise her by making a special cake for her. Chef knows that his girlfriend likes cherries on the cake, so he puts cherries on the top of the cake, but he was not satisfied. Therefore, he decided to replace some of the cherries to make a beautiful pattern. However, Chef has a lot of other work to do so he decided to ask for your help.
The cherries are of two colors red and green. Now Chef wants the cherries to be placed in such a way that each cherry of one color must be adjacent to only cherries of the other color, two cherries are adjacent if they share a side. Now Chef has asked for your help in making that pattern on the cake.
You can replace any cherry of given color with the other color. But there is a cost for each replacement: if you replace a red cherry with a green one, the cost is 5units and if you replace a green cherry with a red one, the cost is 3 units.
Help your friend Chef by making the cake special with minimum cost.

Input

  • The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
  • The first line of each test case contains two space-separated integers N and M, where N � M are the dimensions of the cake.
  • Each of the next N lines contains a string of length M.

Output

For each test case, output the minimum cost required to make the cake special.

Constraints

  • 1 = T = 100
  • 1 = N, M = 100
  • each string consists only of letters 'R' and 'G' denoting red and green cherries respectively

Example

Input:

2
4 5
RGRGR
GRGRG
RGRGR
GRGRG
2 3
RRG
GGR

Output:

8

Solution

#include <stdio.h>

int main(void) {
// your code goes here
int T;
    scanf(" %d",&T);
    while(T--){
        int x,y,cost1=0,cost2=0;
        scanf("%d %d",&x,&y);
        
        char cake[x][y];
        
        for(int i=0; i<x; i++)
            scanf(" %s",cake[i]);
            
        for(int i=0;i<x;i++)
        {
            for(int j=0;j<y;j++)
            {
                if((i+j)%2==0 &&  cake[i][j]=='G')
                    cost1 +=3;
                    
                else if((i+j)%2==0 &&  cake[i][j]=='R')
                    cost2 +=5;
                else if((i+j)%2==1 &&  cake[i][j]=='R')
                    cost1 +=5;
                else if((i+j)%2==1 &&  cake[i][j]=='G')
                    cost2 +=3;
           
            }
        }
        (cost1 < cost2)? printf("%d\n",cost1) : printf("%d\n",cost2);
    }
    return 0;
}

Trip

You are starting out on a long (really long) trip. On the way, there are N gas stations, the locations of which are given as a1,a2,�,aN. Initially you are located at the gas station at a1, and your destination is at location a_N. Your car can only store enough fuel to travel atmost M units without refilling. You can stop at any station and refill the car by any amount. Now you wish to plan your trip such that the number of intermediate stops needed to reach the destination is minimum, and also how many ways are there to plan your trip accordingly.

Input :

The first line two space seperated integers N and M. N lines follow, and the ith line has the value ai (0 <= ai <= 1000000000). The input will be such that a solution will always exist.

Output :

Output two space seperated integers : The least number of stops, and the number of ways to plan the trip which uses the least number of stops. Output this value modulo 1000000007.

Sample Input :

6 3
0
1
3
4
7
10

Sample Output :

3 2

Constraints :

2 <= N <= 1000000
1 <= M <= 1000
a_1 < a_2 < .. < a_N
Solution
#include<stdio.h>
#include<stdlib.h>
#define MOD 1000000007
#define BUF 4096
int arr[1000000];
int temp1[1000000];
int temp2[1000000];
char ibuf[BUF];
int ipt = BUF;
int readInt()
{
while (ipt < BUF && ibuf[ipt] < '0')
ipt++;
if (ipt == BUF)
{
fread(ibuf, 1, BUF, stdin);
ipt = 0;
while (ipt < BUF && ibuf[ipt] < '0')
ipt++;
}
int n = 0;
while (ipt < BUF && ibuf[ipt] >= '0')
n = (n*10)+(ibuf[ipt++]-'0');
if (ipt == BUF)
{
fread(ibuf, 1, BUF, stdin);
ipt = 0;
while (ipt < BUF && ibuf[ipt] >= '0')
n = (n*10)+(ibuf[ipt++]-'0');
}
return n;
}
int main()
{
int m,n,i,j,min,k;
long long count=0;
n=readInt();
m=readInt();
for(i=0;i<n;i++)
arr[i]=readInt();
temp2[0]=1;
j=k=count=0;
for(i=1;i<n;i++)
{
while(arr[i]-arr[j] > m)
count-=temp2[j++];
temp1[i]=temp1[j]+1;
while(temp1[k]==temp1[j])
count+=temp2[k++];
temp2[i]=count%MOD;
}
printf("%d %d\n",temp1[n-1]-1,temp2[n-1]);
return 0;
}

Saturday, April 27, 2019

Occurrence

  • Problem Description

    There was a group of elements in which there may be repeated elements. So there was a task to find occurrence of an element in one dimensional array. Write a C program for this scenario
  • Test Case 1

    Input (stdin)
    6

    10 20 30 10 50 60

    10

    Expected Output
    2
  • Test Case 2

    Input (stdin)
    3

    1 2 3

    3

    Expected Output
    1
Solutions


#include <stdio.h>
#define MAX 100
int main()
{
    int arr[MAX],n,i;
    int num,count;
    scanf("%d",&n);
    for(i=0;i< n;i++)
    {
        scanf("%d",&arr[i]);
    }
    scanf("%d",&num);
    count=0;
    for(i=0;i< n;i++)
    {
        if(arr[i]==num)
            count++;
    }
    printf("%d",count);
    return 0;
}

Pogo Stick Jump

Raju lives in a colony. On his 9th birthday, his father gift him a Pogo Stick. He
is so excited to play with pogo stick. The pogo stick
moves one unit per jump. He wanders around his house jumping with pogo sticks. He
wants to show the pogo stick to his friends and
decide to go using pogo sticks. Write a program to find number of jumps needed to
reach his friends house. Assume that Rajus house is
in the location (3,4). Input and Output Format: Input consists of two integers x,
y. The x and y corresponds to x and y coordinates of his
friends house. Output is an integer - the number of jumps he needs to reach his
friends house.

Solution

#include <stdio.h>
#include <math.h>
int main ()

int x1=3,y1=4,x2,y2;
float power,power1,power2,sqr;
scanf ("%d",&x2);
scanf ("%d",&y2);
power1=pow((x2-x1),2);
power2=pow((y2-y1),2);
power=power1+power2;
sqr=sqrt(power);
printf ("Raju needs %0.f jumps",sqr);
return 0;
}

Sample Input
5 6
Sample Output

Raju needs 3 jumps

ADDING TWO DISTANCES

  • Problem Description

    1. Create a Structure called "Distance" 

    2. Create two data members of "Distance Structure" feet(int), inch(float) 

    3. Create three structure variables as d1, d2 and sumOfDistances 4. Get two distances and add the feet and inches.

    Mandatory:

    To add the distance using the structure variables as follows

    1. sumOfDistances.feet=d1.feet+d2.feet

    2 sumOfDistances.inch=d1.inch+d2.inch

    Note: The structure variables, data members and structure name are CASE Sensitive.

    Follow the same case mentioned in the mandatory
  • CODING ARENA
  • #include <stdio.h>
    struct Distance
    {
      int feet;
      float inch;
    }d1,d2,sumOfDistances;
    int main()
    {
      scanf("%d %f\n",&d1.feet,&d1.inch);
      scanf("%d %f\n",&d2.feet,&d2.inch);
      {
        sumOfDistances.feet=d1.feet+d2.feet;
        sumOfDistances.inch=d1.inch+d2.inch;
        printf("Sum of distances=%d feet and %.2f inches",sumOfDistances.feet,sumOfDistances.inch);
      }
      return 0;
    }

        
      
      



  • Test Case 1

    Input (stdin)
    23 8.6

    34 2.4

    Expected Output
    Sum of distances=57 feet and 11.00 inches
  • Test Case 2

    Input (stdin)
    25 11.9

    34 2.5

    Expected Output
    Sum of distances=59 feet and 14.40 inches

IO 4

  • Problem Description

    Given two cities at geographic coordinates (xA,yA) and (xB,yB), and calculate the distance between from city A to city B?

    Note: Use the distance between points formula

    dist=sqrt(pow((x2-x1),2)+pow((y2-y1),2))

    Input and Output Format:

    Refer sample input and output for formatting specification.

    All float values are displayed correct to 2 decimal places.

    All text in bold corresponds to input and the rest corresponds to output.
  • CODING ARENA
  • #include <stdio.h>
    #include<math.h>
    int main()
    {
      int x1,x2,y1,y2;
      float r;
      scanf("%d%d",&x1,&y1);
      scanf("%d%d",&x2,&y2);
      r=sqrt(pow((x2-x1),2)+pow((y2-y1),2))
      printf("The distance between two points is=%.3f units",r);

    return 0;
    }
  • Test Case 1

    Input (stdin)
    12 33

    13 4

    Expected Output
    The distance between two points is=29.02 units
  • Test Case 2

    Input (stdin)
    7 6

    2 3

    Expected Output
    The distance between two points is=5.83 units

Take a count

Sheela gave a number to Jonnes and ask him to count the number of digits in a number. Sheela tries one simple logic to do that. So she decided to apply this concept in C. 
Test Case 1 
  
Input (stdin) 
3452 
  
Expected Output 
  

Test Case 2 
  
Input (stdin) 
562356 
  
Expected Output 
  


Solution

#include <stdio.h> 
int main() 

    long long num; 
    int count = 0; 
    scanf("%lld", &num); 
    while(num != 0) 
    { 
        count++; 
        num /= 10; 
    } 
    printf("%d", count); 
    return 0; 
}

Parity

Problem Description Ram and Sita playing the parity game. Two types of parity are there. One is odd parity and next is even parity. Ram will...