Skip to content

Commit f8839ca

Browse files
committed
Add ManasaAndStones.c implementation.
Implements the solution for the "Manasa and Stones" problem, including functions for calculating possible stone values and input/output handling. The program supports multiple test cases and handles edge cases like equal step values.
1 parent 8cedae5 commit f8839ca

File tree

1 file changed

+212
-0
lines changed

1 file changed

+212
-0
lines changed

Diff for: ManasaAndStones.c

+212
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
#include <assert.h>
2+
#include <ctype.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+
char* ltrim(char*);
14+
char* rtrim(char*);
15+
16+
int parse_int(char*);
17+
18+
/*
19+
* Complete the 'stones' function below.
20+
*
21+
* The function is expected to return an INTEGER_ARRAY.
22+
* The function accepts following parameters:
23+
* 1. INTEGER n
24+
* 2. INTEGER a
25+
* 3. INTEGER b
26+
*/
27+
28+
/*
29+
* To return the integer array from the function, you should:
30+
* - Store the size of the array to be returned in the result_count variable
31+
* - Allocate the array statically or dynamically
32+
*
33+
* For example,
34+
* int* return_integer_array_using_static_allocation(int* result_count) {
35+
* *result_count = 5;
36+
*
37+
* static int a[5] = {1, 2, 3, 4, 5};
38+
*
39+
* return a;
40+
* }
41+
*
42+
* int* return_integer_array_using_dynamic_allocation(int* result_count) {
43+
* *result_count = 5;
44+
*
45+
* int *a = malloc(5 * sizeof(int));
46+
*
47+
* for (int i = 0; i < 5; i++) {
48+
* *(a + i) = i + 1;
49+
* }
50+
*
51+
* return a;
52+
* }
53+
*
54+
*/
55+
int* stones(int n, int a, int b, int* result_count) {
56+
// Ensure a <= b to simplify calculations
57+
if (a > b) {
58+
int temp = a;
59+
a = b;
60+
b = temp;
61+
}
62+
63+
// If a == b, there's only one possible value
64+
if (a == b) {
65+
*result_count = 1;
66+
int* result = malloc(sizeof(int));
67+
result[0] = (n - 1) * a;
68+
return result;
69+
}
70+
71+
// Array to hold results
72+
*result_count = n;
73+
int* result = malloc(*result_count * sizeof(int));
74+
75+
// Calculate unique final values
76+
for (int i = 0; i < n; i++) {
77+
result[i] = (n - 1 - i) * a + i * b;
78+
}
79+
80+
return result; // Return array and result count
81+
}
82+
83+
int main()
84+
{
85+
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
86+
87+
int T = parse_int(ltrim(rtrim(readline())));
88+
89+
for (int T_itr = 0; T_itr < T; T_itr++) {
90+
int n = parse_int(ltrim(rtrim(readline())));
91+
92+
int a = parse_int(ltrim(rtrim(readline())));
93+
94+
int b = parse_int(ltrim(rtrim(readline())));
95+
96+
int result_count;
97+
int* result = stones(n, a, b, &result_count);
98+
99+
for (int i = 0; i < result_count; i++) {
100+
fprintf(fptr, "%d", *(result + i));
101+
102+
if (i != result_count - 1) {
103+
fprintf(fptr, " ");
104+
}
105+
}
106+
107+
fprintf(fptr, "\n");
108+
}
109+
110+
fclose(fptr);
111+
112+
return 0;
113+
}
114+
115+
char* readline() {
116+
size_t alloc_length = 1024;
117+
size_t data_length = 0;
118+
119+
char* data = malloc(alloc_length);
120+
121+
while (true) {
122+
char* cursor = data + data_length;
123+
char* line = fgets(cursor, alloc_length - data_length, stdin);
124+
125+
if (!line) {
126+
break;
127+
}
128+
129+
data_length += strlen(cursor);
130+
131+
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') {
132+
break;
133+
}
134+
135+
alloc_length <<= 1;
136+
137+
data = realloc(data, alloc_length);
138+
139+
if (!data) {
140+
data = '\0';
141+
142+
break;
143+
}
144+
}
145+
146+
if (data[data_length - 1] == '\n') {
147+
data[data_length - 1] = '\0';
148+
149+
data = realloc(data, data_length);
150+
151+
if (!data) {
152+
data = '\0';
153+
}
154+
} else {
155+
data = realloc(data, data_length + 1);
156+
157+
if (!data) {
158+
data = '\0';
159+
} else {
160+
data[data_length] = '\0';
161+
}
162+
}
163+
164+
return data;
165+
}
166+
167+
char* ltrim(char* str) {
168+
if (!str) {
169+
return '\0';
170+
}
171+
172+
if (!*str) {
173+
return str;
174+
}
175+
176+
while (*str != '\0' && isspace(*str)) {
177+
str++;
178+
}
179+
180+
return str;
181+
}
182+
183+
char* rtrim(char* str) {
184+
if (!str) {
185+
return '\0';
186+
}
187+
188+
if (!*str) {
189+
return str;
190+
}
191+
192+
char* end = str + strlen(str) - 1;
193+
194+
while (end >= str && isspace(*end)) {
195+
end--;
196+
}
197+
198+
*(end + 1) = '\0';
199+
200+
return str;
201+
}
202+
203+
int parse_int(char* str) {
204+
char* endptr;
205+
int value = strtol(str, &endptr, 10);
206+
207+
if (endptr == str || *endptr != '\0') {
208+
exit(EXIT_FAILURE);
209+
}
210+
211+
return value;
212+
}

0 commit comments

Comments
 (0)