Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions Problem-Solving-Basic/active-traders/active-traders.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
* Complete the 'mostActive' function below.
*
* The function is expected to return a STRING_ARRAY.
* The function accepts STRING_ARRAY customers as parameter.
*/

/*
* Algorithm:
* 1. Count the number of transactions for each unique customer:
* a. Iterate through all customer transactions
* b. For each customer, check if we've seen them before
* c. If not, add them to our unique customers list
* d. If yes, increment their transaction count
* 2. Filter customers with at least 5% of the total transactions:
* a. Calculate the threshold: 5% of total transactions
* b. Include only customers whose transaction count exceeds this threshold
* 3. Sort the filtered list of active traders alphabetically
* 4. Return the sorted list of active traders
*
* Time Complexity:
* - O(n²) for counting unique customers in the worst case
* - O(m log m) for sorting the active traders list
* Where n is the total number of transactions, m is the number of unique customers
*
* Space Complexity: O(m) where m is the number of unique customers
*/

// Structure to store customer information
typedef struct
{
char *name;
int count;
} CustomerInfo;

// Comparison function for qsort (for alphabetical sorting)
int compareCustomers(const void *a, const void *b)
{
return strcmp(((CustomerInfo *)a)->name, ((CustomerInfo *)b)->name);
}

char **mostActive(int customers_count, char **customers, int *result_count)
{
const int MAX_CUSTOMERS = 10000; // Maximum number of unique customers
CustomerInfo *unique_customers = (CustomerInfo *)malloc(MAX_CUSTOMERS * sizeof(CustomerInfo));
int unique_count = 0;

// Count occurrences of each customer
for (int i = 0; i < customers_count; i++)
{
// Check if customer already exists in our array
int found = 0;
for (int j = 0; j < unique_count; j++)
{
if (strcmp(unique_customers[j].name, customers[i]) == 0)
{
unique_customers[j].count++;
found = 1;
break;
}
}

// If not found, add as a new customer
if (!found)
{
unique_customers[unique_count].name = strdup(customers[i]);
unique_customers[unique_count].count = 1;
unique_count++;
}
}

// Create result array
char **result = (char **)malloc(unique_count * sizeof(char *));
*result_count = 0;

// Filter customers with >= 5% of transactions
float threshold = 0.05 * customers_count;
for (int i = 0; i < unique_count; i++)
{
if (unique_customers[i].count >= threshold)
{
result[*result_count] = strdup(unique_customers[i].name);
(*result_count)++;
}
}

// Create a new array with just the active traders for sorting
CustomerInfo *active_traders = (CustomerInfo *)malloc(*result_count * sizeof(CustomerInfo));
for (int i = 0; i < *result_count; i++)
{
active_traders[i].name = result[i];
}

// Sort active traders alphabetically
qsort(active_traders, *result_count, sizeof(CustomerInfo), compareCustomers);

// Update result array with sorted names
for (int i = 0; i < *result_count; i++)
{
result[i] = active_traders[i].name;
}

// Free memory
for (int i = 0; i < unique_count; i++)
{
free(unique_customers[i].name);
}
free(unique_customers);
free(active_traders);

return result;
}

