Open In App

Puzzle 81 | 100 people in a circle with gun puzzle

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

100 people standing in a circle in an order 1 to 100. No. 1 has a sword. He kills the next person (i.e. No. 2) and gives the sword to the next (i.e. No. 3). All people do the same until only 1 survives. Which number survives at the last? 
There are 100 people starting from 1 to 100. 
  
Solution:  73rd person will survive at last 

Explanation 1  (intuitive and logical): We can observe that if the number of people standing in a circle is a power of 2, then the person starting the game will live. This is because after each round of killing, the number of people left will be reduced by 2 and no remainder would be there, thus, the next round will again start with the same person who initially started the game. And this will go on.

For the case when the number of people in the circle is not the power of two, then in the first round when the number of people alive becomes a power of 2, then the person holding the gun will win because basically, he is starting a game with people left in the power of 2. So, for 100, 36 people need to die when 64 people remain i.e. a number smaller than the given number and a power of two. 36th person to be killed will be 72nd person, thus 73rd person will have the gun when the number of people left alive is 64.

Explanation 2 (code):  
Here, we can define an array with 100 elements with values from 1 to 100. 
 

  • No.1 has a sword. He kills next person (i.e. no. 2) and gives sword to next (i.e no. 3). 
    We have taken array element as a person. 1st person kills the next. So, starting from 1, we’ll remove next element i.e. 2.
  • Then first person gives sword to next i.e. 3. That person will also kill next person and this continues. Means, in array, we need to start with 1 and remove the every other (alternate) element till 100. (all the even numbers will be removed and we’ll be left with odd numbers only in array).

Round 1: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99 
Round 2: 1, 5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 53, 57, 61, 65, 69, 73, 77, 81, 85, 89, 93, 97 
Round 3: 1, 9, 17, 25, 33, 41, 49, 57, 65, 73, 81, 89, 97 
Round 4: 9, 25, 41, 57, 73, 89 
Round 5: 9, 41, 73 
Round 6: 9, 73 
Round 7: 73 

To avoid the manual calculation done above, here the general algorithm : 
Step 1 : For a given value of N, find the “Power of 2” immediately smaller than N. Let’s call it P 
Step 2 : Subtract N from (P-1). Lets call it M, i.e, M = (P-1)- N 
Step 3 : Multiply M by 2. i.e M*2 
Step 4 : Subtract M*2 from P-1. Let’s call it ans, i.e, ans = (P-1) – (M*2) 
So, the person with number “ans” will survive till last. 

Code:

C++




#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 
    int person = 100;
 
    // Placeholder array for person
    vector<int> a(person);
 
    // Assign placeholders from 1 to N (total person)
    for (int i = 0; i < person; i++) {
        a[i] = i + 1;
    }
 
    // Will start the game from 1st person (Which is at
    // placeholder 0)
    int pos = 0;
 
    // Game will be continued till we end up with only one
    // person left
    while (a.size() > 1) {
        // Current person will shoot the person next to
        // him/her. So incrementing the position.
        pos++;
        // As person are standing in circular manner, for
        // person at last place has right neighbour at
        // placeholder 0. So we are taking modulo to make it
        // circular
        pos %= a.size();
        // Killing the person at placeholder 'pos'
        // To do that we simply remove that element
        a.erase(a.begin() + pos);
        // There is no need to increment the pos again to
        // pass the gun Because by erasing the element at
        // 'pos', now next person will be at 'pos'.
    }
 
    // Print Person that survive the game
    cout << a[0];
 
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
import java.util.Arrays;
 
class GFG {
 
