Given a string consisting of the first N distinct alphabets, the task is to sort the string by using at most N/2 moves. Each move involves the following:
- Select any 3 distinct indices.
- Perform a cyclic shift on the alphabets at these indices.
If it is possible to sort the strings, print the count of required moves. Otherwise, print “Not Possible”.
Input: str = “cbda”
Selecting the indices 0, 2 and 3 and performing a single circular shift among them, the given string “cbda” is converted to “abcd”.
Input: str = “cba”
Output: Not Possible
In order to solve the problem, follow the steps below:
- Store the integers denoting correct of characters of the string in a vector .
- Place those elements correctly which can all occupy correct indices in a single cycle.
- Traverse an element of the vector
- If the element is not at its sorted index position, check if two or more numbers can be placed at correct indices in one cycle. If the condition is satisfied, perform the cycle otherwise check if there is a distinct index that does not hold the correct element available. If the condition is satisfied, select this index as the third index of the cycle and perform the cycle. If neither of the above conditions are satisfied, sorting would be impossible. Hence, break out of the loop and print “Not Possible”.
- Once a cyclic shift is performed, store the indices involved in the shift.
- If the element is at its sorted position, move to the next index.
- Repeat the above two steps for all vector elements.
- After the traversal is completed, if the entire array is in sorted order, print the shifts required. Else print “Not Possible”.
Below is the implementation of the above approach:
- Sort permutation of N natural numbers using triple cyclic right swaps
- Count number of ways to convert string S to T by performing K cyclic shifts
- Check if a decreasing Array can be sorted using Triple cyclic shift
- Sort decreasing permutation of N using triple swaps
- Check if a string can be formed from another string by at most X circular clockwise shifts
- Number of Counterclockwise shifts to make a string palindrome
- Count of cyclic permutations having XOR with other binary string as 0
- Minimum moves to reach from i to j in a cyclic string
- Maximize count of corresponding same elements in given permutations using cyclic rotations
- Comparison among Bubble Sort, Selection Sort and Insertion Sort
- Sort an array of strings lexicographically based on prefix
- Count all indices of cyclic regular parenthesis
- Find initial sequence that produces a given Array by cyclic increments upto index P
- Lexicographically smallest string whose hamming distance from given string is exactly K
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Lexicographically smallest permutation of a string that contains all substrings of another string
- Lexicographically next greater string using same character set
- Lexicographically first palindromic string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Improved By : costheta_z