title: Print all the possible palindromes that can be generated only moving 1 index
body: <p>I am trying to do a program which print all possible palindromes that can be generated rearrangement involves that a character changes its position with maximum 1 index, meaning only interchanges of adjacent characters are allowed.</p>
  
  <p>I tried a lot of codes but the best one I had is the next one.</p>
  
  <p>Only I could get all possible palindromes rearranged but with all changes (not only one)</p>
  
  <p><strong>CODE</strong></p>
  
  <pre><code>#include &lt;bits/stdc++.h&gt;
  
  using namespace std;
  #define M 26
  
  bool caNformPalindrome(string str, int* count_array)
  {
      //Initialize array with zeroes
      for (int i = 0; i &lt; 26; ++i)
      {
          count_array[i] = 0;
      }
      int length = str.length();
  
      /* Updating frequency according to given string */
      for (int i = 0; i &lt; length; i++)
      {
          count_array[str[i] - 'a']++;
      }
      int odd_count = 0;
      //Find odd_count
      for (int i = 0; i &lt; M; i++)
      {
          if (count_array[i] % 2 == 1)
          {
              odd_count++;
          }
      }
      //if length is odd_count then only one letter's frequency must be odd_count
      if ((length % 2 == 1 &amp;&amp; odd_count == 1 ) || (length %2 == 0 &amp;&amp; odd_count == 0))
      {
          return true;
      }
      else//if length is even no letter should have odd_count frequency
      {
          return false;
      }
  }
  //Function to reverse the string
  string reverse(string str)
  {
      string rev = str;
      reverse(rev.begin(), rev.end());
      return rev;
  }
  
  //Function to print 
  void printPalIndromes(string str)
  {
      int count_array[M];
      // checking whether letter can make palindrome or not
      if (!caNformPalindrome(str, count_array))
      {
          return;
      }
      int length = str.length();
  
      // half will contain half part of all palindromes,
      // that is why pushing half count_array of each letter
      string half = "";
      char mid;//store odd count charcter
      for (int i = 0; i &lt; M; i++)
      {
          if(count_array[i] % 2 == 1)
          {
              mid = i + 'a';
          }
          half += string(count_array[i] / 2, i + 'a');
      }
      string palindrome_string;
      do
      {
          palindrome_string = half;
          if (length % 2 == 1)
          {
              palindrome_string += mid;
          }
          palindrome_string += reverse(half);
          cout&lt;&lt;palindrome_string&lt;&lt;endl;
      }
      while (next_permutation(half.begin(), half.end()));
  }
  
  //Main function
  int main()
  {
      string str = "aabbc";
      cout&lt;&lt;"Possible Permutations for Input_string only with 1 change"&lt;&lt;str&lt;&lt;" are: "&lt;&lt;endl;
      printPalIndromes(str);
      return 0;
  }
  </code></pre>