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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions Submission/Shivam Tripathi/Add Binary/solution.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* addBinary(char* a, char* b) {
int lenA = strlen(a);
int lenB = strlen(b);
int maxLen = lenA > lenB ? lenA : lenB;


char* result = (char*)malloc(maxLen + 2);
result[maxLen + 1] = '\0';

int i = lenA - 1;
int j = lenB - 1;
int k = maxLen;
int carry = 0;

while (i >= 0 || j >= 0 || carry > 0) {
int sum = carry;
if (i >= 0) sum += a[i--] - '0';
if (j >= 0) sum += b[j--] - '0';

result[k--] = (sum % 2) + '0';
carry = sum / 2;
}


char* finalResult = result + k + 1;


if (*finalResult == '\0') {

finalResult = result + maxLen;
finalResult[0] = '0';
finalResult[1] = '\0';
}

return finalResult;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions Submission/Shivam Tripathi/Power of Two/solution.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdbool.h>

bool isPowerOfTwo(int n) {
if (n <= 0) {
return false;
}
return (n & (n - 1)) == 0;
}