Skip to content
Related Articles

Related Articles

Improve Article

Lucky alive person in a circle | Code Solution to sword puzzle

  • Difficulty Level : Easy
  • Last Updated : 24 Jun, 2021
Geek Week

Given n people standing in a circle where 1st is having sword, find the luckiest person in the circle, if from 1st soldier who is having a sword each have to kill the next soldier and handover the sword to next soldier, in turn the soldier will kill the adjacent soldier and handover the sword to next soldier such that one soldier remain in this war who is not killed by anyone.
Prerequisite : Puzzle 81 | 100 people in a circle with gun puzzle

Examples : 

Input : 5 
Output : 3 
Explanation : 
N = 5 
Soldier 1 2 3 4 5 (5 soldiers) 
In first go 1 3 5 (remains) as 2 and 4 killed by 1 and 3. 
In second go 3 as 5 killed 1 and 3rd kill 5 soldier 3 remains alive.

Input : 100 
Output : 73 
Explanation : 
N = 10 
Soldiers 1 2 3 4 5 6 7 8 9 10 (10 soldiers) 
In first 1 3 5 7 9 as 2 4 6 8 10 were killed by 1 3 5 7 and 9. 
In second 1 5 9 as 9 kill 1 and in turn 5 kill 9th soldier. 
In third 5 5th soldiers remain alive

Approach : The idea is to use circular linked list. A circular linked list is made based on number of soldier N. As rule state you have to kill your adjacent soldier and handover the sword to the next soldier who in turn kill his adjacent soldier and handover sword to the next soldier. So in circular linked list the adjacent soldier are killed and the remaining soldier fights against each other in a circular way and a single soldier survive who is not killed by anyone. 



C++




// C++ code to find the luckiest person
#include <bits/stdc++.h>
using namespace std;
 
// Node structure
struct Node {
    int data;
    struct Node* next;
};
 
Node *newNode(int data)
{
   Node *node = new Node;
   node->data = data;
   node->next = NULL;
   return node;
}
 
// Function to find the luckiest person
int alivesol(int Num)
{
    if (Num == 1)
        return 1;
 
    // Create a single node circular
    // linked list.
    Node *last = newNode(1);
    last->next = last;
     
    for (int i = 2; i <= Num; i++) {
        Node *temp = newNode(i);
        temp->next = last->next;       
        last->next = temp;
        last = temp;    
    }
 
    // Starting from first soldier.
    Node *curr = last->next;
 
    // condition for evaluating the existence
    // of single soldier who is not killed.
    Node *temp;
    while (curr->next != curr) {
        temp = curr;
        curr = curr->next;
        temp->next = curr->next;
 
        // deleting soldier from the circular
        // list who is killed in the fight.
        delete curr;
        temp = temp->next;
        curr = temp;
    }
 
    // Returning the Luckiest soldier who
    // remains alive.
    int res = temp->data;
    delete temp;
     
    return res;
}
 
// Driver code
int main()
{
    int N = 100;
    cout << alivesol(N) << endl;
    return 0;
}

Java




// Java code to find the luckiest person
class GFG
{
     
// Node structure
static class Node
{
    int data;
    Node next;
};
 
static Node newNode(int data)
{
    Node node = new Node();
    node.data = data;
    node.next = null;
    return node;
}
 
// Function to find the luckiest person
static int alivesol(int Num)
{
    if (Num == 1)
        return 1;
 
    // Create a single node circular
    // linked list.
    Node last = newNode(1);
    last.next = last;
     
    for (int i = 2; i <= Num; i++)
    {
        Node temp = newNode(i);
        temp.next = last.next;    
        last.next = temp;
        last = temp;    
    }
 
    // Starting from first soldier.
    Node curr = last.next;
 
    // condition for evaluating the existence
    // of single soldier who is not killed.
    Node temp = new Node();
    while (curr.next != curr)
    {
        temp = curr;
        curr = curr.next;
        temp.next = curr.next;
 
        // deleting soldier from the circular
        // list who is killed in the fight.
        temp = temp.next;
        curr = temp;
    }
 
    // Returning the Luckiest soldier who
    // remains alive.
    int res = temp.data;
     
    return res;
}
 
// Driver code
public static void main(String args[])
{
    int N = 100;
    System.out.println( alivesol(N) );
}
}
 
