Skip to content

Commit 6d93bf4

Browse files
committed
Add solutions to C domain
1 parent 047d8c3 commit 6d93bf4

24 files changed

+869
-2
lines changed

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ Solutions to HackerRank problems
55
* [Data Structures](#data-structures)
66
* [Python](#python)
77
* [Linux Shell](#linux-shell)
8-
* [C++](#c)
8+
* [CPP](#cpp)
9+
* [C](#c)
910

1011
## Algorithms
1112
| Subdomain | Difficulty | Challenge | Solution |
@@ -410,7 +411,7 @@ Solutions to HackerRank problems
410411
|Grep Sed Awk|Medium|['Sed' command #1](https://www.hackerrank.com/challenges/text-processing-in-linux-the-sed-command-1/problem)|[text-processing-in-linux-the-sed-command-1.sh](shell/text-processing-in-linux-the-sed-command-1.sh)|
411412
|Grep Sed Awk|Medium|['Sed' command #2](https://www.hackerrank.com/challenges/text-processing-in-linux-the-sed-command-2/problem)|[text-processing-in-linux-the-sed-command-2.sh](shell/text-processing-in-linux-the-sed-command-2.sh)|
412413

413-
## C++
414+
## CPP
414415
| Subdomain | Difficulty | Challenge | Solution |
415416
|:---------:|:----------:|:---------:|:--------:|
416417
|Introduction|Easy|[Say "Hello, World!" With C++](https://www.hackerrank.com/challenges/cpp-hello-world/problem)|[cpp-hello-world.cpp](cpp/cpp-hello-world.cpp)|
@@ -438,3 +439,30 @@ Solutions to HackerRank problems
438439
|STL|Easy|[Maps-STL](https://www.hackerrank.com/challenges/cpp-maps/problem)|[cpp-maps.cpp](cpp/cpp-maps.cpp)|
439440
|STL|Easy|[Print Pretty](https://www.hackerrank.com/challenges/prettyprint/problem)|[prettyprint.cpp](cpp/prettyprint.cpp)|
440441
|STL|Medium|[Deque-STL](https://www.hackerrank.com/challenges/deque-stl/problem)|[deque-stl.cpp](cpp/deque-stl.cpp)|
442+
443+
## C
444+
| Subdomain | Difficulty | Challenge | Solution |
445+
|:---------:|:----------:|:---------:|:--------:|
446+
|Introduction|Easy|["Hello World!" in C](https://www.hackerrank.com/challenges/hello-world-c/problem)|[hello-world-c.c](c/hello-world-c.c)|
447+
|Introduction|Easy|[Playing With Characters](https://www.hackerrank.com/challenges/playing-with-characters/problem)|[playing-with-characters.c](c/playing-with-characters.c)|
448+
|Introduction|Easy|[Sum and Difference of Two Numbers](https://www.hackerrank.com/challenges/sum-numbers-c/problem)|[sum-numbers-c.c](c/sum-numbers-c.c)|
449+
|Introduction|Easy|[Functions in C](https://www.hackerrank.com/challenges/functions-in-c/problem)|[functions-in-c.c](c/functions-in-c.c)|
450+
|Introduction|Easy|[Pointers in C](https://www.hackerrank.com/challenges/pointer-in-c/problem)|[pointer-in-c.c](c/pointer-in-c.c)|
451+
|Conditionals and Loops|Easy|[Conditional Statements in C](https://www.hackerrank.com/challenges/conditional-statements-in-c/problem)|[conditional-statements-in-c.c](c/conditional-statements-in-c.c)|
452+
|Conditionals and Loops|Easy|[For Loop in C](https://www.hackerrank.com/challenges/for-loop-in-c/problem)|[for-loop-in-c.c](c/for-loop-in-c.c)|
453+
|Conditionals and Loops|Easy|[Sum of Digits of a Five Digit Number](https://www.hackerrank.com/challenges/sum-of-digits-of-a-five-digit-number/problem)|[sum-of-digits-of-a-five-digit-number.c](c/sum-of-digits-of-a-five-digit-number.c)|
454+
|Conditionals and Loops|Easy|[Bitwise Operators](https://www.hackerrank.com/challenges/bitwise-operators-in-c/problem)|[bitwise-operators-in-c.c](c/bitwise-operators-in-c.c)|
455+
|Conditionals and Loops|Medium|[Printing Pattern using Loops](https://www.hackerrank.com/challenges/printing-pattern-2/problem)|[printing-pattern-2.c](c/printing-pattern-2.c)|
456+
|Arrays and Strings|Medium|[1D Arrays in C](https://www.hackerrank.com/challenges/1d-arrays-in-c/problem)|[1d-arrays-in-c.c](c/1d-arrays-in-c.c)|
457+
|Arrays and Strings|Medium|[Array Reversal](https://www.hackerrank.com/challenges/reverse-array-c/problem)|[reverse-array-c.c](c/reverse-array-c.c)|
458+
|Arrays and Strings|Medium|[Printing Tokens](https://www.hackerrank.com/challenges/printing-tokens-/problem)|[printing-tokens-.c](c/printing-tokens-.c)|
459+
|Arrays and Strings|Medium|[Digit Frequency](https://www.hackerrank.com/challenges/frequency-of-digits-1/problem)|[frequency-of-digits-1.c](c/frequency-of-digits-1.c)|
460+
|Arrays and Strings|Medium|[Dynamic Array in C](https://www.hackerrank.com/challenges/dynamic-array-in-c/problem)|[dynamic-array-in-c.c](c/dynamic-array-in-c.c)|
461+
|Functions|Easy|[Calculate the Nth term](https://www.hackerrank.com/challenges/recursion-in-c/problem)|[recursion-in-c.c](c/recursion-in-c.c)|
462+
|Functions|Easy|[Students Marks Sum](https://www.hackerrank.com/challenges/students-marks-sum/problem)|[students-marks-sum.c](c/students-marks-sum.c)|
463+
|Functions|Medium|[Permutations of Strings](https://www.hackerrank.com/challenges/permutations-of-strings/problem)|[permutations-of-strings.c](c/permutations-of-strings.c)|
464+
|Functions|Medium|[Variadic functions in C](https://www.hackerrank.com/challenges/variadic-functions-in-c/problem)|[variadic-functions-in-c.c](c/variadic-functions-in-c.c)|
465+
|Functions|Hard|[Sorting Array of Strings](https://www.hackerrank.com/challenges/sorting-array-of-strings/problem)|[sorting-array-of-strings.c](c/sorting-array-of-strings.c)|
466+
|Structs and Enums|Easy|[Boxes through a Tunnel](https://www.hackerrank.com/challenges/too-high-boxes/problem)|[too-high-boxes.c](c/too-high-boxes.c)|
467+
|Structs and Enums|Medium|[Small Triangles, Large Triangles](https://www.hackerrank.com/challenges/small-triangles-large-triangles/problem)|[small-triangles-large-triangles.c](c/small-triangles-large-triangles.c)|
468+
|Structs and Enums|Hard|[Post Transition](https://www.hackerrank.com/challenges/post-transition/problem)|[post-transition.c](c/post-transition.c)|

c/1d-arrays-in-c.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <math.h>
4+
#include <stdlib.h>
5+
6+
int main() {
7+
int n;
8+
int sum = 0;
9+
int *arr;
10+
11+
scanf("%d", &n);
12+
arr = (int*) malloc(n * sizeof(int));
13+
14+
for (int i = 0; i < n; i++)
15+
scanf("%d", &arr[i]);
16+
17+
for (int i = 0; i < n; i++)
18+
sum += arr[i];
19+
20+
printf("%d\n", sum);
21+
22+
free(arr);
23+
return 0;
24+
}

c/bitwise-operators-in-c.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <math.h>
4+
#include <stdlib.h>
5+
6+
#define max(a,b) \
7+
({ __typeof__ (a) _a = (a); \
8+
__typeof__ (b) _b = (b); \
9+
_a > _b ? _a : _b; })
10+
11+
void calculate_the_maximum(int n, int k) {
12+
int res = 0;
13+
14+
for (int i = 1; i < n; i++) {
15+
for (int j = i+1; j < n+1; j++) {
16+
int tmp = i & j;
17+
if ((tmp < k) && (res < tmp))
18+
res = tmp;
19+
}
20+
}
21+
printf("%d\n", res);
22+
res = 0;
23+
24+
for (int i = 1; i < n; i++) {
25+
for (int j = i+1; j < n+1; j++) {
26+
int tmp = i | j;
27+
if ((tmp < k) && (res < tmp))
28+
res = tmp;
29+
}
30+
}
31+
printf("%d\n", res);
32+
res = 0;
33+
34+
for (int i = 1; i < n; i++) {
35+
for (int j = i+1; j < n+1; j++) {
36+
int tmp = i ^ j;
37+
if ((tmp < k) && (res < tmp))
38+
res = tmp;
39+
}
40+
}
41+
printf("%d\n", res);
42+
}
43+
44+
int main() {
45+
int n, k;
46+
47+
scanf("%d %d", &n, &k);
48+
calculate_the_maximum(n, k);
49+
50+
return 0;
51+
}

c/conditional-statements-in-c.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <assert.h>
2+
#include <limits.h>
3+
#include <math.h>
4+
#include <stdbool.h>
5+
#include <stddef.h>
6+
#include <stdint.h>
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
#include <string.h>
10+
11+
char* readline();
12+
13+
int main()
14+
{
15+
char* n_endptr;
16+
char* n_str = readline();
17+
const char *numbers[9] = {"one", "two", "three", "four", "five",
18+
"six", "seven", "eight", "nine"};
19+
int n = strtol(n_str, &n_endptr, 10);
20+
21+
if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); }
22+
23+
if ((n > 0) && (n < 10))
24+
printf("%s\n", numbers[n-1]);
25+
else
26+
printf("Greater than 9\n");
27+
28+
return 0;
29+
}
30+
31+
char* readline() {
32+
size_t alloc_length = 1024;
33+
size_t data_length = 0;
34+
char* data = malloc(alloc_length);
35+
36+
while (true) {
37+
char* cursor = data + data_length;
38+
char* line = fgets(cursor, alloc_length - data_length, stdin);
39+
40+
if (!line) { break; }
41+
42+
data_length += strlen(cursor);
43+
44+
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; }
45+
46+
size_t new_length = alloc_length << 1;
47+
data = realloc(data, new_length);
48+
49+
if (!data) { break; }
50+
51+
alloc_length = new_length;
52+
}
53+
54+
if (data[data_length - 1] == '\n') {
55+
data[data_length - 1] = '\0';
56+
}
57+
58+
data = realloc(data, data_length);
59+
60+
return data;
61+
}

c/dynamic-array-in-c.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
int main()
2+
{
3+
int total_number_of_shelves;
4+
scanf("%d", &total_number_of_shelves);
5+
6+
int total_number_of_queries;
7+
scanf("%d", &total_number_of_queries);
8+
9+
total_number_of_books = (int *) malloc(total_number_of_shelves * sizeof(int));
10+
total_number_of_pages = (int **) malloc(total_number_of_shelves * sizeof(int *));
11+
12+
while (total_number_of_queries--) {
13+
int type_of_query;
14+
scanf("%d", &type_of_query);
15+
16+
if (type_of_query == 1) {
17+
int x, y;
18+
scanf("%d %d", &x, &y);
19+
total_number_of_books[x]++;
20+
total_number_of_pages[x] = (int *) realloc(total_number_of_pages[x],
21+
total_number_of_books[x] * sizeof(int));
22+
total_number_of_pages[x][total_number_of_books[x]-1] = y;

c/for-loop-in-c.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <math.h>
4+
#include <stdlib.h>
5+
6+
7+
8+
int main()
9+
{
10+
int a, b;
11+
const char *numbers[9] = {"one", "two", "three", "four", "five",
12+
"six", "seven", "eight", "nine"};
13+
scanf("%d\n%d", &a, &b);
14+
15+
for (int i = a; i <= b; i++) {
16+
if ((i > 0) && (i < 10))
17+
printf("%s\n", numbers[i-1]);
18+
else
19+
printf("%s\n", (i%2) ? "odd" : "even");
20+
}
21+
22+
return 0;
23+
}
24+

c/frequency-of-digits-1.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <math.h>
4+
#include <stdlib.h>
5+
6+
int main() {
7+
int freq[10];
8+
char string[1000];
9+
10+
scanf("%s", &string);
11+
12+
for (int i = 0; i < 10; i++)
13+
freq[i] = 0;
14+
15+
for (int i = 0; i < strlen(string); i++) {
16+
int pos = string[i] - '0';
17+
if (pos >= 0 && pos < 10)
18+
freq[pos]++;
19+
}
20+
21+
for (int i = 0; i < 10; i++)
22+
printf("%d ", freq[i]);
23+
24+
return 0;
25+
}

c/functions-in-c.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <stdio.h>
2+
3+
int max_of_four(int a, int b, int c, int d)
4+
{
5+
int res = a;
6+
7+
if (b > res)
8+
res = b;
9+
if (c > res)
10+
res = c;
11+
if (d > res)
12+
res = d;
13+
14+
return res;
15+
}
16+
17+
18+
int main() {
19+
int a, b, c, d;
20+
scanf("%d %d %d %d", &a, &b, &c, &d);
21+
int ans = max_of_four(a, b, c, d);
22+
printf("%d", ans);
23+
24+
return 0;
25+
}

c/hello-world-c.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <math.h>
4+
#include <stdlib.h>
5+
6+
int main()
7+
{
8+
9+
char s[100];
10+
scanf("%[^\n]%*c", &s);
11+
12+
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
13+
printf("Hello, World!\n%s", s);
14+
return 0;
15+
}

c/permutations-of-strings.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
int lexicographic_sort(const void *a, const void *b) {
6+
const char **ia = (const char **)a;
7+
const char **ib = (const char **)b;
8+
return strcmp(*ia, *ib);
9+
}
10+
11+
/* A utility function two swap two strings a and b */
12+
void swap(char **a, char **b) {
13+
char *t = *a;
14+
*a = *b;
15+
*b = t;
16+
}
17+
18+
/*
19+
* This function finds the index of the smallest string
20+
* which is greater than 'first' and is present in s[l..h]
21+
*/
22+
int find_ceil(char **s, char *first, int l, int h) {
23+
/* initialize index of ceiling element */
24+
int ceil_index = l;
25+
26+
/*
27+
* Now iterate through rest of the elements and find
28+
* the smallest character greater than 'first'
29+
*/
30+
31+
for (int i = l+1; i <= h; i++)
32+
if ((strcmp(s[i], first) > 0) && (strcmp(s[i], s[ceil_index]) < 0))
33+
ceil_index = i;
34+
35+
return ceil_index;
36+
}
37+
38+
int next_permutation(int n, char **s) {
39+
int theres_more = 1;
40+
int i;
41+
42+
/*
43+
* Find the rightmost string which is smaller than its next
44+
* character. Let us call it 'first str'
45+
*/
46+
for (i = n-2; i >= 0; i--)
47+
if (strcmp(s[i], s[i+1]) < 0)
48+
break;
49+
50+
/*
51+
* If there is no such string, all are sorted in decreasing order,
52+
* means we just found the last permutation and we are done.
53+
*/
54+
if (i == -1) {
55+
theres_more = 0;
56+
} else {
57+
/*
58+
* Find the ceil of 'first char' in right of first character.
59+
* Ceil of a character is the smallest character greater than it
60+
*/
61+
int ceil_index = find_ceil(s, s[i], i + 1, n - 1);
62+
63+
/* Swap first and second strings */
64+
swap(&s[i], &s[ceil_index]);
65+
66+
/* Sort the string on right of 'first char' */
67+
qsort(s + i + 1, n - i - 1, sizeof(s[0]), lexicographic_sort);
68+
}
69+
70+
return theres_more;
71+
}

0 commit comments

Comments
 (0)