# Print the arranged positions of characters to make palindrome

You are given a string s(only lowercase alphabets) with length n. Print the position of every character of the String it must acquire so that it will form a palindromic string.

Examples :

Input: c b b a aOutput: 3 1 5 2 4 To make string palindrome 'c' must be at position 3, 'b' at 1 and 5, 'a' at 2 and 4.Input: a b cOutput: Not Possible Any permutation of string cannot form palindrome .

The idea is to create an array of vectors (or dynamic size array) which stores all positions of every character. After storing positions, we check if count of odd characters is more than one. If yes, we return “Not Possible”. Otherwise, we first print first half positions from the array, then one position of odd character (if it is present) and finally second half positions.

## C++

`// CPP program to print original ` `// positions of characters in a ` `// string after rearranging and ` `// forming a palindrome ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Maximum number of characters ` `const` `int` `MAX = 256; ` ` ` `void` `printPalindromePos(string &str) ` `{ ` ` ` `// Insert all positions of every ` ` ` `// character in the given string. ` ` ` `vector<` `int` `> pos[MAX]; ` ` ` `int` `n = str.length(); ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `pos[str[i]].push_back(i+1); ` ` ` ` ` `/* find the number of odd elements. ` ` ` `Takes O(n) */` ` ` `int` `oddCount = 0; ` ` ` `char` `oddChar; ` ` ` `for` `(` `int` `i=0; i<MAX; i++) { ` ` ` `if` `(pos[i].size() % 2 != 0) { ` ` ` `oddCount++; ` ` ` `oddChar = i; ` ` ` `} ` ` ` `} ` ` ` ` ` `/* A palindrome cannot contain more than 1 ` ` ` `odd characters */` ` ` `if` `(oddCount > 1) ` ` ` `cout << ` `"NO PALINDROME"` `; ` ` ` ` ` `/* Print positions in first half ` ` ` `of palindrome */` ` ` `for` `(` `int` `i=0; i<MAX; i++) ` ` ` `{ ` ` ` `int` `mid = pos[i].size()/2; ` ` ` `for` `(` `int` `j=0; j<mid; j++) ` ` ` `cout << pos[i][j] << ` `" "` `; ` ` ` `} ` ` ` ` ` `// Consider one instance odd character ` ` ` `if` `(oddCount > 0) ` ` ` `{ ` ` ` `int` `last = pos[oddChar].size() - 1; ` ` ` `cout << pos[oddChar][last] << ` `" "` `; ` ` ` `pos[oddChar].pop_back(); ` ` ` `} ` ` ` ` ` `/* Print positions in second half ` ` ` `of palindrome */` ` ` `for` `(` `int` `i=MAX-1; i>=0; i--) ` ` ` `{ ` ` ` `int` `count = pos[i].size(); ` ` ` `for` `(` `int` `j=count/2; j<count; j++) ` ` ` `cout << pos[i][j] << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string s = ` `"geeksgk"` `; ` ` ` `printPalindromePos(s); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2 1 4 5 7 6 3

**Time Complexity** : O ( n )

## Recommended Posts:

- Minimum number of characters to be replaced to make a given string Palindrome
- Make lexicographically smallest palindrome by substituting missing characters
- Minimum characters to be added at front to make string palindrome
- Number of positions where a letter can be inserted such that a string becomes palindrome
- Replace minimal number of characters to make all characters pair wise distinct
- Min flips of continuous characters to make all characters same in a string
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Next word that does not contain a palindrome and has characters from first k
- Rearrange characters to form palindrome if possible
- Minimum removal to make palindrome permutation
- Remove a character from a string to make it a palindrome
- Make largest palindrome by changing at most K-digits
- Make a lexicographically smallest palindrome with minimal changes
- Minimum number of deletions to make a string palindrome
- Number of Counterclockwise shifts to make a string palindrome

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.