- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Code Solution to sword puzzle

We will discuss two approaches to solve the sword puzzle. In the first approach, we will use circular linked list, while the second approach is based on general intuition. In this article, we will discuss what is a sword puzzle problem and how we can solve a sword puzzle problem.

## Problem Statement

We have an arrangement of n people in a circle in which, first person is carrying a sword. The first person kills the second person and hands over the sword to the next alive person in the circle. Now the next person carrying the sword kills the next person and handovers the sword to the next alive person. This process continues till only a single person is left. We call the only alive person as the luckiest person alive. Let us now consider this situation with an example.

### Example

N=10

We have 10 people in a circular arrangement Person 1, Person 2 …, Person 10, Person 1.

At first, Person 1 kills Person 2 and hands over the sword to Person 3.

So after 1 iteration from Person 1 to Person 10, we have a list of alive people as {Person 1, person 3, person 5, person 7, person 9}

And Person 1 is having the sword, now again in next iteration, we have a list of people alive as: {Person 1, Person 5, Person 9} and Person 9 is having the sword.

In next iteration, we have people alive as Person 5

Hence, we can say that the luckiest person was Person 5 who was alive till last.

## Approach 1: Using Circular Linked List

We are first solving the puzzle using a circular linked list. For this, we will first create a circular linked list having n different nodes representing n different soldiers and then will start iterating the linked list. According to the rule, we have to kill an adjacent soldier and handover the sword to the next alive person and repeat this until we have only a single person left for this, we will represent the killed soldier by deleting the adjacent linked list and the linked list left in the last represents the alive soldier.

### Example

Below is a C++ program of the sword puzzle. This code is implemented using circular linked list.

#include <bits/stdc++.h> using namespace std; // Defining the linked list as soldier struct Soldier { int data; struct Soldier* next; }; Soldier *newSoldier(int data){ Soldier *soldier = new Soldier; soldier->data = data; soldier->next = NULL; return soldier; } // This function calculates the luckiest person alive int Luckiest (int N){ if (N == 1) return 1; Soldier *last = newSoldier(1); last->next = last; for (int iterator = 2; iterator <= N; iterator++) { Soldier *dummy = newSoldier(iterator); dummy->next = last->next; last->next = dummy; last = dummy; } Soldier *current = last->next; Soldier *dummy; while (current->next != current) { dummy = current; current = current->next; dummy->next = current->next; delete current; dummy = dummy->next; current = dummy; } int result = dummy->data; delete dummy; return result; } int main(){ int N = 100; cout << "The luckiest person alive at last is the person numbered as " << Luckiest(N)<< endl; return 0; }

### Output

On compiling the above C++ program, it will produce the following output −

The luckiest person alive at last is the person numbered as 73

As we are traversing a loop of n elements, time complexity is O(n).

**Space complexity** − As we are using extra space for linked list, space complexity is O(n)

## Approach 2

In this approach, we will deduct a method to calculate the luckiest element by intuition. If the number of soldiers can be expressed as a power of 2, then we can say that the first soldier who started the killing process will be left alive at last. As we know that after every iteration, the number of soldiers decreases by 2 without any remainder. Hence for starting every iteration, we will have the first soldier carrying the sword automatically and this continues but the first person is never killed.

If we have a number of soldiers which is not a power of 2, then in the first iteration when the number of soldiers becomes a power of two, at that time the soldier having the sword will win. Intuition behind this is: When the number of soldiers becomes a power of 2, we can assume it to be a new puzzle with soldiers as a power of two and the soldier having the sword as the first soldier. We will first deduct a method to implement this approach.

**Step 1**− Find the largest number (which can be represented as a power of 2) smaller than the given number. Let it be m.**Step 2**− Subtract m from the given number “n”.**Step 3**− Now, multiply m with 2 to know the number of people killed.**Step 4**− At last, we will be having sword in (2*m+1)th soldier's hand hence 2m+1 soldier will be alive at last.

### Example

In the below example, we implement the approach discussed above

#include <bits/stdc++.h> using namespace std; // Function to calculate largest number (which can be represented as a power of 2) smaller than the given number int Power_of_two(int number){ int result = 0; for (int iterator = number; iterator >= 1; iterator--) { if ((iterator & (iterator - 1)) == 0){ result = iterator; break; } } return result; } // main code goes here int main(){ int number=100; int m= Power_of_two(number); int p; p=number-m; cout<< " The luckiest person alive is person numbered as " <<(2*(p)) +1; return 0; }

### Output

On compilation, the above C++ program will produce the following output −

The luckiest person alive at last is the person numbered as 73

**Time Complexity** − Time complexity of this approach is O(n)

**Space Complexity** − As we are only using constant space to store od variables, apace complexity will be O(1).

In this article, we have discussed two approaches to solve the sword puzzle problem. In first approach, we used a circular linked list and kept deleting each node that dies in the process and the last element left is the luckiest soldier alive. In our second approach, we created an intuitive formula to find our solution.