    // Driver code
    public static void main (String[] args) {
        int person = 100;
     
        // Placeholder array for person
        int[] a = new int[person];
     
        // Assign placeholders from 1 to N (total person)
        for (int i = 0; i < person; i++) {
            a[i] = i + 1;
        }
     
        // Will start the game from 1st person (Which is at
        // placeholder 0)
        int pos = 0;
     
        // Game will be continued till we end up with only one
        // person left
        while (a.length > 1)
        {
           
            // Current person will shoot the person next to
            // him/her. So incrementing the position.
            pos++;
           
            // As person are standing in circular manner, for
            // person at last place has right neighbour at
            // placeholder 0. So we are taking modulo to make it
            // circular
            pos %= a.length;
           
            // Killing the person at placeholder 'pos'
            // To do that we simply remove that element
            int[] anotherArray = new int[a.length - 1];
            System.arraycopy(a, 0, anotherArray, 0, pos);
            System.arraycopy(a, pos + 1, anotherArray, pos,a.length - pos - 1);
            a = anotherArray;
           
            // There is no need to increment the pos again to
            // pass the gun Because by erasing the element at
            // 'pos', now next person will be at 'pos'.
        }
     
        // Print Person that survive the game
        System.out.println(a[0]);
    }
}
 
// This Code is contributed by ShubhamSingh10


Python3




person = 100
 
# Placeholder array for person
a = [0] * person
 
# Assign placeholders from 1 to N (total person)
for i in range(person):
    a[i] = i + 1
     
# Will start the game from 1st person (Which
# is at placeholder 0)
pos = 0
 
# Game will be continued till we end up with
# only one person left
while (len(a) > 1):
     
    # Current person will shoot the person next
    # to him/her. So incrementing the position.
    pos += 1
     
    # As person are standing in circular manner,
    # for person at last place has right neighbour
    # at placeholder 0. So we are taking modulo
    # to make it circular
    pos %= len(a)
     
    # Killing the person at placeholder 'pos'
    # To do that we simply remove that element
    del a[pos]
     
    # There is no need to increment the pos again to
    # pass the gun Because by erasing the element at
    # 'pos', now next person will be at 'pos'.
 
# Driver code
 
# PrPerson that survive the game
print(a[0])
 
# This code is contributed by ShubhamSingh10


C#




// C# program for the above approach
using System;
using System.Linq;
 
public static class GFG
{
   
    // Driver code
    static public void Main ()
    {
     
        int person = 100;
     
        // Placeholder array for person
        int[] a = new int[person];
     
        // Assign placeholders from 1 to N (total person)
        for (int i = 0; i < person; i++) {
            a[i] = i + 1;
        }
     
        // Will start the game from 1st person (Which is at
        // placeholder 0)
        int pos = 0;
     
        // Game will be continued till we end up with only one
        // person left
        while (a.Length > 1)
        {
           
            // Current person will shoot the person next to
            // him/her. So incrementing the position.
            pos++;
           
            // As person are standing in circular manner, for
            // person at last place has right neighbour at
            // placeholder 0. So we are taking modulo to make it
            // circular
            pos %= a.Length;
           
            // Killing the person at placeholder 'pos'
            // To do that we simply remove that element
            a = a.Where((source, index) =>index != pos).ToArray();
           
            // There is no need to increment the pos again to
            // pass the gun Because by erasing the element at
            // 'pos', now next person will be at 'pos'.
        }
     
        // Print Person that survive the game
        Console.Write(a[0]);
    }
}
 
// This Code is contributed by ShubhamSingh10


Javascript




<script>
 
var person = 100;
 
// Placeholder array for person
var a = new Array(person);
 
// Assign placeholders from 1 to N (total person)
for (var i = 0; i < person; i++) {
    a[i] = i + 1;
}
 
// Will start the game from 1st person (Which is at
// placeholder 0)
var pos = 0;
 
// Game will be continued till we end up with only one
// person left
while (a.length > 1) {
    // Current person will shoot the person next to
    // him/her. So incrementing the position.
    pos++;
    // As person are standing in circular manner, for
    // person at last place has right neighbour at
    // placeholder 0. So we are taking modulo to make it
    // circular
    pos = pos% (a.length);
    // Killing the person at placeholder 'pos'
    // To do that we simply remove that element
    a.splice(pos,1)
     
    // There is no need to increment the pos again to
    // pass the gun Because by erasing the element at
    // 'pos', now next person will be at 'pos'.
}
 
// Print Person that survive the game
document.write(a[0]);
 
// This code is contributed by ShubhamSingh10
 
</script>


Output

73

This puzzle is contributed by Jyoti.
 



Last Updated : 29 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads