Skip to content

Instantly share code, notes, and snippets.

@pankajgangwar
Created February 17, 2024 11:35
Show Gist options
  • Save pankajgangwar/59c9cbdd23ca6bad6c5bb2d5542fde06 to your computer and use it in GitHub Desktop.
Save pankajgangwar/59c9cbdd23ca6bad6c5bb2d5542fde06 to your computer and use it in GitHub Desktop.
C Program to find max absolute sum
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int compareIntegers(const void *a, const void *b) {
return (*(int*)a - *(int*)b);
}
int solve(int* sec_values, char* s) {
int n = strlen(s);
int aux[n];
for (int i = 0; i < strlen(s); i++) {
aux[i] = sec_values[s[i] - 'a'];
}
qsort(aux, strlen(s), sizeof(int), compareIntegers);
for(int i = 0; i < n; i++){
printf(" %d", aux[i]);
}
int sum = 0;
for (int i = 0; i < n - 1; i++) {
sum += abs(aux[i + 1] - aux[i]);
}
return sum;
}
int main() {
int sec_values[] = {1, 2, 1, 3, 1, 3, 5, 7, 1, 1, 5, 5, 8, 10, 11, 1, 23, 2, 3, 7, 8, 9, 1, 6, 5, 9};
char encrypted_message[] = "afeb";
int result = solve(sec_values, encrypted_message);
printf("%d\n", result);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment