> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/strings-arrays-and-2-pointers/winning-strategy.md).

# Winning strategy

Our college team is going to the sports fest to play a football match with our coach. There are n players in our team, numbered from 1 to n.

The coach will know the position of another team hence create a winning strategy. He creates the position of every player in a specific order so that we will win and then he starts swapping two players at a time to form the positions.

He swaps payers in such a way that it can't be understood by another team:

1\. Any player can swap with the player directly at front him

2\. One player can swap at most with two other players

If the specific order is formed then our team will win otherwise we will lose

**Input Format**

```
The First line contains numbers of players in team: n
The second line contains n space separated integers denoting the specific position of players: i-th integer denotes the position of Ai player in winning strategy
```

**Output Format**

```
If our team wins print "YES"(without quotes) and in next line print the minimum numbers of swapping required to form this specific order otherwise print "NO"(without quotes) 
```

**Constraints**

```
1 =< n <= 10^5 
1 <= Ai <= n
```

**Sample Input1:**

```
5
2 1 5 3 4
```

**Sample Output1:**

```
YES
3
```

**Sample Input2:**

```
5
2 5 1 3 4
```

**Sample Output3:**

```
NO   
```

**Explaination**

```
In the First Sample case, 
    Initial state: 1 2 3 4 5
    Three moves required to form this order:
    1 2 3 4 5 -> 1 2 3 5 4 -> 1 2 5 3 4 -> 2 1 5 3 4
In the second case, no way to form this specific order
```

```java
class Main {
    public static void isPossible(int[] arr, int n) {
        // Original array we are considering -> 1,2,3...n
        int swaps = 0;
        // Trying to convert given array into -> 1,2,3...n
        for (int i = n - 1; i >= 0; i--) {
            // If this guy is not in correct position
            if (arr[i] != (i + 1)) {
                // Maybe this is the correct position for the guy in front
                if (((i - 1) >= 0) && arr[i - 1] == (i + 1)) {
                    swaps++;
                    int temp = arr[i];
                    arr[i] = arr[i - 1];
                    arr[i - 1] = temp;
                }
                // Maybe this is the position of the guy 2 position ahead
                 else if (((i - 2) >= 0) && arr[i - 2] == (i + 1)) {
                    swaps += 2;
                    arr[i - 2] = arr[i - 1];
                    arr[i - 1] = arr[i];
                    arr[i] = i + 1;
                } 
                // We can do atmost 2 swaps per person
                else {
                    swaps = -1;
                    break;
                }
            }
        }
        if (swaps == -1)
            System.out.println("NO");
        else {
            System.out.println("YES");
            System.out.println(swaps);
        }
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/strings-arrays-and-2-pointers/winning-strategy.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
