CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » Icoder 2010 » A Numbers Game

A Numbers Game

Problem code: ICODER1

  • All Submissions

All submissions for this problem are available.

Two kids John and Matthew are playing a game involving a grid of numbers where John must guess the number chosen by Matthew. The numbers are arranged in a grid. Now, inorder to predict the correct number, John asks Matthew to tell him the column number of the grid where his number lies. John then, keeping the same grid size, starts re-arranging the grid row-wise and keeps repeating the process of asking and arranging till he gets to the number. For example:

If the initial grid of numbers was:

9 1 5
2 3 6
7 17 10
13 20 4
8 11 15
18 21 12
14 16 19

Assuming that Matthew chose '15', which corresponds to the third column, implying that the number could be in the set { 5, 6, 10, 4, 15, 12, 19 }. To narrow down his result to one number, John arranges the numbers row-wise:

9 2 7
13 8 18
14 1 3
17 20 11
21 16 5
6 10 4
15 12 19

He again asks Matthew about the column his number corresponds to, i.e. the first column this time, this allows John to narrow down his result further to the set { 6, 15 }. He continues this column asking and row-wise arranging till he gets to the number.

Given rows and columns, what must be the number of column-asking that would assure that John is able to guess the number assuming that the initial position of the number was not known. Incase an answer is not possible print -1.

Notes
- You must consider the worst position of the number i.e. the position which would require maximum number of steps.

Constraints
- Consider that the grid consists of all distinct numbers.

Input

1. Number of test cases = 'T' ( 1 <= T <= 1000 ). T lines follow, one line for each test case.
2. Each line( representing the size of the grid ) would contain rows = 'R' followed by columns = 'C' ( 1 <= R <= 105, 1 <= C <= 105 ).

Output

1. Return the column-asking required, in order to get to the number, on a new line.

Example

Input:
2

7 3
8 10

Output:
3
2

Author: csirubix
Date Added: 20-03-2010
Time Limit: 1 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

Could not find any info about

cybersharp @ 21 Mar 2010 02:42 PM

Could not find any info about this contest on google.

Can anybody tell??

I was just testing the

jeetnihalani @ 21 Mar 2010 02:43 PM

I was just testing the problems, my submission doesn't count. You can post your queries here...

Please tell the details

cybersharp @ 21 Mar 2010 02:48 PM

Please tell the details about this contest....

CSI-Mumbai Chapter event IT

goks @ 21 Mar 2010 02:51 PM

CSI-Mumbai Chapter event

IT is being organised by Thadomal Shahani Engineering college

It is a part of the International Student Convention iDeate '10

where to register for this

vish @ 21 Mar 2010 03:01 PM

where to register for this contest??

you can directly take part,

jeetnihalani @ 21 Mar 2010 03:02 PM

you can directly take part, no need to register

no need...if u win...we will

goks @ 21 Mar 2010 03:03 PM

no need...if u win...we will take ur details from here itself

its hving any cash prize??

vish @ 21 Mar 2010 03:04 PM

its hving any cash prize??

yes for first and second

goks @ 21 Mar 2010 03:06 PM

yes for first and second place...cant reveal now

The contest has been extended

jeetnihalani @ 21 Mar 2010 03:07 PM

The contest has been extended up to 6 p.m.

grid has to be dynamically

chirag04 @ 21 Mar 2010 03:37 PM

grid has to be dynamically created or static as per my wish in any order?

Well... based on rows and

jeetnihalani @ 21 Mar 2010 03:40 PM

Well... based on rows and columns you have to find the column asking. Its quite clear, please read carefully.

are m asking k the nos in the

chirag04 @ 21 Mar 2010 03:51 PM

are m asking k the nos in the grid..how do i generate them..or do i take them from user?.

is there a trick to get to the solution or ll have to code:P

I know what you are asking...

jeetnihalani @ 21 Mar 2010 03:53 PM

I know what you are asking... its obvious that if there was something extra required to be done I would have mentioned it!

There may be a case where

jeetnihalani @ 21 Mar 2010 04:01 PM

There may be a case where there wont be any column asking required !

too tough :(

sanketmhatre1989 @ 21 Mar 2010 04:03 PM

too tough :(

hey may be you should try

jeetnihalani @ 21 Mar 2010 04:08 PM

hey may be you should try more, read the problem carefully and break it into some cases. You may get it eventually...

beyond the given input what

rebeck @ 21 Mar 2010 04:32 PM

beyond the given input what other test cases are used?

The contest has been extended

jeetnihalani @ 21 Mar 2010 04:32 PM

The contest has been extended up to 8 p.m.

Sorry I didn't understand you

jeetnihalani @ 21 Mar 2010 04:35 PM

Sorry I didn't understand you question

your*

jeetnihalani @ 21 Mar 2010 04:35 PM

your*

bye bye

sanketmhatre1989 @ 21 Mar 2010 04:41 PM

bye bye

is there bound checking?

rebeck @ 21 Mar 2010 04:47 PM

is there bound checking?

why am i getting wrong answer

rebeck @ 21 Mar 2010 04:49 PM

why am i getting wrong answer when i have right answer?

ur program must be failing

goks @ 21 Mar 2010 04:50 PM

ur program must be failing for some test case

tell me the test case rather

rebeck @ 21 Mar 2010 04:58 PM

tell me the test case rather than the 2 given?

Input: 19 4   Output: 4

jeetnihalani @ 21 Mar 2010 05:05 PM

Input:

19 4

 

Output:

4

can the admin tell me for

rebeck @ 21 Mar 2010 07:24 PM

can the admin tell me for which cases the o/p is failing for my recent submisiion?

I am really sorry, even we

jeetnihalani @ 21 Mar 2010 07:28 PM

I am really sorry, even we cant see your solutions at the moment. I may help you after the contest... Let me know your email address for that. But as of now, try to break down your cases and read the comments of this section as well...

ok for 20 10 ans is 10 or 11?

rebeck @ 21 Mar 2010 07:41 PM

ok for 20 10 ans is 10 or 11? just tell me this?

Sorry I can't tell you the

jeetnihalani @ 21 Mar 2010 07:49 PM

Sorry I can't tell you the answer. I don't think I should be telling you this also, but both are incorrect...

Hi I dont have any kids...can

abhishek chandiramani @ 21 Mar 2010 10:24 PM

Hi I dont have any kids...can I still take part

I think john and mathew are

abhishek chandiramani @ 21 Mar 2010 10:24 PM

I think john and mathew are more interested in snakes and ladders rathar than this no. game....can I write a program for that......

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold
CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com