~saurabhs/binarysearchdotcom

35b1b73e55a0a3086008c70b6319cc844eba35d2 — Saurabh S 1 year, 1 month ago f6e67c2
More solved problems
A AddBinaryNumbers/main.cpp => AddBinaryNumbers/main.cpp +34 -0
@@ 0,0 1,34 @@
#include <algorithm>
#include <string>

std::string add(std::string l, std::string s, const int diff) {
  if (s == "0")
    return l;

  std::string result{};
  int s_len = s.length();
  int carry = 0;

  for (int i = s_len - 1; i >= 0; i--) {
    int si = s[i] - '0', li = l[i + diff] - '0';
    int a = (si ^ li) ^ carry;
    carry = (si & li) | (carry & si) | (carry & li);
    result += std::to_string(a);
  }
  for (int i = l.length() - s_len - 1; i >= 0; i--) {
    int li = l[i] - '0';
    int a = li ^ carry;
    carry = li & carry;
    result += std::to_string(a);
  }
  if (carry)
    result += std::to_string((int)carry);

  std::reverse(result.begin(), result.end());
  return result;
}

std::string solve(std::string a, std::string b) {
  int diff = a.length() - b.length();
  return diff > 0 ? add(a, b, diff) : add(b, a, -diff);
}
\ No newline at end of file

A AddBinaryNumbers/tests.cpp => AddBinaryNumbers/tests.cpp +28 -0
@@ 0,0 1,28 @@
#define CATCH_CONFIG_MAIN
#include "../catch2/catch.hpp"
#include "main.cpp"

TEST_CASE("Small Binary Add") {
  REQUIRE(solve("0", "0") == "0");
  REQUIRE(solve("1", "1") == "10");
  REQUIRE(solve("11", "11") == "110");
  REQUIRE(solve("1", "111") == "1000");
  REQUIRE(solve("1", "1111") == "10000");
}

TEST_CASE("Medium Binary Add") {
  REQUIRE(solve("10000", "10000") == "100000");
  REQUIRE(solve("111", "111") == "1110");
  REQUIRE(solve("1010", "1011") == "10101");
}

TEST_CASE("Very Large Binary Add") {
  REQUIRE(solve("0", "1100111000100") == "1100111000100");
  REQUIRE(solve("1", "1111111111111") == "10000000000000");
  REQUIRE(solve("1", "1100000000000") == "1100000000001");
  REQUIRE(solve("1", "1100111000100") == "1100111000101");
  REQUIRE(solve("11111111111111111111111111111111111111111111111111111111111111"
                "111111111111111111",
                "1") == "100000000000000000000000000000000000000000000000000000"
                        "000000000000000000000000000");
}

A BalancedBracketsSequel/main.cpp => BalancedBracketsSequel/main.cpp +18 -0
@@ 0,0 1,18 @@
#include <stack>
#include <string>

bool solve(std::string s) {
  std::stack<char> st{};
  for (const auto &c : s) {
    if (c == '(' || c == '[' || c == '{')
      st.push(c);
    else {
      if (c == ')' && st.top() == '(' || c == ']' && st.top() == '[' ||
          c == '}' && st.top() == '{')
        st.pop();
      else
        return false;
    }
  }
  return st.empty();
}
\ No newline at end of file

A BalancedBracketsSequel/tests.cpp => BalancedBracketsSequel/tests.cpp +11 -0
@@ 0,0 1,11 @@
#define CATCH_CONFIG_MAIN
#include "../catch2/catch.hpp"
#include "main.cpp"

TEST_CASE("Balanced Brackets", "[solve]")
{
    REQUIRE(!solve("[(])"));
    REQUIRE(solve("([])[]({})"));
    REQUIRE(!solve("[())))"));
    REQUIRE(!solve("(])"));
}
\ No newline at end of file

A FirstMissingPositiveSequel/main.cpp => FirstMissingPositiveSequel/main.cpp +17 -0
@@ 0,0 1,17 @@
#include <climits>
#include <set>
#include <vector>

int solve(std::vector<int> &arr) {
  std::set<int> s{};
  for (const auto &n : arr)
    s.insert(n);
  int min = 1;
  for (const auto &n : s) {
    if (n == min)
      min++;
    else
      return min;
  }
  return min;
}
\ No newline at end of file

A FirstMissingPositiveSequel/tests.cpp => FirstMissingPositiveSequel/tests.cpp +23 -0
@@ 0,0 1,23 @@
#define CATCH_CONFIG_MAIN
#include "../catch2/catch.hpp"
#include "main.cpp"

TEST_CASE("Frst Missing") {
  std::vector input = {1, 2};
  REQUIRE(solve(input) == 3);

  input = {2, 3, 4};
  REQUIRE(solve(input) == 1);

  input = {4, 2, 3};
  REQUIRE(solve(input) == 1);

  input = {5, 4, 2, 3};
  REQUIRE(solve(input) == 1);

  input = {5, 1, 2, 3};
  REQUIRE(solve(input) == 4);

  input = {5, 1, 4, 3, 7, 2, 9, 6, 8, 2, 12};
  REQUIRE(solve(input) == 10);
}
\ No newline at end of file