Maximum number of fixed points using atmost 1 swap
Given a permutation of N elements (Elements are in range 0 to N-1). A fixed point is an index at which the value is same as the index (That is, a[i]=i). You are allowed to make atmost 1 swap. Find the maximum number of fixed points that you can get.
Examples:
Input : N = 5
arr[] = {0, 1, 3, 4, 2}
Output : 3
2 and 3 can be swapped to get:
0 1 2 4 3
which has 3 fixed points.
Input : N = 5
a[] = {0, 1, 2, 4, 3}
Output : 5
Since we are allowed to make only 1 swap, the number of fixed points can be increased by atmost 2.
Let’s have an array pos which keeps the position of each element in the input array. Now, we traverse the array and have the following cases:
- If, a[i] = i. We can simply increment the count and move on.
- If, pos[i] = a[i] which means that swapping the 2 terms would make i and a[i] fixed points, hence increasing the count by 2. Keep in mind that swap can be done atmost once.
At the end of the traversal, if we haven’t made any swap, it means that our swap was not able to increase count by 2, so now if there are at least 2 elements which are not fixed points, we can make a swap to increase count by 1, i.e make one of those points a fixed point.
Below is the implementation of the above approach:
C++
// CPP program to find maximum number of // fixed points using atmost 1 swap #include <bits/stdc++.h> using namespace std; // Function to find maximum number of // fixed points using atmost 1 swap int maximumFixedPoints(int a[], int n) { int i, pos[n], count = 0, swapped = 0; // Store position of each element in // input array for (i = 0; i < n; i++) pos[a[i]] = i; for (i = 0; i < n; i++) { // If fixed point, incremenmt count if (a[i] == i) count++; // Else check if swapping increments // count by 2 else if (swapped == 0 && pos[i] == a[i]) { count += 2; swapped = 1; } } // If not swapped yet and elements remaining if (swapped == 0 && count < n - 1) count++; return count; } // Driver Code int main() { int a[] = { 0, 1, 3, 4, 2 }; int n = sizeof(a) / sizeof(a[0]); cout << maximumFixedPoints(a, n); return 0; } |
Java
// Java program to find maximum number of // fixed points using atmost 1 swap import java.io.*; class GFG { // Function to find maximum number of // fixed points using atmost 1 swap static int maximumFixedPoints(int a[], int n) { int i, count = 0, swapped = 0; int pos[] = new int[n]; // Store position of each element in // input array for (i = 0; i < n; i++) pos[a[i]] = i; for (i = 0; i < n; i++) { // If fixed point, incremenmt count if (a[i] == i) count++; // Else check if swapping increments // count by 2 else if (swapped == 0 && pos[i] == a[i]) { count += 2; swapped = 1; } } // If not swapped yet and elements remaining if (swapped == 0 && count < n - 1) count++; return count; } // Driver Code public static void main (String[] args) { int []a= { 0, 1, 3, 4, 2 }; int n = a.length; System.out.println(maximumFixedPoints(a, n)); } } // This code is contributed // by shs |
Python3
# Python3 program to find the maximum number # of fixed points using at most 1 swap # Function to find maximum number of # fixed points using atmost 1 swap def maximumFixedPoints(a, n): pos = [None] * n count, swapped = 0, 0 # Store position of each element # in input array for i in range(0, n): pos[a[i]] = i for i in range(0, n): # If fixed point, incremenmt count if a[i] == i: count += 1 # Else check if swapping increments # count by 2 elif swapped == 0 and pos[i] == a[i]: count += 2 swapped = 1 # If not swapped yet and elements remaining if swapped == 0 and count < n - 1: count += 1 return count # Driver Code if __name__ == "__main__": a = [0, 1, 3, 4, 2] n = len(a) print(maximumFixedPoints(a, n)) # This code is contributed by Rituraj Jain |
C#
// C# program to find maximum number of // fixed points using atmost 1 swap using System; class Program { // Function to find maximum number of // fixed points using atmost 1 swap static int maximumFixedPoints(int []a, int n) { int i, count = 0, swapped = 0; int []pos = new int[n]; // Store position of each element in // input array for (i = 0; i < n; i++) pos[a[i]] = i; for (i = 0; i < n; i++) { // If fixed point, incremenmt count if (a[i] == i) count++; // Else check if swapping increments // count by 2 else if (swapped == 0 && pos[i] == a[i]) { count += 2; swapped = 1; } } // If not swapped yet and elements remaining if (swapped == 0 && count < n - 1) count++; return count; } // Driver Code static void Main() { int []a= { 0, 1, 3, 4, 2 }; int n = a.Length; Console.WriteLine(maximumFixedPoints(a, n)); } } // This code is contributed // by ANKITRAI1 |
PHP
<?php // PHP program to find maximum number of // fixed points using atmost 1 swap // Function to find maximum number of // fixed points using atmost 1 swap function maximumFixedPoints($a, $n) { $i; $pos[$n]=array(); $count = 0; $swapped = 0; // Store position of each element in // input array for ($i = 0; $i < $n; $i++) $pos[$a[$i]] = $i; for ($i = 0; $i < $n; $i++) { // If fixed point, incremenmt count if ($a[$i] == $i) $count++; // Else check if swapping increments // count by 2 else if ($swapped == 0 && $pos[$i] == $a[$i]) { $count += 2; $swapped = 1; } } // If not swapped yet and elements remaining if ($swapped == 0 && $count < $n - 1) $count++; return $count; } // Driver Code $a = array (0, 1, 3, 4, 2 ); $n = sizeof($a) / sizeof($a[0]); echo maximumFixedPoints($a, $n); // This code is contributed by Sachin ?> |
3
Time Complexity: O(N)
Recommended Posts:
- Maximum sum of non-overlapping subarrays of length atmost K
- Maximum average of a subarray of size of atleast X and atmost Y
- Maximize the maximum subarray sum after removing atmost one element
- Collect maximum points in an array with k moves
- Maximum points covered after removing an Interval
- Find maximum points which can be obtained by deleting elements from array
- Steps required to visit M points in order on a circular ring of N points
- Ways to choose three points with distance between the most distant points <= L
- Find the number of points that have atleast 1 point above, below, left or right of it
- Count distinct points visited on the number line
- Longest Subarray having sum of elements atmost 'k'
- Check whether an array can be made strictly increasing by modifying atmost one element
- Find a Fixed Point in an array with duplicates allowed
- Find a Fixed Point (Value equal to index) in a given array
- Find a Fixed Point (Value equal to index) in a given array | Duplicates Allowed
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



