Skip to main content

CounterCode 2015

Degree of Dirtiness 

 

Problem Statement
There are N toilets in a row indexed from 1 to N. At a time, 2 people enter the washroom. The degree of dirtiness of each toilet is 0 initially and it increases by 1 after it is used each time. The 1st person occupies the 1st toilet with the lowest degree of dirtiness moving from 1 to N. The 2nd person occupies the toilet with the lowest degree of dirtiness moving from N to 1. The next two people enter the toilet when the first two people have left. Find the index of toilet and degree of dirtiness for the Mth person.
Note In case M is odd, the last person walks into the washroom alone and occupies the least dirty toilet moving from 1 to N.

Input Format
 
The first line contains T, the number of test cases. Each test case consists of one line containing N, the number of toliets, and M, the person to enter the toilet, seperated by space.

Output Format
 
The index of the toilet used by M and its degree of dirtiness D.


Constraints
 
1T10
1N50000
1M106

Sample Input
3  
10 3  
5 8  
4 26 

Sample Output
2 0  
4 1  
4 6  

Explanation
In the second test case,
for the first two persons, positions are 1 _ _ _ 2, degree of dirtiness 0 0 0 0 0 (dirtiness is 0 since they are the first to use it)
for person 3 and 4, positions are _ 3 _ 4 _ , degree of dirtiness 1 0 0 0 1
for 5 and 6, positions are _ _ 5 _ 6, degree of dirtiness 1 1 0 1 1
for 7 and 8, positions are 7 _ _ 8 _, degree of dirtiness 1 1 1 1 2 so the answer is 4,1

 My solution

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

int main() {

    int t,n,m,c,i,min1,min2,r,pos,dirt;
 scanf("%d",&t);
 while(t--)
 {
  scanf("%d %d",&n,&m);
  int d[n];
  for(i=0;i<n;i++)
  {
   d[i]=0;
  }
  for(c=0;c<m;c=c+1)
  {
   if(c==m-1)
   {
    if(c%2==0)
   {
    min1=d[0];
    r=0;
    for(i=0;i<n;i++)
    {
     if(d[i]<min1)
     {
      min1=d[i];
      r=i;
     }
    }
    pos=r+1;
    dirt=d[r];
   }
   else if(c%2!=0)
   {
    min2=d[n-1];
    r=n-1;
    for(i=0;i<n;i++)
    {
     if(d[i]<=min2)
     {
      min2=d[i];
      r=i;
     }
    }
    pos=r+1;
    dirt=d[r];
   }
  }
  else
  {
   if(c%2==0)
   {
    min1=d[0];
    r=0;
    for(i=0;i<n;i++)
    {
     if(d[i]<min1)
     {
      min1=d[i];
      r=i;
     }
    }
    d[r]++;
   }
   else if(c%2!=0)
   {
    min2=d[n-1];
    r=n-1;
    for(i=0;i<n;i++)
    {
     if(d[i]<=min2)
     {
      min2=d[i];
      r=i;
     }
    }
    d[r]++;
   }   
  }
 }
   printf("\n%d %d",pos,dirt);
 }
 return 0;
}

Comments

Popular posts from this blog

ORACLE 9i practice solutions

Created by BCL easyConverter SDK 3 (HTML Version)

C Questions

C Questions
C Questions

Note : All the programs are tested under Turbo C/C++ compilers.
It is assumed that,
Programs run under DOS environment, The underlying machine is an x86 system, Program is compiled using Turbo C/C++ compiler.
The program output may depend on the information based on this assumptions (for example sizeof(int) == 2 may be assumed).
Predict the output or error(s) for the following:

void main()
{
int const * p=5; printf("%d",++(*p));
}
Answer:
Compiler error: Cannot modify a constant value.
Explanation:
p is a pointer to a "constant integer". But we tried to change the value of the "constant integer".
main()
{
char s[ ]="man"; int i;
for(i=0;s[ i ];i++)
printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);
}
Answer: mmmm
aaaa nnnn
Explanation

Zoho Interview | Set 1 (Advanced Programming Round)

Third Round: (Advanced Programming Round) Here they asked us to create a “Railway reservation system” and gave us 4 modules. The modules were:
    1. Booking
    2. Availability checking
    3. Cancellation
    4. Prepare chart
We were asked to create the modules for representing each data first and to continue with the implementation phase.

 My Solution :

#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<string.h>#include<iostream.h>#include<time.h>#include<iomanip.h>#include<fstream.h>char f[10]="f";char s[10]="s";int addr,ad,flag,f1,d,m,i,amt;float tamt; class login {public:char id[100];char pass[100];char*password;void getid(){ cout<<"Enter your id:";gets(id); password=getpass("Enter the password:");strcpy(pass,password);}void displayid(){ cout<<"Id:";puts(id); cout<<"Password:";puts(pass);}}; class detail {public:in…