1.
Strong Password
#include <stdio.h>
#include <string.h>
// Function to determine the minimum number of characters to add
int minimumNumber(int n, char *password) {
int required_chars = 0;
int has_digit = 0, has_lower = 0, has_upper = 0, has_special = 0;
const char *special_characters = "!@#$%^&*()-+";
// Check the existing characters in the password
for (int i = 0; i < n; i++) {
if (password[i] >= '0' && password[i] <= '9') has_digit = 1;
else if (password[i] >= 'a' && password[i] <= 'z') has_lower = 1;
else if (password[i] >= 'A' && password[i] <= 'Z') has_upper = 1;
else if (strchr(special_characters, password[i])) has_special = 1;
}
// Count the missing types of characters
if (!has_digit) required_chars++;
if (!has_lower) required_chars++;
if (!has_upper) required_chars++;
if (!has_special) required_chars++;
// Ensure the password length is at least 6 characters
if (n + required_chars < 6) {
required_chars += (6 - (n + required_chars));
}
return required_chars;
}
int main() {
int n;
char password[101];
// Input the length of the password and the password itself
scanf("%d", &n);
scanf("%s", password);
// Calculate and print the minimum number of characters to add
int result = minimumNumber(n, password);
printf("%d\n", result);
return 0;
}
2. Running time of the algorithm
#include <stdio.h>
// Function to perform Insertion Sort and count the number of shifts
int runningTime(int arr[], int n) {
int shifts = 0;
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
// Move elements of arr[0..i-1] that are greater than key
//to one position ahead of their current position
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
shifts++;
}
arr[j + 1] = key;
}
return shifts;
}
int main() {
int n;
// Read the number of elements
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: "); // Read the array elements
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int result = runningTime(arr, n); // Get the number of shifts and print it
printf("Number of shifts: %d\n", result);
return 0;
}
3. The Power Sum
#include <stdio.h>
#include <math.h>
// Function to recursively find the power sums
int powerSumHelper(int X, int N, int num) {
int power = pow(num, N);
if (power > X) {
return 0;
} else if (power == X) {
return 1;
} else {
return powerSumHelper(X - power, N, num + 1) + powerSumHelper(X, N, num +
1);
}
}
// Main function to find the number of ways to express X as sum of N-th powers of
unique natural numbers
int powerSum(int X, int N) {
return powerSumHelper(X, N, 1);
}
int main() {
int X, N;
printf("Enter X: ");// Input the values of X and N
scanf("%d", &X);
printf("Enter N: ");
scanf("%d", &N);
int result = powerSum(X, N); // Calculate and print the number of combinations
printf("%d\n", result);
return 0;
}