├── C ├── 01-Hello World!_ in C │ ├── Hello-World!-in-C.c │ └── README.md ├── 02-Playing With Characters │ ├── Playing-With-Characters.c │ └── README.md ├── 03-Sum and Difference of Two Numbers │ ├── README.md │ └── Sum-and-Difference-of-Two-Numbers.c ├── 04-Functions in C │ ├── Functions-in-C.c │ └── README.md ├── 05-Pointers in C │ ├── Pointers-in-C.c │ └── README.md ├── 06-Conditional Statements in C │ ├── Conditional-Statements-in-C.c │ └── README.md ├── 07-For Loop in C │ ├── For-Loop-in-C.c │ └── README.md ├── 08-Sum of Digits of a Five Digit Number │ ├── README.md │ └── Sum-of-Digits-of-a-Five-Digit-Number.c ├── 09-Bitwise Operators │ ├── Bitwise-Operators.c │ └── README.md ├── 10-Printing Pattern Using Loops │ ├── Printing-Pattern-Using-Loop.c │ └── README.md ├── 11-1D Arrays in C │ ├── 1D-Arrays-in-C.c │ └── README.md ├── 12-Array Reversal │ ├── Array-Reversal.c │ └── README.md ├── 13-Printing Tokens │ ├── Printing-Tokens.c │ └── README.md ├── 14-Digit Frequency │ ├── Digit-Frequency.c │ └── README.md ├── 15-Dynamic Array in C │ ├── Dynamic-Array-in-C.c │ └── README.md └── README.md ├── README.md └── certificates └── problem-solving-intermediate ├── bitwise-and ├── README.md ├── problem.png └── solution.c ├── equalizing-array-elements ├── README.md ├── problem.png └── solution.c ├── file-renaming ├── README.md ├── problem.png └── solution.c ├── hotel-construction ├── README.md ├── problem.png └── solution.c ├── largest-area ├── README.md ├── problem.png └── solution.c ├── maximizing-element-with-constraints ├── README.md ├── problem_1.png ├── problem_2.png ├── problem_3.png ├── solution.c └── solution_2.c ├── maximum-subarray-value ├── README.md ├── problem.png └── solution.c ├── nice-teams ├── README.md ├── problem.png └── solution.c ├── sorted-sums ├── README.md ├── problem.png └── solution.c ├── task-of-pairing ├── README.md ├── problem.png └── solution.c └── user-friendly-password-system ├── README.md ├── problem.png └── solution.c /C/01-Hello World!_ in C/Hello-World!-in-C.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | char s[100]; 9 | 10 | scanf("%[^\n]%*c", &s); 11 | printf("Hello, World!\n%s",s); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /C/01-Hello World!_ in C/README.md: -------------------------------------------------------------------------------- 1 | # "Hello World!" in C 2 | ## Easy 3 | 4 | ### Objective 5 | 6 | In this challenge, we will learn some basic concepts of C 7 | that will get you started with the language. You will need 8 | to use the same syntax to read input and write output in many 9 | C challenges. As you work through these problems, review the code 10 | stubs to learn about reading from stdin and writing to stdout. 11 | 12 | ### Task 13 | 14 | This challenge requires you to print *Hello, World!* on a single line, 15 | and then print the already provided input string to stdout. 16 | If you are not familiar with C, you may want to read about the printf() command. 17 | 18 | ### Example 19 | 20 | *s = "Life is beautiful"* 21 | 22 | The required output is: 23 | ``` 24 | Hello, World! 25 | Life is beautiful 26 | ``` 27 | ### Function Description 28 | 29 | Complete the main() function below. 30 | 31 | The main() function has the following input: 32 | 33 | - string s: a string 34 | 35 | **Prints** 36 | 37 | - two strings: "Hello, World!" on one line and the input string on the next line. 38 | 39 | **Input Format** 40 | 41 | There is one line of text, *s*. 42 | 43 | **Sample Input 0** 44 | ``` 45 | Welcome to C programming. 46 | ``` 47 | **Sample Output 0** 48 | ``` 49 | Hello, World! 50 | Welcome to C programming. 51 | ``` 52 | -------------------------------------------------------------------------------- /C/02-Playing With Characters/Playing-With-Characters.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | char ch; 9 | char s[46]; //an array of 46 because the longest word in english is 45 char. long 10 | char sen[100]; 11 | 12 | scanf("%c", &ch ); 13 | scanf("%s \n", &s ); 14 | scanf("%[^\n]%*c", &sen ); 15 | 16 | printf("%c \n", ch); 17 | printf("%s \n", s); 18 | printf("%s", sen); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /C/02-Playing With Characters/README.md: -------------------------------------------------------------------------------- 1 | # Playing With Characters 2 | ## Easy 3 | ### Objective 4 | 5 | This challenge will help you to learn how to take a character, a string and a sentence as input in C. 6 | 7 | To take a single character *ch* as input, you can use scanf("%c", &ch ); and printf("%c", ch) writes a character specified by the argument char to stdout 8 | ``` 9 | char ch; 10 | scanf("%c", &ch); 11 | printf("%c", ch); 12 | ``` 13 | This piece of code prints the character *ch*. 14 | 15 | You can take a string as input in C using scanf(“%s”, s). But, it accepts string only until it finds the first space. 16 | 17 | In order to take a line as input, you can use scanf("%[^\n]%*c", s); where *s* is defined as char s[MAX_LEN] where *MAX_LEN* is the maximum size of . Here, [] is the scanset character. ^\n stands for taking input until a newline isn't encountered. Then, with this %*c, it reads the newline character and here, the used * indicates that this newline character is discarded. 18 | 19 | **Note:** The statement: scanf("%[^\n]%*c", s); will not work because the last statement will read a newline character, \n, from the previous line. This can be handled in a variety of ways. One way is to use scanf("\n"); before the last statement. 20 | 21 | ### Task 22 | 23 | You have to print the character, *ch*, in the first line. Then print in next line. In the last line print the sentence, *sen*. 24 | 25 | ### Input Format 26 | 27 | First, take a character, *ch* as input. 28 | Then take the string, *s* as input. 29 | Lastly, take the sentence *sen* as input. 30 | 31 | ### Constraints 32 | 33 | Strings for *s* and *sen* will have fewer than 100 characters, including the newline. 34 | 35 | ### Output Format 36 | 37 | Print three lines of output. The first line prints the character, *ch*. 38 | The second line prints the string, *s*. 39 | The third line prints the sentence, *sen*. 40 | 41 | ### Sample Input 0 42 | ``` 43 | C 44 | Language 45 | Welcome To C!! 46 | ``` 47 | -------------------------------------------------------------------------------- /C/03-Sum and Difference of Two Numbers/README.md: -------------------------------------------------------------------------------- 1 | # Sum and Difference of Two Numbers 2 | ## Easy 3 | ### Objective 4 | 5 | The fundamental data types in c are int, float and char. Today, we're discussing int and float data types. 6 | 7 | The printf() function prints the given statement to the console. The syntax is printf("format string",argument_list);. In the function, if we are using an integer, character, string or float as argument, then in the format string we have to write %d (integer), %c (character), %s (string), %f (float) respectively. 8 | 9 | The scanf() function reads the input data from the console. The syntax is scanf("format string",argument_list);. For ex: The scanf("%d",&number) statement reads integer number from the console and stores the given value in variable *number*. 10 | 11 | To input two integers separated by a space on a single line, the command is scanf("%d %d", &n, &m), where *n* and *m* are the two integers. 12 | 13 | ### Task 14 | 15 | Your task is to take two numbers of int data type, two numbers of float data type as input and output their sum: 16 | 17 | Declare variables: two of type int and two of type float. 18 | Read lines of input from stdin (according to the sequence given in the 'Input Format' section below) and initialize your variables. 19 | Use the and operator to perform the following operations: 20 | Print the sum and difference of two int variable on a new line. 21 | Print the sum and difference of two float variable rounded to one decimal place on a new line. 22 | 23 | ### Input Format 24 | 25 | The first line contains two integers. 26 | The second line contains two floating point numbers. 27 | 28 | ### Constraints 29 | 30 | 1 <= integer variables <= 10^4 31 | 1 <= float variables <= 10^4 32 | 33 | ### Output Format 34 | 35 | Print the sum and difference of both integers separated by a space on the first line, and the sum and difference of both float (scaled to 1 decimal place) separated by a space on the second line. 36 | 37 | ### Sample Input 38 | ``` 39 | 10 4 40 | 4.0 2.0 41 | ``` 42 | 43 | ### Sample Output 44 | ``` 45 | 14 6 46 | 6.0 2.0 47 | ``` 48 | -------------------------------------------------------------------------------- /C/03-Sum and Difference of Two Numbers/Sum-and-Difference-of-Two-Numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | int int1,int2; 9 | float float1,float2; 10 | 11 | scanf("%d %d %f %f", &int1 ,&int2, &float1, &float2); 12 | 13 | printf("%d %d\n", (int1+int2), (int1-int2)); 14 | printf("%0.1f %0.1f", (float1+float2), (float1-float2)); 15 | 16 | return 0; 17 | } 18 | 19 | // can also be done with a function passing the variables to the functions and returing the sum/diff values, for a neat and clean code. 20 | -------------------------------------------------------------------------------- /C/04-Functions in C/Functions-in-C.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int max_of_four(int a, int b, int c, int d) 4 | { 5 | int AwB, CwD, res; 6 | 7 | AwB = a > b ? a : b; 8 | CwD = c > d ? c : d; 9 | res = AwB > CwD ? AwB : CwD; 10 | 11 | return res; 12 | } 13 | 14 | 15 | int main() 16 | { 17 | int a, b, c, d; 18 | 19 | scanf("%d %d %d %d", &a, &b, &c, &d); 20 | 21 | int ans = max_of_four(a, b, c, d); 22 | 23 | printf("%d", ans); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /C/04-Functions in C/README.md: -------------------------------------------------------------------------------- 1 | # Functions in C 2 | ## Easy 3 | 4 | ### Objective 5 | 6 | In this challenge, you will learn simple usage of functions in C. Functions are a bunch of statements grouped together. A function is provided with zero or more arguments, and it executes the statements on it. Based on the return type, it either returns nothing (void) or something. 7 | 8 | A sample syntax for a function is 9 | ``` 10 | return_type function_name(arg_type_1 arg_1, arg_type_2 arg_2, ...) { 11 | ... 12 | ... 13 | ... 14 | [if return_type is non void] 15 | return something of type `return_type`; 16 | } 17 | ``` 18 | For example, a function to read four variables and return the sum of them can be written as 19 | ``` 20 | int sum_of_four(int a, int b, int c, int d) { 21 | int sum = 0; 22 | sum += a; 23 | sum += b; 24 | sum += c; 25 | sum += d; 26 | return sum; 27 | } 28 | ``` 29 | ``` 30 | += : Add and assignment operator. It adds the right operand to the left operand and assigns the result to the left operand. 31 | 32 | a += b is equivalent to a = a + b; 33 | ``` 34 | ### Task 35 | 36 | Write a function int max_of_four(int a, int b, int c, int d) which reads four arguments and returns the greatest of them. 37 | 38 | **Note** 39 | 40 | There is not built in max function in C. Code that will be reused is often put in a separate function, e.g. int max(x, y) that returns the greater of the two values. 41 | 42 | ### Input Format 43 | 44 | Input will contain four integers -*a*, *b*, *c*, *d* one on each line. 45 | 46 | ### Output Format 47 | 48 | Print the greatest of the four integers. 49 | **Note:** I/O will be automatically handled. 50 | 51 | ### Sample Input 52 | ``` 53 | 3 54 | 4 55 | 6 56 | 5 57 | ``` 58 | ### Sample Output 59 | ``` 60 | 6 61 | ``` 62 | -------------------------------------------------------------------------------- /C/05-Pointers in C/Pointers-in-C.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void update(int *a,int *b) 4 | { 5 | int x,y; 6 | x= (*a)+(*b); 7 | y= *a > *b ? (*a-*b):(*b-*a); 8 | 9 | *a=x; 10 | *b=y; 11 | } 12 | 13 | int main() 14 | { 15 | int a, b; 16 | int *pa = &a, *pb = &b; 17 | 18 | scanf("%d %d", &a, &b); 19 | update(pa, pb); 20 | printf("%d\n%d", a, b); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /C/05-Pointers in C/README.md: -------------------------------------------------------------------------------- 1 | # Pointers in C 2 | ## Easy 3 | 4 | ### Objective 5 | 6 | In this challenge, you will learn to implement the basic functionalities of pointers in C. A pointer in C is a way to share a memory address among different contexts (primarily functions). They are primarily used whenever a function needs to modify the content of a variable that it does not own. 7 | 8 | In order to access the memory address of a variable, *val*, prepend it with *&* sign. For example, &val returns the memory address of *val*. 9 | 10 | This memory address is assigned to a pointer and can be shared among various functions. For example, *int *p = &val* will assign the memory address of *val* to pointer *p*. To access the content of the memory to which the pointer points, prepend it with a *. For example, *p will return the value reflected by *val* and any modification to it will be reflected at the source (*val*). 11 | ``` 12 | void increment(int *v) { 13 | (*v)++; 14 | } 15 | int main() { 16 | int a; 17 | scanf("%d", &a); 18 | increment(&a); 19 | printf("%d", a); 20 | return 0; 21 | } 22 | ``` 23 | ### Task 24 | 25 | Complete the function void update(int *a,int *b). It receives two integer pointers, int* a and int* b. Set the value of to their sum, and to their absolute difference. There is no return value, and no return statement is needed. 26 | 27 | ### Input Format 28 | 29 | The input will contain two integers, *a* and *b*, separated by a newline. 30 | 31 | ### Output Format 32 | 33 | Modify the two values in place and the code stub main() will print their values. 34 | 35 | Note: Input/ouput will be automatically handled. You only have to complete the function described in the 'task' section. 36 | 37 | ### Sample Input 38 | ``` 39 | 4 40 | 5 41 | ``` 42 | ### Sample Output 43 | ``` 44 | 9 45 | 1 46 | ``` 47 | -------------------------------------------------------------------------------- /C/06-Conditional Statements in C/Conditional-Statements-in-C.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | char* readline(); 12 | 13 | char *strings[] = {"one","two","three","four","five","six","seven","eight","nine"}; 14 | 15 | int main() 16 | { 17 | char* n_endptr; 18 | char* n_str = readline(); 19 | int n = strtol(n_str, &n_endptr, 10); 20 | int s; 21 | 22 | if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); } 23 | 24 | // just a simple scanf then these conditions, the rest of the code is pre written i guess it's for the online tests and compiler 25 | if(n<=9){ 26 | printf("%s", strings[n-1]); 27 | } 28 | else if (n>9){ 29 | printf("Greater than 9"); 30 | } 31 | return 0; 32 | } 33 | 34 | char* readline() 35 | { 36 | size_t alloc_length = 1024; 37 | size_t data_length = 0; 38 | char* data = malloc(alloc_length); 39 | 40 | while (true) 41 | { 42 | char* cursor = data + data_length; 43 | char* line = fgets(cursor, alloc_length - data_length, stdin); 44 | 45 | if (!line) { break; } 46 | 47 | data_length += strlen(cursor); 48 | 49 | if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; } 50 | 51 | size_t new_length = alloc_length << 1; 52 | data = realloc(data, new_length); 53 | 54 | if (!data) { break; } 55 | 56 | alloc_length = new_length; 57 | } 58 | 59 | if (data[data_length - 1] == '\n') 60 | { 61 | data[data_length - 1] = '\0'; 62 | } 63 | 64 | data = realloc(data, data_length); 65 | 66 | return data; 67 | } 68 | -------------------------------------------------------------------------------- /C/06-Conditional Statements in C/README.md: -------------------------------------------------------------------------------- 1 | # Conditional Statments in C 2 | ## Easy 3 | 4 | ### Objective 5 | 6 | if and else are two of the most frequently used conditionals in C/C++, and they enable you to execute zero or one conditional statement among many such dependent conditional statements. We use them in the following ways: 7 | 8 | if: This executes the body of bracketed code starting with *statement 1* if *condition* evaluates to true. 9 | ``` 10 | if (condition) { 11 | statement1; 12 | ... 13 | } 14 | ``` 15 | if - else: This executes the body of bracketed code starting with *statement 1* if *condition* evaluates to true, or it executes the body of code starting with *statement 2* if *condition* evaluates to false. Note that only one of the bracketed code sections will ever be executed. 16 | ``` 17 | if (condition) { 18 | statement1; 19 | ... 20 | } 21 | else { 22 | statement2; 23 | ... 24 | } 25 | ``` 26 | if - else if - else: In this structure, dependent statements are chained together and the *condition* for each statement is only checked if all prior conditions in the chain are evaluated to false. Once a *condition* evaluates to true, the bracketed code associated with that statement is executed and the program then skips to the end of the chain of statements and continues executing. If each *condition* in the chain evaluates to false, then the body of bracketed code in the else block at the end is executed. 27 | ``` 28 | if(first condition) { 29 | ... 30 | } 31 | else if(second condition) { 32 | ... 33 | } 34 | . 35 | . 36 | . 37 | else if((n-1)'th condition) { 38 | .... 39 | } 40 | else { 41 | ... 42 | } 43 | ``` 44 | 45 | ### Task 46 | 47 | Given a positive integer denoting *n*, do the following: 48 | 49 | If 1 <= n <= 9, print the lowercase English word corresponding to the number (e.g., one for 1, two for 2, etc.). 50 | If n > 9 , print Greater than 9. 51 | 52 | ### Input Format 53 | 54 | The first line contains a single integer, *n*. 55 | 56 | ### Constraints 57 | 1 <= n <= 10^9 58 | 59 | ### Output Format 60 | 61 | If 1 <= n <= 9, then print the lowercase English word corresponding to the number (e.g., one for 1, two for 2, etc.); otherwise, print Greater than 9 instead. 62 | 63 | ### Sample Input 64 | ``` 65 | 5 66 | ``` 67 | ### Sample Output 68 | ``` 69 | five 70 | ``` 71 | ### Sample Input #01 72 | ``` 73 | 8 74 | ``` 75 | ### Sample Output #01 76 | ``` 77 | eight 78 | ``` 79 | ### Sample Input #02 80 | ``` 81 | 44 82 | ``` 83 | ### Sample Output #02 84 | ``` 85 | Greater than 9 86 | ``` 87 | -------------------------------------------------------------------------------- /C/07-For Loop in C/For-Loop-in-C.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | char *strings[] = {"one","two","three","four","five","six","seven","eight","nine"}; 7 | 8 | int main() 9 | { 10 | int a, b; 11 | scanf("%d\n%d", &a, &b); 12 | 13 | for(int i=a; i<=b ;i++) 14 | { 15 | if (i<=9) 16 | { 17 | printf("%s\n", strings[i-1]); 18 | } 19 | else if (i>9 && (i%2)==0) 20 | { 21 | printf("even\n"); 22 | } 23 | else if (i>9 && (i%2)!=0) 24 | { 25 | printf("odd\n"); 26 | } 27 | } 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /C/07-For Loop in C/README.md: -------------------------------------------------------------------------------- 1 | # For Loop in C 2 | ## Easy 3 | 4 | ### Objective 5 | 6 | In this challenge, you will learn the usage of the for loop, which is a programming language statement which allows code to be executed until a terminal condition is met. They can even repeat forever if the terminal condition is never met. 7 | 8 | The syntax for the for loop is: 9 | ``` 10 | for ( ; ; ) 11 | 12 | ``` 13 | - expression_1 is used for intializing variables which are generally used for controlling the terminating flag for the loop. 14 | - expression_2 is used to check for the terminating condition. If this evaluates to false, then the loop is terminated. 15 | - expression_3 is generally used to update the flags/variables. 16 | The following loop initializes *i* to 0, tests that *i* is less than 10, and increments *i* at every iteration. It will execute 10 times. 17 | ``` 18 | for(int i = 0; i < 10; i++) { 19 | ... 20 | } 21 | ``` 22 | 23 | ### Task 24 | 25 | For each integer *n* in the interval *[a,b] (given as input) : 26 | 27 | - If 1 <= n <= 9, then print the English representation of it in lowercase. That is "one" for 1, "two" for 2, and so on. 28 | - Else if n > 9 and it is an even number, then print "even". 29 | - Else if n > 9and it is an odd number, then print "odd". 30 | 31 | ### Input Format 32 | 33 | The first line contains an integer, *a*. 34 | The seond line contains an integer, *b*. 35 | 36 | ### Output Format 37 | 38 | Print the appropriate English representation,even, or odd, based on the conditions described in the 'task' section. 39 | 40 | ### Sample Input 41 | ``` 42 | 8 43 | 11 44 | ``` 45 | ### Sample Output 46 | ``` 47 | eight 48 | nine 49 | even 50 | odd 51 | ``` 52 | -------------------------------------------------------------------------------- /C/08-Sum of Digits of a Five Digit Number/README.md: -------------------------------------------------------------------------------- 1 | # Sum of Digits of a Five Digit Number 2 | ## Easy 3 | 4 | ### Objective 5 | 6 | The modulo operator, %, returns the remainder of a division. For example, 4 % 3 = 1 and 12 % 10 = 2. The ordinary division operator, /, returns a truncated integer value when performed on integers. For example, 5 / 3 = 1. To get the last digit of a number in base 10, use 10 as the modulo divisor. 7 | 8 | ### Task 9 | 10 | Given a five digit integer, print the sum of its digits. 11 | 12 | ### Input Format 13 | 14 | The input contains a single five digit number, *n*. 15 | 16 | ### Output Format 17 | 18 | Print the sum of the digits of the five digit number. 19 | 20 | ### Sample Input 0 21 | ``` 22 | 10564 23 | ``` 24 | ### Sample Output 0 25 | ``` 26 | 16 27 | ``` 28 | -------------------------------------------------------------------------------- /C/08-Sum of Digits of a Five Digit Number/Sum-of-Digits-of-a-Five-Digit-Number.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | int n; 9 | int sum=0; 10 | scanf("%d", &n); 11 | 12 | /* for a generic algorithm you would make a while loop 13 | * and the condition will be until the number is null, 14 | * but in this case it says it will only be a 5 digit num. 15 | */ 16 | 17 | for(int i=0; i<=5; i++) 18 | { 19 | sum=sum + n%10; 20 | n=n/10; 21 | } 22 | 23 | printf("%d", sum); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /C/09-Bitwise Operators/Bitwise-Operators.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void calculate_the_maximum(int n, int k) 7 | { 8 | int maxand =0; 9 | int maxor =0; 10 | int maxxor =0; 11 | 12 | for(int i=1; i<=n; i++) 13 | { 14 | for(int j=i+1; j<=n; j++) 15 | { 16 | maxand = ((i&j)>maxand)&&((i&j)maxor) &&((i|j)maxxor)&&((i^j) 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | 8 | int main() 9 | { 10 | int n; 11 | int max; 12 | 13 | scanf("%d", &n); 14 | 15 | // the loops are (2*n-1) iterations 16 | for (int row = -n + 1; row < n; row++) 17 | { 18 | for (int col = -n + 1; col < n; col++) 19 | { 20 | max = abs(row) > abs(col) ? abs(row) : abs(col); 21 | printf("%d ", max + 1); 22 | } 23 | printf("\n"); 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /C/10-Printing Pattern Using Loops/README.md: -------------------------------------------------------------------------------- 1 | # Printing Pattern Using Loops 2 | ## Medium 3 | 4 | ### Objective 5 | 6 | Print a pattern of numbers from 1 to n as shown below. Each of the numbers is separated by a single space. 7 | ``` 8 | 4 4 4 4 4 4 4 9 | 4 3 3 3 3 3 4 10 | 4 3 2 2 2 3 4 11 | 4 3 2 1 2 3 4 12 | 4 3 2 2 2 3 4 13 | 4 3 3 3 3 3 4 14 | 4 4 4 4 4 4 4 15 | ``` 16 | ### Input Format 17 | 18 | The input will contain a single integer n. 19 | 20 | 21 | ### Sample Input 0 22 | ``` 23 | 2 24 | ``` 25 | ### Sample Output 0 26 | ``` 27 | 2 2 2 28 | 2 1 2 29 | 2 2 2 30 | ``` 31 | ### Sample Input 1 32 | ``` 33 | 5 34 | ``` 35 | ### Sample Output 1 36 | ``` 37 | 5 5 5 5 5 5 5 5 5 38 | 5 4 4 4 4 4 4 4 5 39 | 5 4 3 3 3 3 3 4 5 40 | 5 4 3 2 2 2 3 4 5 41 | 5 4 3 2 1 2 3 4 5 42 | 5 4 3 2 2 2 3 4 5 43 | 5 4 3 3 3 3 3 4 5 44 | 5 4 4 4 4 4 4 4 5 45 | 5 5 5 5 5 5 5 5 5 46 | ``` 47 | ### Sample Input 2 48 | ``` 49 | 7 50 | ``` 51 | ### Sample Output 2 52 | ``` 53 | 7 7 7 7 7 7 7 7 7 7 7 7 7 54 | 7 6 6 6 6 6 6 6 6 6 6 6 7 55 | 7 6 5 5 5 5 5 5 5 5 5 6 7 56 | 7 6 5 4 4 4 4 4 4 4 5 6 7 57 | 7 6 5 4 3 3 3 3 3 4 5 6 7 58 | 7 6 5 4 3 2 2 2 3 4 5 6 7 59 | 7 6 5 4 3 2 1 2 3 4 5 6 7 60 | 7 6 5 4 3 2 2 2 3 4 5 6 7 61 | 7 6 5 4 3 3 3 3 3 4 5 6 7 62 | 7 6 5 4 4 4 4 4 4 4 5 6 7 63 | 7 6 5 5 5 5 5 5 5 5 5 6 7 64 | 7 6 6 6 6 6 6 6 6 6 6 6 7 65 | 7 7 7 7 7 7 7 7 7 7 7 7 7 66 | ``` 67 | -------------------------------------------------------------------------------- /C/11-1D Arrays in C/1D-Arrays-in-C.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | int n; 9 | int val; 10 | int sum=0; 11 | 12 | scanf("%d", &n); 13 | 14 | int*arr = (int*)malloc(n*sizeof(int)); 15 | 16 | for(int i=0; i 2 | #include 3 | 4 | int main() 5 | { 6 | int num, *arr,*arrrev, i; 7 | 8 | scanf("%d", &num); 9 | arr = (int*) malloc(num * sizeof(int)); 10 | 11 | for(i = 0; i < num; i++) 12 | { 13 | scanf("%d", arr + i); 14 | } 15 | 16 | arrrev = (int*) malloc(num * sizeof(int)); 17 | 18 | for(i = 0; i < num; i++) 19 | { 20 | arrrev[i]=arr[num-1-i]; 21 | } 22 | 23 | for(i = 0; i < num; i++) 24 | { 25 | arr[i]=arrrev[i]; 26 | printf("%d ", arr[i]); 27 | } 28 | 29 | free(arr); 30 | free(arrrev); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /C/12-Array Reversal/README.md: -------------------------------------------------------------------------------- 1 | # Array Reversal 2 | ## Medium 3 | 4 | ### Objective 5 | 6 | Given an array, of size n, reverse it. 7 | 8 | Example: If array, arr = [1,2,3,4,5], after reversing it, the array should be, arr = [5,4,3,2,1]. 9 | 10 | ### Input Format 11 | 12 | The first line contains an integer, n, denoting the size of the array. The next line contains space-separated integers denoting the elements of the array. 13 | 14 | ### Output Format 15 | 16 | The output is handled by the code given in the editor, which would print the array. 17 | 18 | ### Sample Input 0 19 | ``` 20 | 6 21 | 16 13 7 2 1 12 22 | ``` 23 | ### Sample Output 0 24 | ``` 25 | 12 1 2 7 13 16 26 | ``` 27 | ### Sample Input 1 28 | ``` 29 | 7 30 | 1 13 15 20 12 13 2 31 | ``` 32 | ### Sample Output 1 33 | ``` 34 | 2 13 12 20 15 13 1 35 | ``` 36 | ### Sample Input 2 37 | ``` 38 | 8 39 | 15 5 16 15 17 11 5 11 40 | ``` 41 | ### Sample Output 2 42 | ``` 43 | 11 5 11 17 15 16 5 15 44 | ``` 45 | -------------------------------------------------------------------------------- /C/13-Printing Tokens/Printing-Tokens.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | char *s; 9 | s = malloc(1024 * sizeof(char)); 10 | 11 | scanf("%[^\n]", s); 12 | s = realloc(s, strlen(s) + 1); 13 | 14 | //This prints each character on it's own and prints \n instead of spaces 15 | for(int i=0; i 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | int num[10]={0,0,0,0,0,0,0,0,0,0}; 9 | 10 | char *s; 11 | s = malloc(1024 * sizeof(char)); 12 | scanf("%[^\n]", s); 13 | s = realloc(s, strlen(s) + 1); 14 | 15 | for(int i=0; i= threshold) 27 | minO = ( minO <= dp[x][1] )? minO : dp[x][1]; 28 | 29 | if (x == 0) 30 | break; 31 | 32 | arr[i] /= d; 33 | steps += 1; 34 | } 35 | } 36 | return minO; 37 | } 38 | -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/file-renaming/README.md: -------------------------------------------------------------------------------- 1 | # File Renaming 2 | ## Problem Solving Intermediate 3 | ### 15/15 Test Cases 4 | 5 | ![problem](problem.png) 6 | -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/file-renaming/problem.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MahmoudNageh/hackerrank-c-solutions/63c3b5758a9dee3925a08bd57515436e63435e0d/certificates/problem-solving-intermediate/file-renaming/problem.png -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/file-renaming/solution.c: -------------------------------------------------------------------------------- 1 | #define MOD 1000000007 2 | 3 | long temp_arr[1000000]; 4 | long occ_arr[1000000]; 5 | 6 | int renameFile(char* newName,char* oldName) 7 | { 8 | long new_lenght = strlen(newName); 9 | long old_length = strlen(oldName); 10 | 11 | 12 | for (long z = 0; z < old_length + 1; z++) 13 | { 14 | temp_arr[z] = 1; 15 | } 16 | 17 | for (long i = 1; i < new_lenght + 1; i++) 18 | { 19 | for (long z = 0; z < old_length + 1; z++) 20 | { 21 | occ_arr[z] = 0; 22 | } 23 | 24 | for (long j = i; j < old_length + 1 ; j++) 25 | { 26 | occ_arr[j] = occ_arr[j - 1]; 27 | 28 | if (newName[i - 1] == oldName[j - 1]) 29 | occ_arr[j] += temp_arr[j - 1]; 30 | 31 | } 32 | 33 | for (long z = 0; z < old_length + 1; z++) 34 | { 35 | temp_arr[z] = occ_arr[z]; 36 | } 37 | 38 | } 39 | 40 | return occ_arr[old_length] % MOD; 41 | } 42 | -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/hotel-construction/README.md: -------------------------------------------------------------------------------- 1 | # Hotel Construction 2 | ## Problem Solving Intermediate C Solution 3 | ### 15/15 Test Cases 4 | 5 | ![problem](problem.png) 6 | -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/hotel-construction/problem.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MahmoudNageh/hackerrank-c-solutions/63c3b5758a9dee3925a08bd57515436e63435e0d/certificates/problem-solving-intermediate/hotel-construction/problem.png -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/hotel-construction/solution.c: -------------------------------------------------------------------------------- 1 | #define MAX_CITIES_CNT (50) 2 | 3 | 4 | void initDistance(int distance[MAX_CITIES_CNT][MAX_CITIES_CNT], int src, int des, int val, int citiesCnt, char isThereRoad[MAX_CITIES_CNT][MAX_CITIES_CNT]) 5 | { 6 | distance[src][des] = val; 7 | 8 | for (int nextDes = 0; nextDes < citiesCnt; nextDes++) 9 | { 10 | if (isThereRoad[des][nextDes] && distance[src][nextDes] == -1) 11 | { 12 | initDistance(distance, src, nextDes, val + 1, citiesCnt, isThereRoad); 13 | } 14 | } 15 | } 16 | 17 | 18 | int numberOfWays(int roads_rows, int roads_columns, int** roads) 19 | { 20 | int citiesCnt = roads_rows + 1; 21 | 22 | /* 23 | * isThereRoad[src][des] stores either 0 or 1: 24 | * - 0: if there's no direct road from src city to des city 25 | * - 1: if there's a direct road from src city to des city 26 | */ 27 | char isThereRoad[MAX_CITIES_CNT][MAX_CITIES_CNT] = {0}; 28 | 29 | for (int i = 0; i < roads_rows; i++) 30 | { 31 | int city1 = roads[i][0] - 1; 32 | int city2 = roads[i][1] - 1; 33 | 34 | isThereRoad[city1][city2] = 1; 35 | isThereRoad[city2][city1] = 1; 36 | } 37 | 38 | 39 | /* 40 | * distance[src][des] stores distance between src city to des city 41 | */ 42 | int distance[MAX_CITIES_CNT][MAX_CITIES_CNT]; 43 | 44 | // set all to -1 (uninitialized) 45 | for (int src = 0; src < citiesCnt; src++) 46 | { 47 | for (int des = 0; des < citiesCnt; des++) 48 | { 49 | distance[src][des] = -1; 50 | } 51 | } 52 | 53 | 54 | // initialize distances 55 | for (int src = 0; src < citiesCnt; src++) 56 | { 57 | // ditance from src city to itself = 0 58 | initDistance(distance, src, src, 0, citiesCnt, isThereRoad); 59 | } 60 | 61 | 62 | // get 3 cities with equal distances between each two 63 | int cnt = 0; 64 | 65 | for (int city1 = 0; city1 < citiesCnt; city1++) 66 | { 67 | for (int city2 = city1 + 1; city2 < citiesCnt; city2++) 68 | { 69 | for (int city3 = city2 + 1; city3 < citiesCnt; city3++) 70 | { 71 | if (distance[city1][city2] == distance[city1][city3] && distance[city1][city2] == distance[city2][city3]) 72 | cnt++; 73 | } 74 | } 75 | } 76 | 77 | return cnt; 78 | } 79 | -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/largest-area/README.md: -------------------------------------------------------------------------------- 1 | # Largest Area 2 | ## Problem Solving Intermediate 3 | ### 11/15 Test Cases 4 | 5 | ![problem](problem.png) 6 | 7 | -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/largest-area/problem.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MahmoudNageh/hackerrank-c-solutions/63c3b5758a9dee3925a08bd57515436e63435e0d/certificates/problem-solving-intermediate/largest-area/problem.png -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/largest-area/solution.c: -------------------------------------------------------------------------------- 1 | #define MAX(a,b) ((a) > (b) ? (a) : (b)) 2 | 3 | int cmp(const void* a, const void* b) 4 | { 5 | return *(const int*) a - *(const int*) b; 6 | } 7 | 8 | int maxH, maxV, boundry; 9 | 10 | long maxArea(int w, int h, int* horizontalCuts, int horizontalCutsSize, int* verticalCuts, int verticalCutsSize) 11 | { 12 | if ( boundry == 0 ) 13 | { 14 | maxH = MAX(horizontalCuts[0], h - horizontalCuts[horizontalCutsSize - 1]); 15 | 16 | for (int i = 1; i < horizontalCutsSize; i++) 17 | { 18 | maxH = MAX(maxH, horizontalCuts[i] - horizontalCuts[i-1]); 19 | } 20 | } 21 | else if(boundry == 1) 22 | { 23 | maxV = MAX(verticalCuts[0], w - verticalCuts[verticalCutsSize - 1]); 24 | 25 | for (int i = 1; i < verticalCutsSize; i++) 26 | { 27 | maxV = MAX(maxV, verticalCuts[i] - verticalCuts[i-1]); 28 | } 29 | } 30 | return maxH * maxV; 31 | } 32 | 33 | long* getMaxArea(int w, int h, int isVertical_count, bool* isVertical,int distance_count, int* distance, int* result_count) 34 | { 35 | maxH = h; 36 | maxV = w; 37 | 38 | *result_count = isVertical_count; 39 | 40 | long* result = (long*) malloc(isVertical_count * sizeof(long)); 41 | 42 | // Boundry counters 43 | int horizontalCutsSize = 0; 44 | int verticalCutsSize = 0; 45 | 46 | int* horizontalCuts = (int*) malloc(isVertical_count * sizeof(int)); 47 | int* verticalCuts = (int*) malloc(isVertical_count * sizeof(int)); 48 | 49 | for (int i = 0; i < isVertical_count; i++) 50 | { 51 | // horizontal Boundry 52 | if (isVertical[i] == 0) 53 | { 54 | boundry = 0; 55 | horizontalCuts[horizontalCutsSize] = distance[i]; 56 | horizontalCutsSize++; 57 | qsort(horizontalCuts, horizontalCutsSize, sizeof(int), cmp); 58 | } 59 | 60 | // Vertical Boundry 61 | else if (isVertical[i] == 1) 62 | { 63 | boundry =1; 64 | verticalCuts[verticalCutsSize] = distance[i]; 65 | verticalCutsSize++; 66 | qsort(verticalCuts, verticalCutsSize, sizeof(int), cmp); 67 | } 68 | 69 | result[i] = maxArea(w, h, horizontalCuts, horizontalCutsSize, verticalCuts, verticalCutsSize); 70 | } 71 | 72 | free(horizontalCuts); 73 | free(verticalCuts); 74 | 75 | return result; 76 | } 77 | -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/maximizing-element-with-constraints/README.md: -------------------------------------------------------------------------------- 1 | # Maximizing Element With Constraints 2 | ## Problem Solving Intermediate 3 | ### Solution_1: 11/15 Test Cases 4 | ### Solution_2: 15/15 Test Cases 5 | 6 | ![problem](problem_1.png) 7 | ![problem](problem_2.png) 8 | ![problem](problem_3.png) 9 | -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/maximizing-element-with-constraints/problem_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MahmoudNageh/hackerrank-c-solutions/63c3b5758a9dee3925a08bd57515436e63435e0d/certificates/problem-solving-intermediate/maximizing-element-with-constraints/problem_1.png -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/maximizing-element-with-constraints/problem_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MahmoudNageh/hackerrank-c-solutions/63c3b5758a9dee3925a08bd57515436e63435e0d/certificates/problem-solving-intermediate/maximizing-element-with-constraints/problem_2.png -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/maximizing-element-with-constraints/problem_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MahmoudNageh/hackerrank-c-solutions/63c3b5758a9dee3925a08bd57515436e63435e0d/certificates/problem-solving-intermediate/maximizing-element-with-constraints/problem_3.png -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/maximizing-element-with-constraints/solution.c: -------------------------------------------------------------------------------- 1 | int maxElement(int n, int maxSum, int k) 2 | { 3 | 4 | int maxValue = 0; 5 | 6 | for (int val = 1; val <= maxSum; val++) 7 | { 8 | int sum = val; 9 | 10 | // Iterate j from K-1 to 0 and find the minimum value that could be placed at j 11 | // That is the difference of val and distance between K and j and add value to sum. 12 | for (int j = k - 1; j >= 0; j--) 13 | { 14 | if ((val + j - k) > 0) 15 | { 16 | sum +=(val + j - k); 17 | } 18 | 19 | // The minimum value that could be placed at any index is 1 20 | else 21 | { 22 | sum += 1; 23 | } 24 | } 25 | 26 | // Iterate j from K+1 to N-1 27 | for (int j = k + 1; j < n; j++) 28 | { 29 | if ((val + k - j) > 0) 30 | { 31 | sum +=(val + k - j); 32 | } 33 | else 34 | { 35 | sum += 1; 36 | } 37 | } 38 | 39 | // If sum is less than or equal to maxSum then update maxValue with val, otherwise terminate the loop 40 | if (sum <= maxSum) 41 | { 42 | maxValue = val; 43 | } 44 | else 45 | { 46 | break; 47 | } 48 | } 49 | 50 | return maxValue; 51 | } 52 | -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/maximizing-element-with-constraints/solution_2.c: -------------------------------------------------------------------------------- 1 | int maxElement(int n, int maxSum, int k) 2 | { 3 | if(n==1) return maxSum; 4 | int right =k; 5 | int left =k; 6 | int count = 1; 7 | int limitRight = n-1; 8 | 9 | while(n <= maxSum && (left > 0 || right < limitRight)){ 10 | 11 | n+= right - left +1; if(left>0) left--; if(right < limitRight) right++; count++;} 12 | 13 | if(n (b) ? (a) : (b)) 2 | #define min(a,b) ((a) < (b) ? (a) : (b)) 3 | 4 | long maxSubarrayValue(int n, int* arr) 5 | { 6 | long MAX = 0; 7 | 8 | //negate odd indexed elements 9 | for(int i=1;i 0) 24 | currMin = arr[i]; 25 | else 26 | currMin += arr[i]; 27 | 28 | minSoFar = min(minSoFar , currMin); 29 | } 30 | MAX = abs(maxSoFar) > abs(minSoFar) ? abs(maxSoFar) : abs(minSoFar); 31 | 32 | return MAX * MAX; 33 | } 34 | -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/nice-teams/README.md: -------------------------------------------------------------------------------- 1 | # Nice Teams 2 | ## Problem Solving Intermediate C Solution 3 | ### 15/15 Test Cases 4 | 5 | ![problem](problem.png) 6 | -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/nice-teams/problem.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MahmoudNageh/hackerrank-c-solutions/63c3b5758a9dee3925a08bd57515436e63435e0d/certificates/problem-solving-intermediate/nice-teams/problem.png -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/nice-teams/solution.c: -------------------------------------------------------------------------------- 1 | int cmpfunc(const void *a, const void *b) 2 | { 3 | return *(int *)a - *(int *)b; 4 | } 5 | 6 | int maxPairs(int skillLevel_count, int* skillLevel, int minDiff) 7 | { 8 | qsort(skillLevel,skillLevel_count, sizeof(int), cmpfunc); 9 | int l = 0; 10 | int r = skillLevel_count / 2 + 1; 11 | while(r - l > 1) 12 | { 13 | int m = (l + r) / 2; 14 | bool good = true; 15 | for(int i = 0; i < m; i++) 16 | good &= (skillLevel[skillLevel_count - m + i] - skillLevel[i] >= minDiff); 17 | if(good) 18 | l = m; 19 | else 20 | r = m; 21 | } 22 | return l; 23 | } 24 | -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/sorted-sums/README.md: -------------------------------------------------------------------------------- 1 | # Sorted Sums 2 | ## Problem Solving Intermediate 3 | ### 11/15 Test Cases 4 | 5 | ![problem](problem.png) 6 | -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/sorted-sums/problem.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MahmoudNageh/hackerrank-c-solutions/63c3b5758a9dee3925a08bd57515436e63435e0d/certificates/problem-solving-intermediate/sorted-sums/problem.png -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/sorted-sums/solution.c: -------------------------------------------------------------------------------- 1 | void swap(int *xp, int *yp) 2 | { 3 | int temp = *xp; 4 | *xp = *yp; 5 | *yp = temp; 6 | } 7 | 8 | int sortedSum(int a_count, int* a) 9 | { 10 | long long ans = 0, M = 1e9 + 7; 11 | 12 | for(int i = 0; i < a_count; i++) 13 | { 14 | int j = i; 15 | while(j > 0 && a[j - 1] > a[j]) 16 | { 17 | swap(&a[j], &a[j-1]); 18 | j--; 19 | } 20 | for(int j = 0; j <= i; j++) 21 | { 22 | ans = (ans + a[j] * 1ll * (j + 1)) % M; 23 | } 24 | ans %= M; 25 | } 26 | return ans; 27 | } 28 | -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/task-of-pairing/README.md: -------------------------------------------------------------------------------- 1 | # Task of Pairing 2 | ## Problem Solving Intermediate C Solution 3 | ### 15/15 Test Cases 4 | 5 | ![problem](problem.png) 6 | -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/task-of-pairing/problem.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MahmoudNageh/hackerrank-c-solutions/63c3b5758a9dee3925a08bd57515436e63435e0d/certificates/problem-solving-intermediate/task-of-pairing/problem.png -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/task-of-pairing/solution.c: -------------------------------------------------------------------------------- 1 | int taskOfPairing(int freq_size, int* freq) 2 | { 3 | int pairsCnt = 0; 4 | 5 | int prevRemainder = 0; 6 | 7 | for (int i = 0; i < freq_size; i++) 8 | { 9 | if (freq[i] == 0) 10 | { 11 | prevRemainder = 0; 12 | continue; 13 | } 14 | 15 | freq[i] += prevRemainder; 16 | pairsCnt += freq[i] / 2; 17 | prevRemainder = freq[i] % 2; 18 | } 19 | 20 | return pairsCnt; 21 | } 22 | -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/user-friendly-password-system/README.md: -------------------------------------------------------------------------------- 1 | # User-Friendly Password System 2 | ## Problem Solving Intermediate C Solution 3 | ### 15/15 Test Cases 4 | 5 | ![problem](problem.png) 6 | -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/user-friendly-password-system/problem.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MahmoudNageh/hackerrank-c-solutions/63c3b5758a9dee3925a08bd57515436e63435e0d/certificates/problem-solving-intermediate/user-friendly-password-system/problem.png -------------------------------------------------------------------------------- /certificates/problem-solving-intermediate/user-friendly-password-system/solution.c: -------------------------------------------------------------------------------- 1 | #define P (131) 2 | #define MOD (1000000007) 3 | 4 | 5 | int setPassword(char* password) 6 | { 7 | long res = 0; 8 | 9 | for (int i = 0; password[i]; i++) 10 | { 11 | res *= P; 12 | res %= MOD; 13 | res += password[i]; 14 | res %= MOD; 15 | } 16 | 17 | return res; 18 | } 19 | 20 | 21 | int authorize(long correctHash, long inputHash) 22 | { 23 | if (correctHash == inputHash) 24 | return 1; 25 | 26 | correctHash *= P; 27 | correctHash %= MOD; 28 | 29 | // try adding digits 30 | for (int i = '0'; i <= '9'; i++) 31 | { 32 | if ((correctHash + i) % MOD == inputHash) 33 | return 1; 34 | } 35 | 36 | // try adding lower characters 37 | for (int i = 'a'; i <= 'z'; i++) 38 | { 39 | if ((correctHash + i) % MOD == inputHash) 40 | return 1; 41 | } 42 | 43 | // try adding upper characters 44 | for (int i = 'A'; i <= 'Z'; i++) 45 | { 46 | if ((correctHash + i) % MOD == inputHash) 47 | return 1; 48 | } 49 | 50 | return 0; 51 | } 52 | 53 | 54 | int *authEvents(int events_rows, int events_columns, char ***events, int *result_count) 55 | { 56 | int* res = malloc(events_rows * sizeof(int)); 57 | int resIdx = 0; 58 | int currHash; 59 | 60 | for (int i = 0; i < events_rows; i++) 61 | { 62 | switch (events[i][0][0]) 63 | { 64 | case 's': 65 | currHash = setPassword(events[i][1]); 66 | break; 67 | 68 | case 'a': 69 | res[resIdx] = authorize(currHash, atoi(events[i][1])); 70 | resIdx++; 71 | break; 72 | } 73 | } 74 | 75 | *result_count = resIdx; 76 | return res; 77 | } 78 | --------------------------------------------------------------------------------