C - Bigger Is Greater Hackerrank Solution
// Step 3: Swap pivot with that character swap(&str[i], &str[j]);
// Step 2: Find the smallest character on right of pivot that is greater than str[i] for (j = n - 1; j > i; j--) if (str[j] > str[i]) break;
while (t--) char str[101]; scanf("%s", str); biggerIsGreater(str); bigger is greater hackerrank solution c
void reverse(char *str, int start, int end) while (start < end) swap(&str[start], &str[end]); start++; end--;
// Step 1: Find the pivot for (i = n - 2; i >= 0; i--) if (str[i] < str[i + 1]) break; // Step 3: Swap pivot with that character
int main() int t; scanf("%d", &t);
void biggerIsGreater(char *str) int n = strlen(str); int i, j; // Step 4: Reverse the suffix after pivot
if (i < 0) printf("no answer\n"); return;
Here’s a structured report on solving the HackerRank problem using C . Problem Statement Given a word (string of lowercase English letters), find the lexicographically smallest greater permutation of its characters. If no such permutation exists (i.e., the string is already the largest possible), return "no answer" .
// Step 4: Reverse the suffix after pivot reverse(str, i + 1, n - 1);
printf("%s\n", str);