0% found this document useful (0 votes)
3 views

ds 3

The document contains a C program that implements a function 'twoStacks' to determine the maximum number of elements that can be taken from two stacks without exceeding a specified sum. It includes helper functions for reading input, trimming strings, splitting strings, and parsing integers. The main function orchestrates reading input values, calling 'twoStacks', and outputting the result.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

ds 3

The document contains a C program that implements a function 'twoStacks' to determine the maximum number of elements that can be taken from two stacks without exceeding a specified sum. It includes helper functions for reading input, trimming strings, splitting strings, and parsing integers. The main function orchestrates reading input values, calling 'twoStacks', and outputting the result.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

#include <assert.

h>

#include <ctype.h>

#include <limits.h>

#include <math.h>

#include <stdbool.h>

#include <stddef.h>

#include <stdint.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

Char* readline();

Char* ltrim(char*);

Char* rtrim(char*);
Char** split_string(char*);

Int parse_int(char*);

/*

 Complete the ‘twoStacks’ function below.

 The function is expected to return an INTEGER.

 The function accepts following parameters:

 1. INTEGER maxSum

 2. INTEGER_ARRAY a

 3. INTEGER_ARRAY b
*/

Int twoStacks(int maxSum, int a_count, int* a, int b_count, int* b) {

Int sum = 0, count = 0;

Int i = 0;

// Add elements from stack a as long as the sum doesn’t exceed maxSum

While (i < a_count && sum + a[i] ≤ maxSum) {

Sum += a[i];

I++;

Count++;

// Now try adding elements from stack b


Int j = 0;

Int maxCount = count; // Store the current maximum number of elements


removed

While (j < b_count && i ≥ 0) {

Sum += b[j];

J++;

// If the sum exceeds maxSum, start removing elements from stack a

While (sum > maxSum && i > 0) {

i--;

sum -= a[i]; // Remove the most recently taken element from stack a

}
//pehle a se sum nikaalke check kiya,,then usme b ke ele add kiye if working
then good wrna a ke ele remove kiye … jab tak i 0 nahi hota tab tak check
karenge kyuki tab tak sum ko maxSum se kam ya equal rakhne ka manage
kr skte hai kuch na kuch krke but if i 0 ho gaya to koi a ka ele remove nahi ho
skta to fir aur sum manage nahi ho skta maxSum se chota rakhne ka

// If valid (sum ≤ maxSum), update the maximum count

If (sum ≤ maxSum) {

maxCount = (i + j > maxCount) ? i + j : maxCount;

Return maxCount;

}
Int main()

FILE* fptr = fopen(getenv(“OUTPUT_PATH”), “w”);

Int g = parse_int(ltrim(rtrim(readline())));

For (int g_itr = 0; g_itr < g; g_itr++) {

Char** first_multiple_input = split_string(rtrim(readline()));

Int n = parse_int(*(first_multiple_input + 0));

Int m = parse_int(*(first_multiple_input + 1));


Int maxSum = parse_int(*(first_multiple_input + 2));

Char** a_temp = split_string(rtrim(readline()));

Int* a = malloc(n * sizeof(int));

For (int i = 0; i < n; i++) {

Int a_item = parse_int(*(a_temp + i));

*(a + i) = a_item;

Char** b_temp = split_string(rtrim(readline()));


Int* b = malloc(m * sizeof(int));

For (int i = 0; i < m; i++) {

Int b_item = parse_int(*(b_temp + i));

*(b + i) = b_item;

Int result = twoStacks(maxSum, n, a, m, b);

Fprintf(fptr, “%d\n”, result);

}
Fclose(fptr);

Return 0;

Char* readline() {

Size_t alloc_length = 1024;

Size_t data_length = 0;

Char* data = malloc(alloc_length);

While (true) {

Char* cursor = data + data_length;

Char* line = fgets(cursor, alloc_length – data_length, stdin);


If (!line) {

Break;

Data_length += strlen(cursor);

If (data_length < alloc_length – 1 || data[data_length – 1] == ‘\n’) {

Break;

Alloc_length ≪= 1;
Data = realloc(data, alloc_length);

If (!data) {

Data = ‘\0’;

Break;

If (data[data_length – 1] == ‘\n’) {

Data[data_length – 1] = ‘\0’;

Data = realloc(data, data_length);


If (!data) {

Data = ‘\0’;

} else {

Data = realloc(data, data_length + 1);

If (!data) {

Data = ‘\0’;

} else {

Data[data_length] = ‘\0’;

}
Return data;

Char* ltrim(char* str) {

If (!str) {

Return ‘\0’;

If (!*str) {

Return str;

While (*str != ‘\0’ && isspace(*str)) {

Str++;
}

Return str;

Char* rtrim(char* str) {

If (!str) {

Return ‘\0’;

If (!*str) {

Return str;

}
Char* end = str + strlen(str) – 1;

While (end ≥ str && isspace(*end)) {

End--;

*(end + 1) = ‘\0’;

Return str;

Char** split_string(char* str) {

Char** splits = NULL;


Char* token = strtok(str, “ “);

Int spaces = 0;

While (token) {

Splits = realloc(splits, sizeof(char*) * ++spaces);

If (!splits) {

Return splits;

Splits[spaces – 1] = token;
Token = strtok(NULL, “ “);

Return splits;

Int parse_int(char* str) {

Char* endptr;

Int value = strtol(str, &endptr, 10);

If (endptr == str || *endptr != ‘\0’) {

Exit(EXIT_FAILURE);

}
Return value;

You might also like