Question

Coins in a Line 12.4.1 The first game we consider is reported to arise in a problem that is sometimes asked during job interv

Design an O(n)-time non-losing strategy for the first player, Alice, in the coins in-a-line game. Your strategy does not have to be optimal, but it should be guaranteed to end in a tie or better for Alice.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution -:

Strategy -:

The idea is to first calculate the sum of all even place coins and sum of all odd place coins.

Then compare the both even and odd sums , Alice(First player) will make sure that if even sum is larger then bob (second player) will not be able to pick even number of coin.

Similarly Alice will make sure that if odd sum is large then bob will not be able to pick odd number of coin.

Executable Code -:

#include <iostream>
using namespace std;

void Totalworth(int arr[], int n)
{
int oddcoinSum = 0;
for (int i = 0; i < n; i += 2)
oddcoinSum += arr[i];
  
int evencoinSum = 0;
for (int i = 1; i < n; i += 2)
evencoinSum += arr[i];
  
if(evencoinSum>oddcoinSum)
{
   cout << "\nAlice worth is " << evencoinSum;
   cout << "\nBob worth is " << oddcoinSum;
   }
   else
   {
       cout << "\nAlice worth is " << oddcoinSum;
       cout << "\nBob's' worth is " << evencoinSum;
   }
}
  
int main()
{
int arr[] = {6,5,2,7,3,5} ;
int n = sizeof(arr) / sizeof(arr[0]);
Totalworth(arr, n);
  
return 0;
}

Output -:

CAUsers Shashank DesktoplUntitled2.exe Alice worth is 17 Bob worth is 11 Process exited after 0.07633 seconds with return val

Explanation and Example -:

Coins ={6,5,2,7,3,5}

even place coin sum =5+7+5 =17

odd place coin sum =6+2+3=11

even place sum is large

So Alice will choose even place coin (Coin no 6).  

Then bob have no choice to choose only odd coin (either coin 1 or coin 5) suppose he choose coin no 1

Now Alice will choose Coin no 2

Suppose bob will choose coin no 5.

Now Alice will choose coin no.4

Now bob is left with only coin no 3

So total worth of Alice is 17

total worth of bob is 11

Alice Won ...!!!

Add a comment
Know the answer?
Add Answer to:
Design an O(n)-time non-losing strategy for the first player, Alice, in the coins in-a-line game. Your...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A subtraction game Subtraction games are two-player games in which there is a pile of objects,...

    A subtraction game Subtraction games are two-player games in which there is a pile of objects, say coins. There are two players, Alice and Bob, who alternate turns subtracting 4.9. A SUBTRACTION GAME 19 from the pile some number of coins belonging to a set S (the subtraction set). Alice goes first. The first player who is unable to make a legal move loses. For example, suppose the initial pile contains 5 coins, and each player can, on his turn,...

  • second attempt. need asap please 2-4 sentences summarizing the article 4 interesting quotes from the article...

    second attempt. need asap please 2-4 sentences summarizing the article 4 interesting quotes from the article and 4 points explaining each quote In the first few years of the new millennium, at the height of the boom in the offshore call-center business, Tata Consultancy Services, the Indian technology-services giant, made the counterintuitive decision to divest its call-center operations. Why? Because although outsourced call centers were a fast-growing piece of its current business, TCS’s leadership had come to believe that they...

  • Please read the article and answer about questions. You and the Law Business and law are...

    Please read the article and answer about questions. You and the Law Business and law are inseparable. For B-Money, the two predictably merged when he was negotiat- ing a deal for his tracks. At other times, the merger is unpredictable, like when your business faces an unexpected auto accident, product recall, or government regulation change. In either type of situation, when business owners know the law, they can better protect themselves and sometimes even avoid the problems completely. This chapter...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT