Skip to content

Instantly share code, notes, and snippets.

View flechnical's full-sized avatar
🤡

flechnical

🤡
View GitHub Profile
@flechnical
flechnical / next_permutation.c
Last active November 30, 2023 15:18
Next permutation in C - CodeSignal: stringsRearrangement
/*
* Solution to CodeSignal arcade level "stringsRearrangement" (https://app.codesignal.com/arcade/intro/level-7/PTWhv2oWqd6p4AHB9) using a custom next-permutation-function without recursion.
* It switches the values in an integer-array, so I can use the values in this array as my index while looping through the strings-array (it simulates reordering the strings themselves).
*
* Permutation logic:
* 1) find index of last increasing value in array of ints and save position before in variable i | 0, 2, 4, 3, 1 -> index of last increasing value = [2]:4 -> i = [1]:2
* 2) replace value at i with the nearest increasing number after it | [1]:2 <-> [3]:3 -> 0, 3, ...
* 3) at the same time take remaining numbers after i, sort them and fill the remaining spots with them in ascending order | -> 0, 3, 1, 2, 4
*/