top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Can any one solve this problem (a Game) in c or c++ or any other language?

+3 votes
266 views

A person has two piles of stones with him, one has n1 stones and the other has n2 stones. Fired up by boredom, he invented a game with the two piles.

  1. Before the start of the game person chooses an integer m.
  2. In the j-th move: He chooses a number xj such that 1 ≤ xj ≤ m, and removes xj stones from both the piles (this is only possible when both the piles have ≥ xj stones).
  3. The number chosen must be unique over all the moves in the game. That is, for all k < j, xj ≠ xk.
  4. The game stops when person is unable to make any more moves.
    Note: Person wants to make the moves in such a way that the sum of the number of stones remaining in the two piles is minimized.

Please help person find this.

Input
The first line of input contains an integer T denoting the number of test cases.
Each test case consists of 1 line with three integers — n1, n2 and m — separated by single spaces.

Output
For each test case, output a single line containing the minimum sum of the number of stones of two piles.

posted Dec 6, 2015 by Rajan Paswan

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes
#include<stdio.h>
int main()
 {
   int t;
   long int m;
   long long int n1,n2;
   scanf("%d",&t);
   if(t<0)
     return 1;
   while(t--)
    {

      scanf("%lld%lld%ld",&n1,&n2,&m);
       if(n1<0 || n2<0 || m<0)
         return 1;
      label:

        if(m>n1 || m>n2)
        {
          if(n2>n1)
           printf("%lld\n",n1+n2-(2*n1));
          else
           printf("%lld\n",n1+n2-(2*n2));
          continue;
        }

          if(m==0 || (n1==0 || n2==0))
            {
              printf("%lld\n",(n1+n2));
            }  
           else
            {
             n1=n1-m;
             n2=n2-m;
             m--;
            goto label;
            }
    }  
   return 0;
 }
answer Mar 26, 2016 by Shahsikant Dwivedi
Similar Questions
+2 votes

suppose we have an array of N natural numbers and asks him to solve the following queries:-
Query a:- modify the element present at index i to x.
Query b:- count the number of even numbers in range l to r inclusive.
Query c:- count the number of odd numbers in range l to r inclusive.

input:
First line of the input contains the number N. Next line contains N natural numbers.Next line contains an integer Q followed by Q queries.
a x y - modify the number at index x to y.
b x y - count the number of even numbers in range l to r inclusive.
c x y - count the number of odd numbers in range l to r inclusive.
I tried to solve using simple arrays but it isn't doing well for big constraints so I thought to use other DS with efficient algorithm so please explain appropriate algorithm.Thanks in advance.

+5 votes

Josephus Problem talks about a problem where there are people standing in a circle waiting to be executed. The counting out begins at some point in the circle and proceeds around the circle in a fixed direction. In each step, a certain number of people are skipped and the next person is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given freedom.

+3 votes

Given a 2d array, u need to sort the 2 diagonals independently.
And the elements in diagonal should remain in diagonal only.

INPUT =>
24 2 3 4 7

6 13 8 5 10

11 12 9 14 15

16 21 18 25 20

17 22 23 24 1

OUTPUT =>
1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

16 17 18 19 20

21 22 23 24 25

+3 votes

A list contains a set of numbers, one number presents once and other numbers present even no. of times. Find out the number that occurs once in the list.

+6 votes

I was trying to get maximum rectangle area for a given histogram but I used brute force approach which have O(n^2) time complexity so I want some better solution using stack so that we could reduce time complexity to O(n) or O(log n ).

...