Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Find the length of largest subarray with equal number of 0's and 1's using recursion
#include <stdio.h> #include <stdbool.h> #include <stdlib.h> #include <time.h> struct calcul_data { size_t zero; size_t one; }; struct calcul_data calcul(bool *values, size_t zero, size_t one, size_t i, size_t n) { if (i < n) { if (values[i]) { return calcul(values, zero, one + 1, i + 1, n); } else { return calcul(values, zero + 1, one, i + 1, n); } } return (struct calcul_data){.zero = zero, .one = one}; } struct magic_data { struct calcul_data data; size_t i; size_t n; }; struct magic_data magic(bool *values, size_t i, size_t n) { if (i < n) { struct calcul_data data = calcul(values, 0, 0, i, n); if (data.zero == data.one) { return (struct magic_data){.data = data, .i = i, .n = n}; } struct magic_data a = magic(values, i + 1, n); struct magic_data b = magic(values, i, n - 1); return a.n - a.i > b.n - b.i ? a : b; } return (struct magic_data){0}; } int main(void) { srand(time(NULL)); bool values[16]; size_t n = sizeof values / sizeof *values; for (size_t i = 0; i < n; i++) { if (rand() % 2) { values[i] = true; printf("true, "); } else { values[i] = false; printf("false, "); } } printf("\n"); struct magic_data data = magic(values, 0, n); printf("zero = %zu, one = %zu, i = %zu, n = %zu\n", data.data.zero, data.data.one, data.i, data.n); }
run
|
edit
|
history
|
help
0
Numero pi Euler
Celcius to Farenheir,Romer,Kelvin
VKI_Mihalyk_3_2
UDP - Pytagorova veta
fun kce
Bit string printer
Bucles: Triángulo de asteriscos lateral izquierdo
C
C - Append Character to String
Vectores: Llena vector con números al azar