int main()
{
FILE *fptr = fopen(getenv("OUTPUT_PATH"), "w");

int customers_count;
scanf("%d", &customers_count);

char **customers = malloc(customers_count * sizeof(char *));
for (int i = 0; i < customers_count; i++)
{
customers[i] = malloc(101 * sizeof(char)); // Assuming max length of 100
scanf("%s", customers[i]);
}

int result_count;
char **result = mostActive(customers_count, customers, &result_count);

for (int i = 0; i < result_count; i++)
{
fprintf(fptr, "%s\n", result[i]);
free(result[i]);
}
fprintf(fptr, "\n");

for (int i = 0; i < customers_count; i++)
{
free(customers[i]);
}
free(customers);
free(result);

fclose(fptr);

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

/*
* Complete the 'mostBalancedPartition' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER_ARRAY parent
* 2. INTEGER_ARRAY files_size
*/

/*
* Algorithm:
* 1. Calculate the total size of each subtree in the file system tree:
* a. Use a recursive approach to sum up file sizes
* b. For each node, its total size = its own size + sizes of all child subtrees
* 2. Find the optimal partition point by:
* a. The system is divided into two parts by removing one edge from the tree
* b. When edge to node i is cut, we get subtree i and the rest of the tree
* c. Size of subtree i is size_sums[i]
* d. Size of the rest of the tree is total_size - size_sums[i]
* 3. Calculate the absolute difference between these two parts for each possible cut
* 4. Return the minimum difference found
*
* Time Complexity: O(n) where n is the number of nodes in the tree
* Space Complexity: O(n) for storing the size sums
*/

// Recursive function to calculate size sums
long size_sums_rec(int node, int *parent, int *files_size, long *size_sums, int parent_count)
{
// Calculate children first
for (int i = 0; i < parent_count; i++)
{
if (parent[i] == node)
{
size_sums[i] = size_sums_rec(i, parent, files_size, size_sums, parent_count);
}
}

// Sum up this node's size plus all its children's sizes
long sum = files_size[node];
for (int i = 0; i < parent_count; i++)
{
if (parent[i] == node)
{
sum += size_sums[i];
}
}

size_sums[node] = sum;
return sum;
}

int mostBalancedPartition(int parent_count, int *parent, int files_size_count, int *files_size)
{
// Allocate and initialize size_sums array
long *size_sums = (long *)malloc(parent_count * sizeof(long));
for (int i = 0; i < parent_count; i++)
{
size_sums[i] = -1; // Initialize with -1 to indicate not calculated yet
}

// Calculate size sums using recursion
size_sums_rec(0, parent, files_size, size_sums, parent_count);

// Find the minimum absolute difference
long total_size = size_sums[0];
long min_diff = LONG_MAX;

for (int i = 1; i < parent_count; i++)
{
long diff = labs(total_size - 2 * size_sums[i]);
if (diff < min_diff)
{
min_diff = diff;
}
}

free(size_sums);
return (int)min_diff;
}

int main()
{
FILE *fptr = fopen(getenv("OUTPUT_PATH"), "w");

int parent_count;
scanf("%d", &parent_count);

int *parent = malloc(parent_count * sizeof(int));
for (int i = 0; i < parent_count; i++)
{
scanf("%d", &parent[i]);
}

int files_size_count;
scanf("%d", &files_size_count);

int *files_size = malloc(files_size_count * sizeof(int));
for (int i = 0; i < files_size_count; i++)
{
scanf("%d", &files_size[i]);
}

int result = mostBalancedPartition(parent_count, parent, files_size_count, files_size);

fprintf(fptr, "%d\n", result);

free(parent);
free(files_size);
fclose(fptr);

return 0;
}
114 changes: 114 additions & 0 deletions Problem-Solving-Basic/longest-subarray/longest-subarray.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include <stdio.h>
#include <stdlib.h>

/*
* Complete the 'longestSubarray' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY arr as parameter.
*/

/*
* Algorithm:
* 1. For each possible starting position:
* a. Try to form a subarray with at most 2 distinct values
* b. Additionally, the two values must differ by at most 1
* 2. Use a window expansion approach:
* a. Track distinct values seen so far (at most 2)
* b. When a new value is encountered, check if it can be added:
* i. If we have 0 values so far, add it
* ii. If we have 1 value so far, add it if |new_val - existing_val| ≤ 1
* iii. If we have 2 values already, check if the new value matches either
* c. If a value can't be added, the window can't be expanded further
* 3. Track the maximum valid window length
*
* Time Complexity: O(n²) where n is the array length
* Space Complexity: O(1) additional space
*/

int longestSubarray(int arr_count, int *arr)
{
int ans = 0;

// O(n^2) is okay because of constraints.
for (int i = 0; i < arr_count; i++)
{
int w[2] = {-1, -1}; // Stores the two distinct values (-1 means empty)
int w_count = 0; // Number of distinct values stored
int cnt = 0; // Length of current subarray

for (int j = i; j < arr_count; j++)
{
// Check if current element matches either of the two values
if (w_count > 0 && arr[j] == w[0])
{
cnt++;
continue;
}

if (w_count > 1 && arr[j] == w[1])
{
cnt++;
continue;
}

// If we don't have any value yet, add this as first
if (w_count == 0)
{
w[0] = arr[j];
w_count = 1;
cnt++;
}
// If we have only one value, check if new value can be added
else if (w_count == 1)
{
if (abs(w[0] - arr[j]) <= 1)
{
w[1] = arr[j];
w_count = 2;
cnt++;
}
else
{
// If difference > 1, we can't add this element
break;
}
}
// If we already have two values, and this is a third distinct value, break
else
{
break;
}
}

// Update answer with max subarray length found
if (cnt > ans)
{
ans = cnt;
}
}

return ans;
}

int main()
{
FILE *fptr = fopen(getenv("OUTPUT_PATH"), "w");
int arr_count;
scanf("%d", &arr_count);

int *arr = malloc(arr_count * sizeof(int));
for (int i = 0; i < arr_count; i++)
{
scanf("%d", &arr[i]);
}

int result = longestSubarray(arr_count, arr);

fprintf(fptr, "%d\n", result);

free(arr);
fclose(fptr);

return 0;
}
Loading