Skip to content

Commit 41a16db

Browse files
authored
Merge pull request larissalages#107 from abdzitter/master
UtopianTree
2 parents 9231eb9 + 126edbc commit 41a16db

File tree

3 files changed

+238
-0
lines changed

3 files changed

+238
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//Problem Link : https://www.hackerrank.com/challenges/anagram/problem
2+
#include <bits/stdc++.h>
3+
4+
using namespace std;
5+
6+
// Complete the anagram function below.
7+
int anagram(string s)
8+
{
9+
long n=s.size(),k=0;
10+
string a,b;
11+
if(n%2==1)
12+
return -1;
13+
else
14+
{
15+
for(int i=0;i<n/2;i++)
16+
{
17+
a[i]=s[i];
18+
}
19+
for(int i=n/2;i<n;i++)
20+
{
21+
b[i]=s[i];
22+
}
23+
for(int i=n/2;i<n;i++)
24+
{
25+
for(int j=0;j<n/2;j++)
26+
{
27+
if(b[i]==a[j])
28+
{
29+
k++;
30+
a[j]='0';
31+
j=n/2;
32+
33+
}
34+
}
35+
}
36+
return (n/2-k);
37+
}
38+
}
39+
40+
int main()
41+
{
42+
ofstream fout(getenv("OUTPUT_PATH"));
43+
44+
int q;
45+
cin >> q;
46+
cin.ignore(numeric_limits<streamsize>::max(), '\n');
47+
48+
for (int q_itr = 0; q_itr < q; q_itr++) {
49+
string s;
50+
getline(cin, s);
51+
52+
int result = anagram(s);
53+
54+
fout << result << "\n";
55+
}
56+
57+
fout.close();
58+
59+
return 0;
60+
}
61+
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
// Complete the utopianTree function below.
14+
int utopianTree(int n)
15+
{
16+
int sum=1;
17+
for(int i=1;i<=n;i++)
18+
{
19+
if(i%2==0)
20+
{
21+
sum=sum+1;
22+
}
23+
else
24+
{
25+
sum=sum*2;
26+
}
27+
}
28+
return sum;
29+
}
30+
31+
int main()
32+
{
33+
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
34+
35+
char* t_endptr;
36+
char* t_str = readline();
37+
int t = strtol(t_str, &t_endptr, 10);
38+
39+
if (t_endptr == t_str || *t_endptr != '\0') { exit(EXIT_FAILURE); }
40+
41+
for (int t_itr = 0; t_itr < t; t_itr++) {
42+
char* n_endptr;
43+
char* n_str = readline();
44+
int n = strtol(n_str, &n_endptr, 10);
45+
46+
if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); }
47+
48+
int result = utopianTree(n);
49+
50+
fprintf(fptr, "%d\n", result);
51+
}
52+
53+
fclose(fptr);
54+
55+
return 0;
56+
}
57+
58+
char* readline() {
59+
size_t alloc_length = 1024;
60+
size_t data_length = 0;
61+
char* data = malloc(alloc_length);
62+
63+
while (true) {
64+
char* cursor = data + data_length;
65+
char* line = fgets(cursor, alloc_length - data_length, stdin);
66+
67+
if (!line) { break; }
68+
69+
data_length += strlen(cursor);
70+
71+
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; }
72+
73+
size_t new_length = alloc_length << 1;
74+
data = realloc(data, new_length);
75+
76+
if (!data) { break; }
77+
78+
alloc_length = new_length;
79+
}
80+
81+
if (data[data_length - 1] == '\n') {
82+
data[data_length - 1] = '\0';
83+
}
84+
85+
data = realloc(data, data_length);
86+
87+
return data;
88+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//Problem Link:https://www.hackerrank.com/challenges/utopian-tree/problem
2+
#include <assert.h>
3+
#include <limits.h>
4+
#include <math.h>
5+
#include <stdbool.h>
6+
#include <stddef.h>
7+
#include <stdint.h>
8+
#include <stdio.h>
9+
#include <stdlib.h>
10+
#include <string.h>
11+
12+
char* readline();
13+
14+
// Complete the utopianTree function below.
15+
int utopianTree(int n)
16+
{
17+
int sum=1;
18+
for(int i=1;i<=n;i++)
19+
{
20+
if(i%2==0)
21+
{
22+
sum=sum+1;
23+
}
24+
else
25+
{
26+
sum=sum*2;
27+
}
28+
}
29+
return sum;
30+
}
31+
32+
int main()
33+
{
34+
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
35+
36+
char* t_endptr;
37+
char* t_str = readline();
38+
int t = strtol(t_str, &t_endptr, 10);
39+
40+
if (t_endptr == t_str || *t_endptr != '\0') { exit(EXIT_FAILURE); }
41+
42+
for (int t_itr = 0; t_itr < t; t_itr++) {
43+
char* n_endptr;
44+
char* n_str = readline();
45+
int n = strtol(n_str, &n_endptr, 10);
46+
47+
if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); }
48+
49+
int result = utopianTree(n);
50+
51+
fprintf(fptr, "%d\n", result);
52+
}
53+
54+
fclose(fptr);
55+
56+
return 0;
57+
}
58+
59+
char* readline() {
60+
size_t alloc_length = 1024;
61+
size_t data_length = 0;
62+
char* data = malloc(alloc_length);
63+
64+
while (true) {
65+
char* cursor = data + data_length;
66+
char* line = fgets(cursor, alloc_length - data_length, stdin);
67+
68+
if (!line) { break; }
69+
70+
data_length += strlen(cursor);
71+
72+
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; }
73+
74+
size_t new_length = alloc_length << 1;
75+
data = realloc(data, new_length);
76+
77+
if (!data) { break; }
78+
79+
alloc_length = new_length;
80+
}
81+
82+
if (data[data_length - 1] == '\n') {
83+
data[data_length - 1] = '\0';
84+
}
85+
86+
data = realloc(data, data_length);
87+
88+
return data;
89+
}

0 commit comments

Comments
 (0)