#include <iostream>
using namespace std;

    void ProcessThePermutation(int *arr, int arrLen)
    {
        while(--arrLen >= 0)
            cout << *arr++;
        cout << endl;
    }

    void ProcessAllPermutations(int arr[], int arrLen, int permLen)
    {
        if(permLen == 1)
            ProcessThePermutation(arr, arrLen); // print the permutation
        else
        {
            int lastpos = permLen - 1;          // last item position for swaps

            for(int pos = lastpos; pos >= 0; pos--)     // pos of item to swap with the last
            {
                swap(arr[pos], arr[lastpos]);           // put the chosen item at the end
                ProcessAllPermutations(arr, arrLen, permLen - 1);
                swap(arr[pos], arr[lastpos]);           // put the chosen item back at pos
            }
        }
    }
    
int main() {
	// your code goes here
	int arr[] = {2, 4, 7, 8};
	ProcessAllPermutations(arr, 4, 4);
	return 0;
}