// This code is contributed by Arnab Kundu

Python3




# Python3 code to find the luckiest person
  
# Node structure
class Node:
     
    def __init__(self, data):
        self.data = data
        self.next = None
     
def newNode(data): 
    node = Node(data)
    return node
  
# Function to find the luckiest person
def alivesol( Num):
    if (Num == 1):
        return 1;
  
    # Create a single node circular
    # linked list.
    last = newNode(1);
    last.next = last;
     
    for i in range(2, Num + 1):       
        temp = newNode(i);
        temp.next = last.next;       
        last.next = temp;
        last = temp;    
      
    # Starting from first soldier.
    curr = last.next;
  
    # condition for evaluating the existence
    # of single soldier who is not killed.
    temp = None
     
    while (curr.next != curr):       
        temp = curr;
        curr = curr.next;
        temp.next = curr.next;
  
        # deleting soldier from the circular
        # list who is killed in the fight.
        del curr;
        temp = temp.next;
        curr = temp;
  
    # Returning the Luckiest soldier who
    # remains alive.
    res = temp.data;
    del temp;    
    return res;
  
# Driver code
if __name__=='__main__':
     
    N = 100;
    print(alivesol(N))
    
  # This code is contributed by rutvik_56

C#




// C# code to find the luckiest person
using System;
 
class GFG
{
     
// Node structure
public class Node
{
    public int data;
    public Node next;
};
 
static Node newNode(int data)
{
    Node node = new Node();
    node.data = data;
    node.next = null;
    return node;
}
 
// Function to find the luckiest person
static int alivesol(int Num)
{
    if (Num == 1)
        return 1;
 
    // Create a single node circular
    // linked list.
    Node last = newNode(1);
    last.next = last;
     
    for (int i = 2; i <= Num; i++)
    {
        Node tem = newNode(i);
        tem.next = last.next;
        last.next = tem;
        last = tem;
    }
 
    // Starting from first soldier.
    Node curr = last.next;
 
    // condition for evaluating the existence
    // of single soldier who is not killed.
    Node tem1 = new Node();
    while (curr.next != curr)
    {
        tem1 = curr;
        curr = curr.next;
        tem1.next = curr.next;
 
        // deleting soldier from the circular
        // list who is killed in the fight.
        tem1 = tem1.next;
        curr = tem1;
    }
 
    // Returning the Luckiest soldier who
    // remains alive.
    int res = tem1.data;
     
    return res;
}
 
// Driver code
public static void Main(String []args)
{
    int N = 100;
    Console.WriteLine( alivesol(N) );
}
}
 
// This code is contributed by Arnab Kundu

Javascript




<script>
 
// JavaScript code to find the luckiest person
 
// Node structure
class Node
{
    constructor()
    {
        this.data = 0;
        this.next = null;
    }
}
 
function newNode(data)
{
    var node = new Node();
    node.data = data;
    node.next = null;
    return node;
}
 
// Function to find the luckiest person
function alivesol(Num)
{
    if (Num == 1)
        return 1;
     
    // Create a single node circular
    // linked list.
    var last = newNode(1);
    last.next = last;
     
    for(var i = 2; i <= Num; i++)
    {
        var tem = newNode(i);
        tem.next = last.next;
        last.next = tem;
        last = tem;
    }
     
    // Starting from first soldier.
    var curr = last.next;
     
    // Condition for evaluating the existence
    // of single soldier who is not killed.
    var tem1 = new Node();
    while (curr.next != curr)
    {
        tem1 = curr;
        curr = curr.next;
        tem1.next = curr.next;
         
        // Deleting soldier from the circular
        // list who is killed in the fight.
        tem1 = tem1.next;
        curr = tem1;
    }
     
    // Returning the Luckiest soldier who
    // remains alive.
    var res = tem1.data;
     
    return res;
}
 
// Driver code
var N = 100;
document.write(alivesol(N) + "<br>");
 
// This code is contributed by rdtank
 
</script>
Output: 
73

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :