├── .clang-format ├── .clang-tidy ├── .cmake-format.yaml ├── .gitignore ├── 1_easy_difficulty_level ├── 01_first_reverse.cpp ├── 02_first_factorial.cpp ├── 03_longest_word.cpp ├── 04_letter_changes.cpp ├── 05_simple_adding.cpp ├── 06_letter_capitalize.cpp ├── 07_simple_symbols.cpp ├── 08_check_nums.cpp ├── 09_time_convert.cpp ├── 10_alphabet_soup.cpp ├── 11_ab_check.cpp ├── 12_vowel_count.cpp ├── 13_word_count.cpp ├── 14_ex_oh.cpp ├── 15_palindrome.cpp ├── 16_arith_geo.cpp ├── 17_array_addition_I_v1.cpp ├── 17_array_addition_I_v2.cpp ├── 17_array_addition_I_v3.cpp ├── 18_letter_count.cpp ├── 19_second_greatlow.cpp ├── 20_division_stringified.cpp ├── 21_counting_minutes_i.cpp ├── 22_mean_mode.cpp ├── 23_dash_insert.cpp ├── 24_swap_case.cpp ├── 25_number_addition.cpp ├── 26_third_greatest.cpp ├── 27_powers_of_two.cpp ├── 28_additive_persistence.cpp ├── 29_multiplicative_persistence.cpp ├── 30_off_line_minimum.cpp ├── 31_changing_sequence.cpp ├── 32_overlapping_ranges.cpp ├── 33_superincreasing.cpp ├── 34_hamming_distance.cpp ├── 35_rectangle_area.cpp ├── 36_bitwise_one.cpp ├── 37_other_products.cpp ├── 38_wave_sorting.cpp ├── 39_array_matching.cpp ├── 40_binary_reversal.cpp ├── 41_longest_increasing_sequence.cpp ├── 42_even_pairs.cpp ├── 43_next_palindrome.cpp ├── 44_largest_pair.cpp ├── 45_nonrepeating_character.cpp ├── 46_two_sum.cpp ├── 47_bitwise_two.cpp ├── 48_power_set_count.cpp ├── 49_product_digits.cpp ├── 50_palindrome_creator.cpp ├── 51_basic_roman_numerals.cpp ├── 52_food_distribution.cpp ├── 53_three_sum.cpp ├── 54_correct_path_v1.cpp ├── 54_correct_path_v2.cpp ├── 54_correct_path_v3.cpp ├── 55_scale_balancing.cpp ├── 56_three_numbers.cpp ├── 57_alphabet_searching.cpp ├── 58_time_difference.cpp ├── 59_triangle_row.cpp ├── 60_vowel_square.cpp ├── 61_closest_enemy.cpp ├── 62_closest_enemy_II.cpp ├── 63_number_stream.cpp ├── 64_largest_four.cpp ├── 65_distinct_characters.cpp ├── 66_question_marks.cpp ├── 67_camel_case.cpp ├── 68_ascii_conversion.cpp ├── 69_simple_evens.cpp ├── 70_snake_case.cpp ├── 71_sum_multiplier.cpp ├── 72_string_merge.cpp ├── 73_one_decremented.cpp ├── 74_element_merger.cpp ├── 75_gcf.cpp ├── 76_serial_number.cpp ├── 77_string_periods.cpp ├── 78_palindrome_swapper.cpp ├── 79_remove_brackets.cpp ├── 80_command_line.cpp ├── 81_star_rating.cpp ├── 82_moving_median.cpp ├── 83_group_totals.cpp ├── 84_different_cases.cpp ├── 84_string_changes.cpp ├── 85_fizzbuzz.cpp ├── 85_h_distance.cpp ├── 86_different_cases.cpp ├── 87_h_distance.cpp ├── custom_templates_ex1.cpp ├── dp_find_sets_of_numbers_ex1.cpp ├── k_closest_point_to_the_origin_v1.cpp ├── optimal_binary_search_ex1.cpp ├── singly_linked_lists_ex1.cpp └── vowel_substring.cpp ├── 2_medium_difficulty_level ├── 01_prime_time.cpp ├── 02_run_length.cpp ├── 03_prime_mover.cpp ├── 04_palindrome_two.cpp ├── 05_division.cpp ├── 06_string_scramble.cpp ├── 07_arith_geo_ii.cpp ├── 08_array_addition.cpp ├── 09_binary_converter.cpp ├── 10_letter_count.cpp ├── 11_simple_mode.cpp ├── 12_caesar_cipher.cpp ├── 13_formatted_division.cpp ├── 14_consecutive.cpp ├── 15_counting_minutes.cpp ├── 16_permutation_step.cpp ├── 17_prime_checker.cpp ├── 18_dash_insert_II.cpp ├── 19_swap_ii.cpp ├── 20_number_search.cpp ├── 21_triple_double.cpp ├── 22_bracket_matcher.cpp ├── 23_string_reduction.cpp ├── 24_coin_determiner.cpp ├── 25_threefive_multiples.cpp ├── 26_fibonacci_checker.cpp ├── 27_multiple_brackets.cpp ├── 28_most_free_time.cpp ├── 29_overlapping_rectangles.cpp ├── 30_distinct_list.cpp ├── 31_look_say_sequence.cpp ├── 32_longest_valid_parentheses.cpp ├── 32_number_encoding.cpp ├── 33_stock_picker.cpp ├── 34_max_subarray.cpp ├── 35_missing_digit.cpp ├── 36_k_unique_characters.cpp ├── 37_bitmap_holes_v1.cpp ├── 37_bitmap_holes_v2.cpp ├── 38_symmetric_tree.cpp ├── 39_binary_search_tree_v1.cpp ├── 39_binary_search_tree_v2.cpp ├── 39_binary_search_tree_v3.cpp ├── 40_binary_tree_lca_v1.cpp ├── 40_binary_tree_lca_v2.cpp ├── 41_lru_cache_v1.cpp ├── 41_lru_cache_v2.cpp ├── 41_lru_cache_v3.cpp ├── 42_tree_constructor.cpp ├── 43_array_min_jumps_v1.cpp ├── 43_array_min_jumps_v2.cpp ├── 43_array_min_jumps_v3.cpp ├── 44_nearest_smaller_values.cpp ├── 45_matrix_spiral.cpp ├── 46_word_split_v1.cpp ├── 46_word_split_v2.cpp ├── 47_pair_searching_v1.cpp ├── 47_pair_searching_v2.cpp ├── 48_boggle_solver_v1.cpp ├── 48_boggle_solver_v2.cpp ├── 49_html_elements.cpp ├── 50_missing_digit_II.cpp ├── 51_palindromic_substring.cpp ├── 52_trapping_water.cpp ├── 53_matrix_path_v1.cpp ├── 53_matrix_path_v2.cpp ├── 54_seating_students.cpp ├── 55_longest_matrix_path_v1.cpp ├── 55_longest_matrix_path_v2.cpp ├── 56_min_window_substring_v1.cpp ├── 56_min_window_substring_v2.cpp ├── 57_matrix_chains_v1.cpp ├── 57_matrix_chains_v2.cpp ├── 58_histogram_area.cpp ├── 59_matching_characters.cpp ├── 60_ternary_converter.cpp ├── 61_linear_congruence.cpp ├── 62_formatted_number.cpp ├── 63_largest_row_column_v1.cpp ├── 63_largest_row_column_v2.cpp ├── 64_eight_queens.cpp ├── 65_three_points.cpp ├── 66_character_removal.cpp ├── 67_simple_password.cpp ├── 68_preorder_traversal.cpp ├── 69_string_zigzag.cpp ├── 70_off_binary.cpp ├── 71_longest_consecutive.cpp ├── 72_string_expression.cpp ├── 73_charlie_the_dog.cpp ├── 74_plus_minus.cpp ├── 75_primes.cpp ├── 76_string_calculate.cpp ├── build.bat └── preorder_traversal_iterative_solution_v1.cpp ├── 3_hard_difficulty_level ├── 01_shortest_path_v1.cpp ├── 01_shortest_path_v2.cpp ├── 01_shortest_path_v3.cpp ├── 02_pattern_chaser.cpp ├── 03_calculator_v1.cpp ├── 03_calculator_v2.cpp ├── 04_weighted_path_v1.cpp ├── 04_weighted_path_v2.cpp ├── 04_weighted_path_v3.cpp ├── 05_intersecting_lines.cpp ├── 06_rref_matrix.cpp ├── 07_polynomial_expansion.cpp ├── 08_gas_station.cpp ├── 09_symmetric_matrix.cpp ├── 10_switch_sort_v1.cpp ├── 10_switch_sort_v2.cpp ├── 10_switch_sort_v3.cpp ├── 11_matrix_determinant.cpp ├── 12_queen_check.cpp ├── 13_noughts_determiner.cpp ├── 14_blackjack_highest.cpp ├── 15_transitivity_relations.cpp ├── 16_alphabet_run_encryption_v1.cpp ├── 16_alphabet_run_encryption_v2.cpp ├── 17_knight_jumps.cpp ├── 18_line_ordering.cpp ├── 19_sudoku_quadrant_checker.cpp ├── 20_quick_knight.cpp ├── 21_square_figures.cpp ├── 22_connect_four_winner.cpp ├── 23_array_jumping_v1.cpp ├── 23_array_jumping_v2.cpp ├── 24_matrix_border_v1.cpp ├── 24_matrix_border_v2.cpp ├── 25_roman_numeral_reduction.cpp ├── 26_parallel_sums_v1.cpp ├── 26_parallel_sums_v2.cpp ├── 27_lcs.cpp ├── 28_nim_winner.cpp ├── 29_farthest_nodes.cpp ├── 30_optimal_assignments.cpp ├── 31_kaprekars_constant.cpp ├── 32_hamiltonian_path.cpp ├── 33_vertex_covering.cpp ├── 34_step_walking.cpp ├── 35_pascals_triangle.cpp ├── 36_reverse_polish_notation.cpp ├── 37_max_heap_checker.cpp ├── 38_chessboard_traveling.cpp ├── 39_matching_couples.cpp ├── 40_approaching_fibonacci.cpp ├── 41_simple_sat.cpp ├── 42_array_couples.cpp ├── 43_array_rotation.cpp ├── 44_bracket_combinations_v1.cpp ├── 44_bracket_combinations_v2.cpp ├── 44_bracket_combinations_v3.cpp ├── 45_counting_anagrams.cpp ├── 46_maximal_square.cpp ├── 47_maximal_rectangle.cpp ├── 48_bipartite_matching.cpp ├── 49_pentagonal_number.cpp ├── 50_city_traffic.cpp ├── 50_city_traffic_v2.cpp ├── 51_convex_hull_points.cpp ├── 52_wildcard_characters.cpp ├── 53_tetris_move.cpp ├── 54_wildcards.cpp └── build.bat ├── CMakeLists.txt ├── LICENSE ├── README.md ├── dependencies └── CMakeLists.txt └── include ├── catch.hpp ├── detail └── stl_helper_functions_impl.hpp └── stl_helper_functions.hpp /.clang-format: -------------------------------------------------------------------------------- 1 | AccessModifierOffset: -2 2 | AlignAfterOpenBracket: DontAlign 3 | AlignConsecutiveAssignments: false 4 | AlignConsecutiveDeclarations: false 5 | AlignEscapedNewlines: Left 6 | AlignOperands: true 7 | AlignTrailingComments: false 8 | AllowAllParametersOfDeclarationOnNextLine: false 9 | AllowShortBlocksOnASingleLine: true 10 | AllowShortCaseLabelsOnASingleLine: false 11 | AllowShortFunctionsOnASingleLine: All 12 | AllowShortIfStatementsOnASingleLine: true 13 | AllowShortLoopsOnASingleLine: true 14 | AlwaysBreakAfterDefinitionReturnType: None 15 | AlwaysBreakAfterReturnType: None 16 | AlwaysBreakBeforeMultilineStrings: true 17 | AlwaysBreakTemplateDeclarations: false 18 | BinPackArguments: false 19 | BinPackParameters: false 20 | BraceWrapping: 21 | AfterClass: true 22 | AfterControlStatement: false 23 | AfterEnum: false 24 | AfterFunction: true 25 | AfterNamespace: false 26 | AfterObjCDeclaration: false 27 | AfterStruct: true 28 | AfterUnion: false 29 | BeforeCatch: false 30 | BeforeElse: false 31 | IndentBraces: false 32 | SplitEmptyFunction: false 33 | SplitEmptyNamespace: true 34 | SplitEmptyRecord: true 35 | BreakAfterJavaFieldAnnotations: true 36 | BreakBeforeBinaryOperators: NonAssignment 37 | BreakBeforeBraces: Custom 38 | BreakBeforeInheritanceComma: true 39 | BreakBeforeTernaryOperators: true 40 | BreakConstructorInitializers: BeforeColon 41 | BreakConstructorInitializersBeforeComma: false 42 | BreakStringLiterals: true 43 | ColumnLimit: 0 44 | CommentPragmas: '^ IWYU pragma:' 45 | CompactNamespaces: false 46 | ConstructorInitializerAllOnOneLineOrOnePerLine: false 47 | ConstructorInitializerIndentWidth: 2 48 | ContinuationIndentWidth: 2 49 | Cpp11BracedListStyle: false 50 | DerivePointerAlignment: false 51 | DisableFormat: false 52 | ExperimentalAutoDetectBinPacking: true 53 | FixNamespaceComments: true 54 | ForEachMacros: 55 | - foreach 56 | - Q_FOREACH 57 | - BOOST_FOREACH 58 | IncludeCategories: 59 | - Priority: 2 60 | Regex: ^"(llvm|llvm-c|clang|clang-c)/ 61 | - Priority: 3 62 | Regex: ^(<|"(gtest|gmock|isl|json)/) 63 | - Priority: 1 64 | Regex: .* 65 | IncludeIsMainRegex: (Test)?$ 66 | IndentCaseLabels: false 67 | IndentWidth: 2 68 | IndentWrappedFunctionNames: true 69 | JavaScriptQuotes: Leave 70 | JavaScriptWrapImports: true 71 | KeepEmptyLinesAtTheStartOfBlocks: true 72 | Language: Cpp 73 | MacroBlockBegin: '' 74 | MacroBlockEnd: '' 75 | MaxEmptyLinesToKeep: 2 76 | NamespaceIndentation: Inner 77 | ObjCBlockIndentWidth: 7 78 | ObjCSpaceAfterProperty: true 79 | ObjCSpaceBeforeProtocolList: false 80 | PointerAlignment: Right 81 | ReflowComments: true 82 | SortIncludes: false 83 | SortUsingDeclarations: false 84 | SpaceAfterCStyleCast: false 85 | SpaceAfterTemplateKeyword: false 86 | SpaceBeforeAssignmentOperators: true 87 | SpaceBeforeParens: ControlStatements 88 | SpaceInEmptyParentheses: false 89 | SpacesBeforeTrailingComments: 0 90 | SpacesInAngles: false 91 | SpacesInCStyleCastParentheses: false 92 | SpacesInContainerLiterals: true 93 | SpacesInParentheses: false 94 | SpacesInSquareBrackets: false 95 | Standard: Cpp11 96 | TabWidth: 2 97 | UseTab: Never 98 | 99 | -------------------------------------------------------------------------------- /.clang-tidy: -------------------------------------------------------------------------------- 1 | --- 2 | Checks: '*,-fuchsia-*,-google-*,-zircon-*,-abseil-*,-modernize-use-trailing-return-type,-llvm-*' 3 | WarningsAsErrors: '0' 4 | HeaderFilterRegex: '' 5 | FormatStyle: Chromium 6 | 7 | 8 | -------------------------------------------------------------------------------- /.cmake-format.yaml: -------------------------------------------------------------------------------- 1 | additional_commands: 2 | foo: 3 | flags: 4 | - BAR 5 | - BAZ 6 | kwargs: 7 | DEPENDS: '*' 8 | HEADERS: '*' 9 | SOURCES: '*' 10 | bullet_char: '*' 11 | dangle_parens: false 12 | enum_char: . 13 | line_ending: unix 14 | line_width: 120 15 | max_subargs_per_line: 3 16 | separate_ctrl_name_with_space: false 17 | separate_fn_name_with_space: false 18 | tab_size: 2 19 | 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | # custom files to ignore 35 | .idea/vcs.xml 36 | .vscode/settings.json 37 | CMakeLists.txt.user 38 | 39 | # custom folders to ignore 40 | d:/ 41 | build 42 | debug 43 | release 44 | .idea 45 | .metals 46 | .vscode 47 | cmake-build-debug* 48 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/01_first_reverse.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: First Reverse 3 | 4 | Using the C++ language, have the function FirstReverse(str) take the str 5 | parameter being passed and return the string in reversed order. For example: if 6 | the input string is "Hello World and Coders" then your program should return the 7 | string sredoC dna dlroW olleH. 8 | 9 | Sample test cases: 10 | 11 | Input: "coderbyte" 12 | Output: "etybredoc" 13 | 14 | Input: "I Love Code" 15 | Output: "edoC evoL I" 16 | */ 17 | 18 | #include 19 | #include 20 | 21 | #include 22 | #include 23 | #define CATCH_CONFIG_MAIN 24 | 25 | using namespace std; 26 | // using namespace string_literals; 27 | 28 | string FirstReverse_v1(string str) { 29 | str = stl::helper::trim(str); 30 | const size_t str_len{str.length()}; 31 | 32 | if (str_len < 2u) 33 | return str; 34 | 35 | for (size_t i{}; i < str_len / 2; i++) 36 | swap(str[i], str[str_len - 1 - i]); 37 | 38 | return str; 39 | } 40 | 41 | string FirstReverse_v2(string str) { 42 | str = stl::helper::trim(str); 43 | reverse(begin(str), end(str)); 44 | 45 | return str; 46 | } 47 | 48 | TEST_CASE("First Reverse: FirstReverse_v1") { 49 | REQUIRE(FirstReverse_v1("Hello World and Coders") == 50 | "sredoC dna dlroW olleH"); 51 | REQUIRE(FirstReverse_v1("coderbyte") == "etybredoc"); 52 | REQUIRE(FirstReverse_v1("I Love Code") == "edoC evoL I"); 53 | } 54 | 55 | TEST_CASE("First Reverse: FirstReverse_v2") { 56 | REQUIRE(FirstReverse_v2("Hello World and Coders") == 57 | "sredoC dna dlroW olleH"); 58 | REQUIRE(FirstReverse_v2("coderbyte") == "etybredoc"); 59 | REQUIRE(FirstReverse_v2("I Love Code") == "edoC evoL I"); 60 | } 61 | 62 | // int main() { 63 | // // cout << FirstReverse_v1(gets(stdin)); 64 | // cout << FirstReverse_v1("Hello World and Coders") 65 | // << '\n'; // expected output: "sredoC dna dlroW olleH" 66 | // cout << FirstReverse_v1("coderbyte") << '\n'; // expected output: 67 | // "etybredoc" cout << FirstReverse_v1("I Love Code") 68 | // << '\n'; // expected output: "edoC evoL I" 69 | // 70 | // return 0; 71 | // } 72 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/06_letter_capitalize.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Letter Capitalize 3 | 4 | Using the C++ language, have the function LetterCapitalize(str) take the str 5 | parameter being passed and capitalize the first letter of each word. Words will 6 | be separated by only one space. 7 | 8 | Sample test cases: 9 | 10 | Input: "hello world" 11 | Output: "Hello World" 12 | 13 | Input: "i ran there" 14 | Output: "I Ran There" 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include 26 | #define CATCH_CONFIG_MAIN 27 | #include 28 | 29 | using namespace std; 30 | 31 | string LetterCapitalize(string str) { 32 | str = stl::helper::trim(str); 33 | 34 | bool is_space_char{true}; 35 | 36 | const auto& f = use_facet>(locale{}); 37 | 38 | for (auto& ch : str) { 39 | if (' ' == ch) { 40 | is_space_char = true; 41 | } else if (is_space_char) { 42 | ch = f.toupper(ch); 43 | is_space_char = false; 44 | } 45 | } 46 | 47 | return str; 48 | } 49 | 50 | TEST_CASE("Letter Capitalize") { 51 | REQUIRE(LetterCapitalize("hello world") == "Hello World"); 52 | REQUIRE(LetterCapitalize("i ran there") == "I Ran There"); 53 | } 54 | 55 | // int main() { 56 | // // cout << LetterCapitalize(gets(stdin)); 57 | // cout << LetterCapitalize("hello world") 58 | // << '\n'; // expected output: "Hello World" 59 | // cout << LetterCapitalize("i ran there") 60 | // << '\n'; // expected output: "I Ran There" 61 | 62 | // return 0; 63 | // } 64 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/08_check_nums.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Check Nums 3 | 4 | Using the C++ language, have the function CheckNums(num1,num2) take both 5 | parameters being passed and return the string true if num2 is greater than num1, 6 | otherwise return the string false. If the parameter values are equal to each 7 | other then return the string -1. 8 | 9 | Sample test cases: 10 | 11 | Input: 3 & num2 = 122 12 | Output: "true" 13 | 14 | Input: 67 & num2 = 67 15 | Output: "-1" 16 | 17 | Input: 83 & num2 = 67 18 | Output: "false" 19 | */ 20 | 21 | #include 22 | // #include 23 | #include 24 | #include 25 | 26 | #define CATCH_CONFIG_MAIN 27 | #include 28 | 29 | using namespace std; 30 | 31 | string CheckNums_v1(const int num1, const int num2) { 32 | if (num1 == num2) 33 | return "-1"; 34 | return num2 > num1 ? "true" : "false"; 35 | } 36 | 37 | string CheckNums_v2(const int num1, const int num2) { 38 | static constexpr const char* dict[3]{"true", "-1", "false"}; 39 | 40 | const int diff{num1 - num2}; 41 | const int abs_diff{abs(diff)}; 42 | const int key{1 + diff / (0 != abs_diff ? abs_diff : 1)}; 43 | return dict[key]; 44 | } 45 | 46 | TEST_CASE("Check Nums: CheckNums_v1") { 47 | REQUIRE(CheckNums_v1(3, 122) == "true"); 48 | REQUIRE(CheckNums_v1(67, 67) == "-1"); 49 | REQUIRE(CheckNums_v1(35, 17) == "false"); 50 | } 51 | 52 | TEST_CASE("Check Nums: CheckNums_v2") { 53 | REQUIRE(CheckNums_v2(3, 122) == "true"); 54 | REQUIRE(CheckNums_v2(67, 67) == "-1"); 55 | REQUIRE(CheckNums_v2(35, 17) == "false"); 56 | } 57 | 58 | // int main() { 59 | // // cout << CheckNums_v2(gets(stdin)); 60 | // cout << CheckNums_v2(3, 122) << '\n'; // expected output: "true" 61 | // cout << CheckNums_v2(67, 67) << '\n'; // expected output: "-1" 62 | // cout << CheckNums_v2(35, 17) << '\n'; // expected output: "false" 63 | 64 | // return 0; 65 | // } 66 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/09_time_convert.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Time Convert 3 | 4 | Using the C++ language, have the function TimeConvert(num) take the num 5 | parameter being passed and return the number of hours and minutes the parameter 6 | converts to (ie. if num = 63 then the output should be 1:3). Separate the 7 | number of hours and minutes with a colon. 8 | 9 | Sample test cases: 10 | 11 | Input: 126 12 | Output: "2:6" 13 | 14 | Input: 45 15 | Output: "0:45" 16 | */ 17 | 18 | #include 19 | #include 20 | // #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #define CATCH_CONFIG_MAIN 27 | #include 28 | 29 | using namespace std; 30 | 31 | string TimeConvert(uint64_t minutes) { 32 | uint64_t hours{minutes / 60}; 33 | minutes %= 60; 34 | string result; 35 | 36 | if (0 == minutes) { 37 | result.push_back('0'); 38 | } else { 39 | while (minutes > 0) { 40 | result.push_back('0' + minutes % 10); 41 | minutes /= 10; 42 | } 43 | } 44 | 45 | result.push_back(':'); 46 | 47 | if (0 == hours) { 48 | result.push_back('0'); 49 | } else { 50 | while (hours > 0) { 51 | result.push_back('0' + hours % 10); 52 | hours /= 10; 53 | } 54 | } 55 | 56 | reverse(begin(result), end(result)); 57 | 58 | return result; 59 | } 60 | 61 | TEST_CASE("Time Convert") { 62 | REQUIRE(TimeConvert(120) == "2:0"); 63 | REQUIRE(TimeConvert(35) == "0:35"); 64 | REQUIRE(TimeConvert(256) == "4:16"); 65 | REQUIRE(TimeConvert(308) == "5:8"); 66 | REQUIRE(TimeConvert(935) == "15:35"); 67 | } 68 | 69 | // int main() { 70 | // constexpr static Solution s{}; 71 | 72 | // // cout << s.TimeConvert(gets(stdin)); 73 | // std::cout << s.TimeConvert(120) << '\n'; // expected output: "2:0" 74 | // std::cout << s.TimeConvert(35) << '\n'; // expected output: "0:35" 75 | // std::cout << s.TimeConvert(256) << '\n'; // expected output: "4:16" 76 | // std::cout << s.TimeConvert(308) << '\n'; // expected output: "5:8" 77 | // std::cout << s.TimeConvert(935) << '\n'; // expected output: "15:35" 78 | 79 | // return 0; 80 | // } 81 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/10_alphabet_soup.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Alphabet Soup 3 | 4 | Using the C++ language, have the function AlphabetSoup(str) take the str string 5 | parameter being passed and return the string with the letters in alphabetical 6 | order (ie. hello becomes ehllo). Assume numbers and punctuation symbols will not 7 | be included in the string. 8 | 9 | Sample test cases: 10 | 11 | Input: "coderbyte" 12 | Output: "bcdeeorty" 13 | 14 | Input: "hooplah" 15 | Output: "ahhloop" 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | // #include 22 | #include 23 | #include 24 | 25 | #define CATCH_CONFIG_MAIN 26 | #include 27 | 28 | using namespace std; 29 | 30 | string AlphabetSoup_v1(string str) { 31 | sort(begin(str), end(str)); 32 | return str; 33 | } 34 | 35 | int compare(const void* lc, const void* rc) { 36 | return *static_cast(lc) - *static_cast(rc); 37 | } 38 | 39 | string AlphabetSoup_v2(string str) { 40 | if (str.empty()) 41 | return {}; 42 | qsort(str.data(), str.length(), sizeof(str.front()), compare); 43 | return str; 44 | } 45 | 46 | // simple bubblesort implementation 47 | string AlphabetSoup_v3(string str) { 48 | const size_t str_len{str.length()}; 49 | if (str_len < 2U) 50 | return str; 51 | 52 | for (size_t i{1U}, did_swap_elements{1}; 53 | did_swap_elements != 0 && i < str_len; ++i) { 54 | did_swap_elements = 0; 55 | for (size_t j{}; j < str_len - i; ++j) { 56 | if (str[j] > str[j + 1]) { 57 | swap(str[j], str[j + 1]); 58 | did_swap_elements = 1; 59 | } 60 | } 61 | } 62 | 63 | return str; 64 | } 65 | 66 | TEST_CASE("Alphabet Soup : AlphabetSoup_v1") { 67 | REQUIRE(AlphabetSoup_v1("hello") == "ehllo"); 68 | REQUIRE(AlphabetSoup_v1("coderbyte") == "bcdeeorty"); 69 | REQUIRE(AlphabetSoup_v1("hooplah") == "ahhloop"); 70 | } 71 | 72 | TEST_CASE("Alphabet Soup : AlphabetSoup_v2") { 73 | REQUIRE(AlphabetSoup_v2("hello") == "ehllo"); 74 | REQUIRE(AlphabetSoup_v2("coderbyte") == "bcdeeorty"); 75 | REQUIRE(AlphabetSoup_v2("hooplah") == "ahhloop"); 76 | } 77 | 78 | TEST_CASE("Alphabet Soup : AlphabetSoup_v3") { 79 | REQUIRE(AlphabetSoup_v3("hello") == "ehllo"); 80 | REQUIRE(AlphabetSoup_v3("coderbyte") == "bcdeeorty"); 81 | REQUIRE(AlphabetSoup_v3("hooplah") == "ahhloop"); 82 | } 83 | 84 | // int main() { 85 | // // cout << AlphabetSoup_v2(gets(stdin)); 86 | // cout << AlphabetSoup_v3("hello") << '\n'; // expected output: ehllo 87 | // cout << AlphabetSoup_v3("coderbyte") << '\n'; // expected output: 88 | // bcdeeorty cout << AlphabetSoup_v3("hooplah") << '\n'; // expected 89 | // output: ahhloop return 0; 90 | // } 91 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/19_second_greatlow.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Second Greatlow 3 | 4 | Using the C++ language, have the function SecondGreatLow(arr) take the array of 5 | numbers stored in arr and return the second lowest and second greatest numbers, 6 | respectively, separated by a space. For example: if arr contains [7, 7, 12, 98, 7 | 106] the output should be 12 98. The array will not be empty and will contain at 8 | least 2 numbers. It can get tricky if there's just two numbers! 9 | 10 | Sample test cases: 11 | 12 | Input: 1, 42, 42, 180 13 | Output: "42 42" 14 | 15 | Input: 4, 90 16 | Output: "90 4" 17 | */ 18 | 19 | #include 20 | // #include 21 | #include 22 | 23 | #include 24 | 25 | #define CATCH_CONFIG_MAIN 26 | #include 27 | 28 | using namespace std; 29 | 30 | string second_great_low(int* arr, const int arr_size) { 31 | if (1 == arr_size) 32 | return "Array argument has only 1 element!"; 33 | 34 | sort(arr, arr + arr_size); 35 | 36 | if (2 == arr_size) 37 | return to_string(arr[1]) + " " + to_string(arr[0]); 38 | 39 | int second_min_element{arr[0]}; 40 | int second_max_element{arr[arr_size - 1]}; 41 | int x{}; 42 | 43 | // 1 2 5 44 | // 1 5 45 | // second_min_element = 2, x = 1 46 | // 1, 1 >= 1, 1-- 47 | // 2 < 5 ? 2 48 | // second_max_element = 2 49 | 50 | for (int i{1}; i < arr_size; ++i) { 51 | if (arr[i] > second_min_element) { 52 | second_min_element = arr[i]; 53 | x = i; 54 | break; 55 | } 56 | } 57 | 58 | for (int j{arr_size - 2}; j >= x; --j) { 59 | if (arr[j] < second_max_element) { 60 | second_max_element = arr[j]; 61 | break; 62 | } 63 | } 64 | 65 | return to_string(second_min_element) + " " + to_string(second_max_element); 66 | } 67 | 68 | TEST_CASE("Second Greatlow : second_great_low") { 69 | int a[] = {7, 7, 12, 98, 106}; 70 | CHECK(second_great_low(a, sizeof(a) / sizeof(*a)) == "12 98"); 71 | int b[] = {1, 42, 42, 180}; 72 | CHECK(second_great_low(b, sizeof(b) / sizeof(*b)) == "42 42"); 73 | int c[] = {4, 90}; 74 | CHECK(second_great_low(c, sizeof(c) / sizeof(*c)) == "90 4"); 75 | int d[] = {1, 2, 5}; 76 | CHECK(second_great_low(d, sizeof(d) / sizeof(*d)) == "2 2"); 77 | } 78 | 79 | // int main() { 80 | // // int A[] = gets(stdin); 81 | // // cout << second_great_low(A, sizeof(A) / sizeof(*A)); 82 | // int a[] = {7, 7, 12, 98, 106}; 83 | // cout << second_great_low(a, sizeof(a) / sizeof(*a)) 84 | // << '\n'; // expected output: "12 98" 85 | // int b[] = {1, 42, 42, 180}; 86 | // cout << second_great_low(b, sizeof(b) / sizeof(*b)) 87 | // << '\n'; // expected output: "42 42" 88 | // int c[] = {4, 90}; 89 | // cout << second_great_low(c, sizeof(c) / sizeof(*c)) 90 | // << '\n'; // expected output: "90 4" 91 | // int d[] = {1, 2, 5}; 92 | // cout << second_great_low(d, sizeof(d) / sizeof(*d)) 93 | // << '\n'; // expected output: "2 2" 94 | 95 | // return 0; 96 | // } 97 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/22_mean_mode.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Mean Mode 3 | 4 | Using the C++ language, have the function MeanMode(arr) take the array of 5 | numbers stored in arr and return 1 if the mode equals the mean, 0 if they don't 6 | equal each other (ie. [5, 3, 3, 3, 1] should return 1 because the mode (3) 7 | equals the mean (3)). The array will not be empty, will only contain positive 8 | integers, and will not contain more than one mode. 9 | 10 | Sample test cases: 11 | 12 | Input: 1, 2, 3 13 | Output: 0 14 | 15 | Input: 4, 4, 4, 6, 2 16 | Output: 1 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | using namespace std; 26 | 27 | int MeanMode_v1(vector numbers) { 28 | if (numbers.size() < 2U) 29 | return -1; 30 | 31 | const int mean = accumulate(begin(numbers), end(numbers), 0) / numbers.size(); 32 | 33 | unordered_map number_frequency{}; 34 | 35 | int max_frequency_number{numbers.front()}; 36 | size_t max_frequency{1U}; 37 | 38 | for (const int n : numbers) { 39 | number_frequency[n]++; 40 | if (number_frequency[n] > max_frequency) { 41 | max_frequency = number_frequency[n]; 42 | max_frequency_number = n; 43 | } 44 | } 45 | 46 | return mean == max_frequency_number ? 1 : 0; 47 | } 48 | 49 | int main() { 50 | // cout << MeanMode_v1({gets(stdin)}); 51 | cout << MeanMode_v1({5, 3, 3, 3, 1}) << '\n'; // expected output: 1 52 | cout << MeanMode_v1({1, 2, 3}) << '\n'; // expected output: 0 53 | cout << MeanMode_v1({4, 4, 4, 6, 2}) << '\n'; // expected output: 1 54 | cout << MeanMode_v1({10, 10}) << '\n'; // expected output: 1 55 | cout << MeanMode_v1({1, 1, 1, 1, 1}) << '\n'; // expected output: 1 56 | cout << MeanMode_v1({10, 10, 10, 10, 10, 10, 10, 20, 9, 1}) 57 | << '\n'; // expected output: 1 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/23_dash_insert.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Dash Insert 3 | 4 | Using the C++ language, have the function DashInsert(str) insert dashes ('-') 5 | between each two odd numbers in str. For example: if str is 454793 the output 6 | should be 4547-9-3. Don't count zero as an odd number. 7 | 8 | Sample test cases: 9 | 10 | Input: 99946 11 | Output: 9-9-946 12 | 13 | Input: 56730 14 | Output: 567-30 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | using namespace std; 26 | 27 | std::string trim(const std::string& src, 28 | const char* chars_to_trim = " \t\n\f\v\r") { 29 | if (0U == src.length()) 30 | return {}; 31 | 32 | const std::unordered_set trimmed_chars( 33 | chars_to_trim, chars_to_trim + strlen(chars_to_trim)); 34 | 35 | const auto first{std::find_if( 36 | std::cbegin(src), std::cend(src), [&trimmed_chars](const char ch) { 37 | return trimmed_chars.find(ch) == std::cend(trimmed_chars); 38 | })}; 39 | 40 | if (first == std::cend(src)) 41 | return {}; 42 | 43 | const auto last{std::find_if(std::crbegin(src), std::crend(src), 44 | [&trimmed_chars](const char ch) { 45 | return trimmed_chars.find(ch) == 46 | std::cend(trimmed_chars); 47 | }) 48 | .base()}; 49 | 50 | return {first, last}; 51 | } 52 | 53 | string DashInsert_v1(string str) { 54 | str = trim(str); 55 | 56 | const size_t str_len{str.length()}; 57 | 58 | if (str_len < 2U) 59 | return str; 60 | 61 | string result(1, str.front()); 62 | result.reserve(2 * str_len - 1); 63 | 64 | bool is_prev_odd{(str[0] - '0') % 2 == 1}; 65 | 66 | for (size_t i{1}; i < str_len; i++) { 67 | if (is_prev_odd && (str[i] - '0') % 2 == 1) { 68 | result.push_back('-'); 69 | result.push_back(str[i]); 70 | } else { 71 | result.push_back(str[i]); 72 | is_prev_odd = (str[i] - '0') % 2 == 1; 73 | } 74 | } 75 | 76 | return result; 77 | } 78 | 79 | string DashInsert_v2(string str) { 80 | str = trim(str); 81 | 82 | const size_t str_len{str.length()}; 83 | 84 | if (str_len < 2U) 85 | return str; 86 | 87 | ostringstream oss{}; 88 | oss << str.front(); 89 | 90 | bool is_prev_odd{(str.front() - '0') % 2 == 1}; 91 | 92 | for (size_t i{1}; i < str_len; i++) { 93 | if (is_prev_odd && (str[i] - '0') % 2 == 1) 94 | oss << '-' << str[i]; 95 | else { 96 | oss << str[i]; 97 | is_prev_odd = (str[i] - '0') % 2 == 1; 98 | } 99 | } 100 | 101 | return oss.str(); 102 | } 103 | 104 | int main() { 105 | // cout << DashInsert_v2(gets(stdin)); 106 | cout << DashInsert_v2("454793") << '\n'; // expected output: "4547-9-3" 107 | cout << DashInsert_v2("99946") << '\n'; // expected output: "9-9-946" 108 | cout << DashInsert_v2("56730") << '\n'; // expected output: "567-30" 109 | 110 | return 0; 111 | } 112 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/24_swap_case.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Swap Case 3 | 4 | Using the C++ language, have the function SwapCase(str) take the str parameter 5 | and swap the case of each character. For example: if str is "Hello World" the 6 | output should be hELLO wORLD. Let numbers and symbols stay the way they are. 7 | 8 | Sample test cases: 9 | 10 | Input: "Hello-LOL" 11 | Output: "hELLO-lol" 12 | 13 | Input: "Sup DUDE!!?" 14 | Output: "sUP dude!!?" 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | using namespace std; 26 | 27 | std::string trim(const std::string& src, 28 | const char* chars_to_trim = " \t\n\f\v\r") { 29 | if (0U == src.length()) 30 | return {}; 31 | 32 | const std::unordered_set trimmed_chars( 33 | chars_to_trim, chars_to_trim + strlen(chars_to_trim)); 34 | 35 | const auto first{std::find_if( 36 | std::cbegin(src), std::cend(src), [&trimmed_chars](const char ch) { 37 | return trimmed_chars.find(ch) == std::cend(trimmed_chars); 38 | })}; 39 | 40 | if (first == std::cend(src)) 41 | return {}; 42 | 43 | const auto last{std::find_if(std::crbegin(src), std::crend(src), 44 | [&trimmed_chars](const char ch) { 45 | return trimmed_chars.find(ch) == 46 | std::cend(trimmed_chars); 47 | }) 48 | .base()}; 49 | 50 | return {first, last}; 51 | } 52 | 53 | string swap_case_v1(string str) { 54 | str = trim(str); 55 | 56 | const int offset{'a' - 'A'}; 57 | 58 | transform(cbegin(str), cend(str), begin(str), [&offset](const char ch) { 59 | if (ch >= 'a' && ch <= 'z') 60 | return static_cast(ch - offset); 61 | else if (ch >= 'A' && ch <= 'Z') 62 | return static_cast(ch + offset); 63 | return ch; 64 | }); 65 | 66 | return str; 67 | } 68 | 69 | template 70 | struct character_offsets { 71 | int offsets[256U]; 72 | 73 | constexpr character_offsets() : offsets{} { 74 | for (char i{'A'}, j{'a'}; i <= 'Z'; ++i, ++j) { 75 | offsets[i] = offset1; 76 | offsets[j] = offset2; 77 | } 78 | } 79 | }; 80 | 81 | template 82 | using co = character_offsets; 83 | 84 | string swap_case_v2(string str) { 85 | constexpr co<'a' - 'A', 'A' - 'a'> char_offsets{}; 86 | 87 | str = trim(str); 88 | 89 | transform(cbegin(str), cend(str), begin(str), [&char_offsets](const char ch) { 90 | return static_cast(ch + char_offsets.offsets[ch]); 91 | }); 92 | 93 | return str; 94 | } 95 | 96 | int main() { 97 | // cout << swap_case_v2(gets(stdin)); 98 | cout << swap_case_v2("Hello World") 99 | << '\n'; // expected output: "hELLO wORLD" 100 | cout << swap_case_v2("Hello-LOL") << '\n'; // expected output: "hELLO-lol" 101 | cout << swap_case_v2("Sup DUDE!!?") 102 | << '\n'; // expected output: "sUP dude!!?" 103 | 104 | return 0; 105 | } 106 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/26_third_greatest.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Third Greatest 3 | 4 | Using the C++ language, have the function ThirdGreatest(strArr) take the array 5 | of strings stored in strArr and return the third largest word within it. So for 6 | example: if strArr is ["hello", "world", "before", "all"] your output should be 7 | world because "before" is 6 letters long, and "hello" and "world" are both 5, 8 | but the output should be world because it appeared as the last 5 letter word in 9 | the array. If strArr was ["hello", "world", "after", "all"] the output should be 10 | after because the first three words are all 5 letters long, so return the last 11 | one. The array will have at least three strings and each string will only 12 | contain letters. 13 | 14 | Sample test cases: 15 | 16 | Input: "coder","byte","code" 17 | Output: "code" 18 | 19 | Input: "abc","defg","z","hijk" 20 | Output: "abc" 21 | */ 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | using namespace std; 30 | 31 | string ThirdGreatest_v1(vector words) { 32 | if (words.size() < 3U) 33 | return "not possible"; 34 | 35 | stable_sort(begin(words), end(words), [](const string& lw, const string& rw) { 36 | return lw.length() > rw.length(); 37 | }); 38 | 39 | return move(words[2]); 40 | } 41 | 42 | string ThirdGreatest_v2(vector words) { 43 | const size_t words_len{words.size()}; 44 | 45 | if (words_len < 3U) 46 | return "not possible"; 47 | 48 | auto longest_word_iter = max_element( 49 | begin(words), end(words), 50 | [](const auto& lw, const auto& rw) { return lw.length() < rw.length(); }); 51 | 52 | longest_word_iter->clear(); 53 | 54 | longest_word_iter = max_element( 55 | begin(words), end(words), 56 | [](const auto& lw, const auto& rw) { return lw.length() < rw.length(); }); 57 | 58 | longest_word_iter->clear(); 59 | 60 | longest_word_iter = max_element( 61 | begin(words), end(words), 62 | [](const auto& lw, const auto& rw) { return lw.length() < rw.length(); }); 63 | 64 | return move(*longest_word_iter); 65 | } 66 | 67 | int main() { 68 | // cout << ThirdGreatest_v2(gets(stdin)); 69 | cout << ThirdGreatest_v2({"hello", "world", "before", "all"}) 70 | << '\n'; // expected output: world 71 | cout << ThirdGreatest_v2({"hello", "world", "after", "all"}) 72 | << '\n'; // expected output: after 73 | cout << ThirdGreatest_v2({"coder", "byte", "code"}) 74 | << '\n'; // expected output: code 75 | cout << ThirdGreatest_v2({"abc", "defg", "z", "hijk"}) 76 | << '\n'; // expected output: abc 77 | 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/27_powers_of_two.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Powers Of Two 3 | 4 | Using the C++ language, have the function PowersofTwo(num) take the num 5 | parameter being passed which will be an integer and return the string true if 6 | it's a power of two. If it's not return the string false. For example if the 7 | input is 16 then your program should return the string true but if the input is 8 | 22 then the output should be the string false. 9 | 10 | Sample test cases: 11 | 12 | Input: 4 13 | Output: "true" 14 | 15 | Input: 124 16 | Output: "false" 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | using namespace std; 24 | 25 | template 26 | struct Log2N { 27 | static constexpr size_t value = (((N & 0xFFFFFFFF00000000ULL) != 0) << 5) | 28 | (((N & 0xFFFF0000FFFF0000ULL) != 0) << 4) | 29 | (((N & 0xFF00FF00FF00FF00ULL) != 0) << 3) | 30 | (((N & 0xF0F0F0F0F0F0F0F0ULL) != 0) << 2) | 31 | (((N & 0xCCCCCCCCCCCCCCCCULL) != 0) << 1) | 32 | ((N & 0xAAAAAAAAAAAAAAAAULL) != 0); 33 | }; 34 | 35 | string PowersofTwo_v1(int64_t target_num) { 36 | if (target_num < 1) 37 | return "false"; 38 | 39 | while (target_num > 1) { 40 | if (1 == target_num % 2) 41 | return "false"; 42 | 43 | target_num /= 2; 44 | } 45 | 46 | return "true"; 47 | } 48 | 49 | string PowersofTwo_v2(const int64_t target_num) { 50 | if (target_num < 1) 51 | return "false"; 52 | 53 | int64_t num{1}; 54 | 55 | while (num <= target_num) { 56 | if (target_num == num) 57 | return "true"; 58 | 59 | num <<= 1; 60 | } 61 | 62 | return "false"; 63 | } 64 | 65 | constexpr const char* PowersofTwo_v3(const uint64_t target_num) { 66 | if (0U == target_num) 67 | return "false"; 68 | 69 | const size_t value = (((target_num & 0xFFFFFFFF00000000ULL) != 0) << 5) | 70 | (((target_num & 0xFFFF0000FFFF0000ULL) != 0) << 4) | 71 | (((target_num & 0xFF00FF00FF00FF00ULL) != 0) << 3) | 72 | (((target_num & 0xF0F0F0F0F0F0F0F0ULL) != 0) << 2) | 73 | (((target_num & 0xCCCCCCCCCCCCCCCCULL) != 0) << 1) | 74 | ((target_num & 0xAAAAAAAAAAAAAAAAULL) != 0); 75 | 76 | const uint64_t correct_result{1ULL << value}; 77 | return target_num == correct_result ? "true" : "false"; 78 | } 79 | 80 | int main() { 81 | // cout << PowersofTwo_v2(gets(stdin)); 82 | cout << PowersofTwo_v3(16) << '\n'; // expected output: "true" 83 | cout << PowersofTwo_v3(22) << '\n'; // expected output: "false" 84 | cout << PowersofTwo_v3(4) << '\n'; // expected output: "true" 85 | cout << PowersofTwo_v3(124) << '\n'; // expected output: "false" 86 | cout << PowersofTwo_v3(16) << '\n'; // expected output: "true" 87 | cout << PowersofTwo_v3(8) << '\n'; // expected output: "true" 88 | cout << PowersofTwo_v3(32) << '\n'; // expected output: "true" 89 | cout << PowersofTwo_v3(11128) << '\n'; // expected output: "false" 90 | 91 | return 0; 92 | } 93 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/28_additive_persistence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Additive Persistence 3 | 4 | Using the C++ language, have the function AdditivePersistence(num) take the num 5 | parameter being passed which will always be a positive integer and return its 6 | additive persistence which is the number of times you must add the digits in num 7 | until you reach a single digit. For example: if num is 2718 then your program 8 | should return 2 because 2 + 7 + 1 + 8 = 18 and 1 + 8 = 9 and you stop at 9. 9 | 10 | Sample test cases: 11 | 12 | Input: 4 13 | Output: 0 14 | 15 | Input: 19 16 | Output: 2 17 | */ 18 | 19 | #include 20 | 21 | using namespace std; 22 | 23 | constexpr int AdditivePersistence(int64_t num) { 24 | if (num < 0) 25 | num = -num; 26 | 27 | int iter_count{}; 28 | 29 | while (num > 9) { 30 | iter_count++; 31 | int64_t sum{}; 32 | 33 | while (0 != num) { 34 | sum += num % 10; 35 | num /= 10; 36 | } 37 | 38 | num = sum; 39 | } 40 | 41 | return iter_count; 42 | } 43 | 44 | int main() { 45 | // cout << AdditivePersistence(gets(stdin)); 46 | cout << AdditivePersistence(2718) << '\n'; // expected output: 2 47 | cout << AdditivePersistence(4) << '\n'; // expected output: 0 48 | cout << AdditivePersistence(19) << '\n'; // expected output: 2 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/29_multiplicative_persistence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Multiplicative Persistence 3 | 4 | Using the C++ language, have the function MultiplicativePersistence(num) take 5 | the num parameter being passed which will always be a positive integer and 6 | return its multiplicative persistence which is the number of times you must 7 | multiply the digits in num until you reach a single digit. For example: if num 8 | is 39 then your program should return 3 because 3 * 9 = 27 then 2 * 7 = 14 and 9 | finally 1 * 4 = 4 and you stop at 4. 10 | 11 | Sample test cases: 12 | 13 | Input: 4 14 | Output: 0 15 | 16 | Input: 25 17 | Output: 2 18 | */ 19 | 20 | #include 21 | 22 | using namespace std; 23 | 24 | constexpr int MultiplicativePersistence(int64_t num) { 25 | if (num < 0) 26 | num = -num; 27 | 28 | int iter_count{}; 29 | 30 | while (num > 9) { 31 | iter_count++; 32 | int64_t product{1}; 33 | 34 | while (0 != num) { 35 | product *= num % 10; 36 | num /= 10; 37 | } 38 | 39 | num = product; 40 | } 41 | 42 | return iter_count; 43 | } 44 | 45 | int main() { 46 | // cout << MultiplicativePersistence(gets(stdin)); 47 | cout << MultiplicativePersistence(39) << '\n'; // expected output: 3 48 | cout << MultiplicativePersistence(4) << '\n'; // expected output: 0 49 | cout << MultiplicativePersistence(25) << '\n'; // expected output: 2 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/31_changing_sequence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Changing Sequence 3 | 4 | Using the C++ language, have the function ChangingSequence(arr) take the array 5 | of numbers stored in arr and return the index at which the numbers stop 6 | increasing and begin decreasing or stop decreasing and begin increasing. For 7 | example: if arr is [1, 2, 4, 6, 4, 3, 1] then your program should return 3 8 | because 6 is the last point in the array where the numbers were increasing and 9 | the next number begins a decreasing sequence. The array will contain at least 3 10 | numbers and it may contain only a single sequence, increasing or decreasing. If 11 | there is only a single sequence in the array, then your program should return 12 | -1. Indexing should begin with 0. 13 | 14 | Sample test cases: 15 | 16 | Input: -4, -2, 9, 10 17 | Output: -1 18 | 19 | Input: 5, 4, 3, 2, 10, 11 20 | Output: 3 21 | */ 22 | 23 | #include 24 | #include 25 | 26 | using namespace std; 27 | 28 | int ChangingSequence_v1(vector numbers) { 29 | if (numbers.size() < 3U) 30 | return -1; 31 | const size_t numbers_size{numbers.size()}; 32 | size_t si{1}; 33 | 34 | for (; si < numbers_size; si++) { 35 | if (numbers[0] != numbers[si]) 36 | break; 37 | } 38 | 39 | const bool ascending_order{numbers[0] < numbers[si]}; 40 | 41 | for (size_t i{si}; i < numbers.size(); ++i) { 42 | if (ascending_order) { 43 | if (numbers[i] < numbers[i - 1]) 44 | return i - 1; 45 | } 46 | 47 | if (!ascending_order) { 48 | if (numbers[i] > numbers[i - 1]) 49 | return i - 1; 50 | } 51 | } 52 | 53 | return -1; 54 | } 55 | 56 | int ChangingSequence_v2(vector numbers) { 57 | if (numbers.size() < 3U) 58 | return -1; 59 | const size_t numbers_size{numbers.size()}; 60 | 61 | for (size_t i{1}; i < numbers_size - 1; ++i) { 62 | if (numbers[i] > numbers[i - 1] && numbers[i] > numbers[i + 1]) 63 | return i; 64 | 65 | if (numbers[i] < numbers[i - 1] && numbers[i] < numbers[i + 1]) 66 | return i; 67 | } 68 | 69 | return -1; 70 | } 71 | 72 | int main() { 73 | // cout << ChangingSequence_v2(gets(stdin)); 74 | cout << ChangingSequence_v2({1, 2, 4, 6, 4, 3, 1}) 75 | << '\n'; // expected output: 3 76 | cout << ChangingSequence_v2({-4, -2, 9, 10}) << '\n'; // expected output: -1 77 | cout << ChangingSequence_v2({5, 4, 3, 2, 10, 11}) 78 | << '\n'; // expected output: 3 79 | 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/32_overlapping_ranges.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Overlapping Ranges 3 | 4 | Using the C++ language, have the function OverlappingRanges(arr) take the array 5 | of numbers stored in arr which will contain 5 positive integers, the first two 6 | representing a range of numbers (a to b), the next 2 also representing another 7 | range of integers (c to d), and a final 5th element (x) which will also be a 8 | positive integer, and return the string true if both sets of ranges overlap by 9 | at least x numbers. For example: if arr is [4, 10, 2, 6, 3] then your program 10 | should return the string true. The first range of numbers are 4, 5, 6, 7, 8, 9, 11 | 10 and the second range of numbers are 2, 3, 4, 5, 6. The last element in the 12 | array is 3, and there are 3 numbers that overlap between both ranges: 4, 5, 13 | and 6. If both ranges do not overlap by at least x numbers, then your program 14 | should return the string false. 15 | 16 | Sample test cases: 17 | 18 | Input: 5,11,1,5,1 19 | Output: "true" 20 | 21 | Input: 1,8,2,4,4 22 | Output: "false" 23 | */ 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | using namespace std; 31 | 32 | string OverlappingRanges_v1(vector v) { 33 | if (v.size() < 5U) 34 | return "Please give me at least 5 whole numbers!"; 35 | 36 | const int64_t A1{v[0]}; 37 | const int64_t B1{v[1]}; 38 | 39 | const int64_t A2{v[2]}; 40 | const int64_t B2{v[3]}; 41 | 42 | const int64_t x{v[4]}; 43 | 44 | if (A1 <= A2 && B1 >= B2 && B2 - A2 + 1 >= x) 45 | return "true"; 46 | 47 | if (A1 <= A2 && B1 <= B2 && B1 >= A2 && B1 - A2 + 1 >= x) 48 | return "true"; 49 | 50 | if (A1 >= A2 && B1 >= B2 && A1 <= B2 && B2 - A1 + 1 >= x) 51 | return "true"; 52 | 53 | return "false"; 54 | } 55 | 56 | string OverlappingRanges_v2(vector v) { 57 | if (v.size() < 5U) 58 | return "Please give me at least 5 whole numbers!"; 59 | 60 | vector> intervals{ 61 | {v[0], 1}, {v[1], -1}, {v[2], 1}, {v[3], -1}}; 62 | sort(begin(intervals), end(intervals), 63 | [](const auto& lp, const auto& rp) { return lp.first < rp.first; }); 64 | 65 | if (0 == intervals[0].second + intervals[1].second) { 66 | if (intervals[1].first == intervals[2].first && 1 == v[4]) 67 | return "true"; 68 | return "false"; 69 | } 70 | 71 | if (intervals[2].first - intervals[1].first + 1 >= v[4]) 72 | return "true"; 73 | 74 | return "false"; 75 | } 76 | 77 | int main() { 78 | // cout << OverlappingRanges_v2(gets(stdin)); 79 | cout << OverlappingRanges_v2({4, 10, 2, 6, 3}) 80 | << '\n'; // expected output: "true" 81 | cout << OverlappingRanges_v2({5, 11, 1, 5, 1}) 82 | << '\n'; // expected output: "true" 83 | cout << OverlappingRanges_v2({1, 8, 2, 4, 4}) 84 | << '\n'; // expected output: "false" 85 | 86 | return 0; 87 | } 88 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/33_superincreasing.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Superincreasing 3 | 4 | Using the C++ language, have the function Superincreasing(arr) take the array of 5 | numbers stored in arr and determine if the array forms a superincreasing 6 | sequence where each element in the array is greater than the sum of all previous 7 | elements. The array will only consist of positive integers. For example: if arr 8 | is [1, 3, 6, 13, 54] then your program should return the string "true" because 9 | it forms a superincreasing sequence. If a superincreasing sequence isn't formed, 10 | then your program should return the string "false". 11 | 12 | Sample test cases: 13 | 14 | Input: 1,2,3,4 15 | Output: "false" 16 | 17 | Input: 1,2,5,10 18 | Output: "true" 19 | */ 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | using namespace std; 26 | 27 | string Superincreasing(vector numbers) { 28 | if (numbers.empty()) 29 | return "not possible"; 30 | 31 | int64_t prev_sum{numbers.front()}; 32 | 33 | for (size_t i{1}; i < numbers.size(); ++i) { 34 | if (numbers[i] <= prev_sum) 35 | return "false"; 36 | 37 | prev_sum += numbers[i]; 38 | } 39 | 40 | return "true"; 41 | } 42 | 43 | int main() { 44 | // cout << Superincreasing(gets(stdin)); 45 | cout << Superincreasing({1, 3, 6, 13, 54}) 46 | << '\n'; // expected output: "true" 47 | cout << Superincreasing({1, 2, 3, 4}) << '\n'; // expected output: "false" 48 | cout << Superincreasing({1, 2, 5, 10}) << '\n'; // expected output: "true" 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/38_wave_sorting.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Wave Sorting 3 | 4 | Using the C++ language, have the function WaveSorting(arr) take the array of 5 | positive integers stored in arr and return the string true if the numbers can be 6 | arranged in a wave pattern: a1 > a2 < a3 > a4 < a5 > ..., otherwise return the 7 | string false. For example, if arr is: [0, 1, 2, 4, 1, 4], then a possible wave 8 | ordering of the numbers is: [2, 0, 4, 1, 4, 1]. So for this input your program 9 | should return the string true. The input array will always contain at least 2 10 | elements. More examples are given below as sample test cases. 11 | 12 | Sample test cases: 13 | 14 | Input: 0, 1, 2, 4, 1, 1, 1 15 | Output: "false" 16 | 17 | Input: 0, 4, 22, 4, 14, 4, 2 18 | Output: "true" 19 | */ 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | using namespace std; 29 | 30 | string WaveSorting_v1(int* arr, const size_t arr_size) { 31 | sort(arr, arr + arr_size); 32 | unordered_map number_freq{{arr[0], 1}}; 33 | int last_counted_num{arr[0]}; 34 | size_t balance{1}; 35 | 36 | for (size_t i{1}; i < arr_size; ++i) { 37 | number_freq[arr[i]]++; 38 | 39 | if (0U == balance) { 40 | last_counted_num = arr[i]; 41 | balance++; 42 | 43 | } else if (last_counted_num == arr[i]) 44 | balance++; 45 | else 46 | balance--; 47 | } 48 | 49 | if (0U == balance) 50 | return "true"; 51 | 52 | return number_freq[last_counted_num] > arr_size / 2 ? "false" : "true"; 53 | } 54 | 55 | string WaveSorting_v2(int* arr, const size_t arr_size) { 56 | unordered_set unique_numbers{arr[0]}; 57 | int balance{}; 58 | int sign_factor{1}; 59 | int last_counted_number{arr[0]}; 60 | 61 | for (size_t i{1}; i < arr_size; ++i) { 62 | if (0U == unique_numbers.count(arr[i])) 63 | unique_numbers.emplace(arr[i]); 64 | else if (last_counted_number == arr[i]) 65 | balance += sign_factor; 66 | else { 67 | sign_factor = -sign_factor; 68 | balance += sign_factor; 69 | } 70 | 71 | last_counted_number = arr[i]; 72 | } 73 | 74 | return 0 == balance ? "true" : "false"; 75 | } 76 | 77 | int main() { 78 | // int arr[] = gets(stdin); 79 | // cout << WaveSorting_v2(arr, sizeof(arr)/sizeof(*arr)); 80 | int b[]{0, 1, 2, 4, 1, 4}; 81 | cout << WaveSorting_v2(b, sizeof(b) / sizeof(*b)) 82 | << '\n'; // expected output: "true" 83 | int c[]{0, 1, 2, 4, 1, 1, 1}; 84 | cout << WaveSorting_v2(c, sizeof(c) / sizeof(*c)) 85 | << '\n'; // expected output: "false" 86 | int d[]{0, 4, 22, 4, 14, 4, 2}; 87 | cout << WaveSorting_v2(d, sizeof(d) / sizeof(*d)) 88 | << '\n'; // expected output: "true" 89 | 90 | return 0; 91 | } 92 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/40_binary_reversal.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Binary Reversal 3 | 4 | Using the C++ language, have the function BinaryReversal(str) take the str 5 | parameter being passed, which will be a positive integer, take its binary 6 | representation, reverse that string of bits, and then finally return the new 7 | reversed string in decimal form. For example: if str is "47" then the binary 8 | version of this integer is 00101111. Your program should reverse this binary 9 | string which then becomes: 11110100 and then finally return the decimal version 10 | of this string, which is 244. 11 | 12 | Sample test cases: 13 | 14 | Input: "213" 15 | Output: "171" 16 | 17 | Input: "4567" 18 | Output: "60296" 19 | */ 20 | 21 | #include 22 | #include 23 | 24 | using namespace std; 25 | 26 | string BinaryReversal(string str) { 27 | int64_t number{stoll(str)}; 28 | 29 | if (0 == number) 30 | return "0"; 31 | 32 | int64_t reversed_number{}; 33 | size_t digit_count{}; 34 | 35 | while (0 != number) { 36 | reversed_number <<= 1; 37 | reversed_number |= number % 2; 38 | number >>= 1; 39 | ++digit_count; 40 | } 41 | 42 | if (0 != digit_count % 8) 43 | reversed_number <<= (digit_count / 8 + 1) * 8 - digit_count; 44 | 45 | return to_string(reversed_number); 46 | } 47 | 48 | int main() { 49 | // cout << BinaryReversal(gets(stdin)); 50 | cout << BinaryReversal("47") << '\n'; // expected output: "244" 51 | cout << BinaryReversal("213") << '\n'; // expected output: "171" 52 | cout << BinaryReversal("4567") << '\n'; // expected output: "60296" 53 | cout << BinaryReversal("6") << '\n'; // expected output: "96" 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/43_next_palindrome.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Next Palindrome 3 | 4 | Using the C++ language, have the function NextPalindrome(num) take the num 5 | parameter being passed and return the next largest palindromic number. The input 6 | can be any positive integer. For example: if num is 24, then your program should 7 | return 33 because that is the next largest number that is a palindrome. 8 | 9 | Sample test cases: 10 | 11 | Input: 2 12 | Output: 3 13 | 14 | Input: 180 15 | Output: 181 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | using namespace std; 24 | 25 | bool is_next_number_palindrome(const string& num_str) { 26 | for (size_t i{}, j{num_str.length() - 1}; i < j; ++i, --j) { 27 | if (num_str[i] != num_str[j]) 28 | return false; 29 | } 30 | 31 | return true; 32 | } 33 | 34 | int NextPalindrome_v1(int num) { 35 | num = abs(num); 36 | 37 | if (num < 9) 38 | return num + 1; 39 | 40 | do { 41 | ++num; 42 | } while (!is_next_number_palindrome(to_string(num))); 43 | 44 | return num; 45 | } 46 | 47 | int NextPalindrome_v2(int num) { 48 | num = abs(num); 49 | 50 | if (num < 9) 51 | return num + 1; 52 | 53 | do { 54 | ++num; 55 | 56 | int original_num{num}; 57 | int palindromic_num{}; 58 | 59 | while (0 != original_num) { 60 | palindromic_num *= 10; 61 | palindromic_num += original_num % 10; 62 | original_num /= 10; 63 | } 64 | 65 | if (num == palindromic_num) 66 | return num; 67 | 68 | } while (true); 69 | } 70 | 71 | int NextPalindrome_v3(int num) { 72 | num = abs(num); 73 | 74 | if (num < 9) 75 | return num + 1; 76 | 77 | array next_num_digits{}; 78 | 79 | do { 80 | ++num; 81 | int next_num{num}; 82 | size_t last{}; 83 | 84 | while (0 != next_num) { 85 | next_num_digits[last++] = next_num % 10; 86 | next_num /= 10; 87 | } 88 | 89 | for (size_t i{}, j{last - 1}; i < j; ++i, --j) { 90 | if (next_num_digits[i] != next_num_digits[j]) { 91 | next_num = -1; 92 | break; 93 | } 94 | } 95 | 96 | if (0 == next_num) 97 | return num; 98 | 99 | } while (true); 100 | } 101 | 102 | int main() { 103 | // cout << NextPalindrome_v3(gets(stdin)); 104 | cout << NextPalindrome_v3(24) << '\n'; // expected output: 33 105 | cout << NextPalindrome_v3(2) << '\n'; // expected output: 3 106 | cout << NextPalindrome_v3(180) << '\n'; // expected output: 181 107 | 108 | return 0; 109 | } 110 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/44_largest_pair.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Largest Pair 3 | 4 | Using the C++ language, have the function LargestPair(num) take the num 5 | parameter being passed and determine the largest double digit number within the 6 | whole number. For example: if num is 4759472 then your program should return 94 7 | because that is the largest double digit number. The input will always contain 8 | at least two positive digits. 9 | 10 | Sample test cases: 11 | 12 | Input: 453857 13 | Output: 85 14 | 15 | Input: 363223311 16 | Output: 63 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | using namespace std; 26 | 27 | int64_t LargestPair_v1(int64_t num) { 28 | num = abs(num); 29 | 30 | string num_str{to_string(num)}; 31 | 32 | priority_queue two_digit_numbers{}; 33 | 34 | for (size_t i{}; i < num_str.length() - 1; ++i) { 35 | two_digit_numbers.push((num_str[i] - '0') * 10 + (num_str[i + 1] - '0')); 36 | } 37 | 38 | return two_digit_numbers.top(); 39 | } 40 | 41 | int LargestPair_v2(int64_t num) { 42 | num = abs(num); 43 | int current_max_num{}; 44 | 45 | while (num > 9) { 46 | int current_num = num % 100; 47 | num /= 10; 48 | if (current_num > current_max_num) 49 | current_max_num = current_num; 50 | } 51 | 52 | return current_max_num; 53 | } 54 | 55 | int LargestPair_v3(int64_t num) { 56 | num = abs(num); 57 | string num_str{to_string(num)}; 58 | int current_max_num{}; 59 | 60 | for (size_t i{}; i < num_str.length() - 1; ++i) { 61 | const int current_num{(num_str[i] - '0') * 10 + (num_str[i + 1] - '0')}; 62 | if (current_num > current_max_num) 63 | current_max_num = current_num; 64 | } 65 | 66 | return current_max_num; 67 | } 68 | 69 | int main() { 70 | // cout << LargestPair_v2(gets(stdin)); 71 | cout << LargestPair_v2(4759472) << '\n'; // expected output: 94 72 | cout << LargestPair_v2(453857) << '\n'; // expected output: 85 73 | cout << LargestPair_v2(363223311) << '\n'; // expected output: 63 74 | cout << LargestPair_v2(5673536634) << '\n'; // expected output: 73 75 | 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/45_nonrepeating_character.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Nonrepeating Character 3 | 4 | Using the C++ language, have the function NonrepeatingCharacter(str) take the 5 | str parameter being passed, which will contain only alphabetic characters and 6 | spaces, and return the first non-repeating character. For example: if str is 7 | "agettkgaeee" then your program should return k. The string will always contain 8 | at least one character and there will always be at least one non-repeating 9 | character. 10 | 11 | Sample test cases: 12 | 13 | Input: "abcdef" 14 | Output: "a" 15 | 16 | Input: "hello world hi hey" 17 | Output: "w" 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | using namespace std; 32 | 33 | char find_first_non_repeating_character_v1(string str) { 34 | unordered_set visited_chars{' '}; 35 | 36 | for (const char ch : str) { 37 | if (0U == visited_chars.count(ch)) { 38 | if (1 == count(begin(str), end(str), ch)) 39 | return ch; 40 | visited_chars.emplace(ch); 41 | } 42 | } 43 | 44 | return 0; 45 | } 46 | 47 | char find_first_non_repeating_character_v2(string str) { 48 | constexpr const size_t buffer_size{256U}; 49 | constexpr const int int_max{numeric_limits::max()}; 50 | 51 | int char_index[buffer_size]; 52 | fill(char_index, char_index + buffer_size, int_max); 53 | map first_char_index; 54 | 55 | for (size_t i{}; i < str.length(); ++i) { 56 | if (' ' != str[i]) { 57 | if (int_max == char_index[static_cast(str[i])]) { 58 | first_char_index[i] = str[i]; 59 | char_index[static_cast(str[i])] = i; 60 | } else if (-1 != char_index[static_cast(str[i])]) { 61 | first_char_index.erase(char_index[static_cast(str[i])]); 62 | char_index[static_cast(str[i])] = -1; 63 | } 64 | } 65 | } 66 | 67 | return !first_char_index.empty() ? cbegin(first_char_index)->second : 0; 68 | } 69 | 70 | char find_first_non_repeating_character_v3(string str) { 71 | if (str.empty()) 72 | return 0; 73 | unordered_set visited_chars{' '}; 74 | 75 | for (size_t i{}; i < str.length() - 1; ++i) { 76 | if (0U == visited_chars.count(str[i])) { 77 | if (string::npos == str.find(str[i], i + 1)) 78 | return str[i]; 79 | visited_chars.emplace(str[i]); 80 | } 81 | } 82 | 83 | return str.back(); 84 | } 85 | 86 | int main() { 87 | // cout << find_first_non_repeating_character_v3(gets(stdin)); 88 | cout << find_first_non_repeating_character_v2("agettkgaeee") 89 | << '\n'; // expected output: 'k' 90 | cout << find_first_non_repeating_character_v2("abcdef") 91 | << '\n'; // expected output: 'a' 92 | cout << find_first_non_repeating_character_v2("hello world hi hey") 93 | << '\n'; // expected output: 'w' 94 | 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/47_bitwise_two.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Bitwise Two 3 | 4 | Using the C++ language, have the function BitwiseTwo(strArr) take the array of 5 | strings stored in strArr, which will only contain two strings of equal length 6 | that represent binary numbers, and return a final binary string that performed 7 | the bitwise AND operation on both strings. A bitwise AND operation places a 1 in 8 | the new string where there is a 1 in both locations in the binary strings, 9 | otherwise it places a 0 in that spot. For example: if strArr is ["10111", 10 | "01101"] then your program should return the string "00101" 11 | 12 | Sample test cases: 13 | 14 | Input: "100", "000" 15 | Output: "000" 16 | 17 | Input: "10100", "11100" 18 | Output: "10100" 19 | */ 20 | 21 | #include 22 | #include 23 | 24 | using namespace std; 25 | 26 | string BitwiseTwo_v1(string* binary_numbers, const size_t str_arr_size) { 27 | if (nullptr == binary_numbers || str_arr_size < 2U || 28 | binary_numbers[0].length() != binary_numbers[1].length()) 29 | return "not possible"; 30 | 31 | for (size_t i{}; i < binary_numbers[0].length(); ++i) { 32 | if ('0' == binary_numbers[1][i]) 33 | binary_numbers[0][i] = '0'; 34 | } 35 | 36 | return move(binary_numbers[0]); 37 | } 38 | 39 | string BitwiseTwo_v2(string* binary_numbers, const size_t str_arr_size) { 40 | if (nullptr == binary_numbers || str_arr_size < 2U || 41 | binary_numbers[0].length() != binary_numbers[1].length()) 42 | return "not possible"; 43 | 44 | unsigned long result{stoul(binary_numbers[0], nullptr, 2) & 45 | stoul(binary_numbers[1], nullptr, 2)}; 46 | 47 | int index = binary_numbers[0].length(); 48 | 49 | while (0U != result) { 50 | binary_numbers[0][--index] = '0' + result % 2; 51 | result /= 2; 52 | } 53 | 54 | while (index > 0) 55 | binary_numbers[0][--index] = '0'; 56 | 57 | return move(binary_numbers[0]); 58 | } 59 | 60 | int main() { 61 | // string str_arr1[] = gets(stdin); 62 | // cout << BitwiseTwo_v1(str_arr1, sizeof(str_arr1)/sizeof(*str_arr1)); 63 | string str_arr2[]{"10111", "01101"}; 64 | cout << BitwiseTwo_v1(str_arr2, sizeof(str_arr2) / sizeof(*str_arr2)) 65 | << '\n'; // expected output: "00101" 66 | string str_arr3[]{"100", "000"}; 67 | cout << BitwiseTwo_v1(str_arr3, sizeof(str_arr3) / sizeof(*str_arr3)) 68 | << '\n'; // expected output: "000" 69 | string str_arr4[]{"10100", "11100"}; 70 | cout << BitwiseTwo_v1(str_arr4, sizeof(str_arr4) / sizeof(*str_arr4)) 71 | << '\n'; // expected output: "10100" 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/48_power_set_count.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Power Set Count 3 | 4 | Using the C++ language, have the function PowerSetCount(arr) take the array of 5 | integers stored in arr, and return the length of the power set (the number of 6 | all possible sets) that can be generated. For example: if arr is [1, 2, 3], then 7 | the following sets form the power set: 8 | 9 | [] 10 | [1] 11 | [2] 12 | [3] 13 | [1, 2] 14 | [1, 3] 15 | [2, 3] 16 | [1, 2, 3] 17 | 18 | You can see above all possible sets, along with the empty set, are generated. 19 | Therefore, for this input, your program should return 8. 20 | 21 | Sample test cases: 22 | 23 | Input: 1, 2, 3, 4 24 | Output: 16 25 | 26 | Input: 5, 6 27 | Output: 4 28 | */ 29 | 30 | #include 31 | #include 32 | 33 | using namespace std; 34 | 35 | template 36 | void unused(Args&&...) {} 37 | 38 | size_t PowerSetCount_v1(const int* numbers, const size_t numbers_size) { 39 | unused(numbers); 40 | return pow(static_cast(2U), numbers_size); 41 | } 42 | 43 | size_t PowerSetCount_v2(const int* numbers, const size_t numbers_size) { 44 | unused(numbers); 45 | return 1U << numbers_size; 46 | } 47 | 48 | int main() { 49 | // const int A[] = gets(stdin); 50 | // cout << PowerSetCount_v2(A, sizeof(A)/sizeof(*A)); 51 | const int b[]{1, 2, 3}; 52 | cout << PowerSetCount_v2(b, sizeof(b) / sizeof(*b)) 53 | << '\n'; // expected output: 8 54 | const int c[]{1, 2, 3, 4}; 55 | cout << PowerSetCount_v2(c, sizeof(c) / sizeof(*c)) 56 | << '\n'; // expected output: 16 57 | const int d[]{5, 6}; 58 | cout << PowerSetCount_v2(d, sizeof(d) / sizeof(*d)) 59 | << '\n'; // expected output: 4 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/49_product_digits.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Product Digits 3 | 4 | Using the C++ language, have the function ProductDigits(num) take the num 5 | parameter being passed which will be a positive integer, and determine the least 6 | amount of digits you need to multiply to produce it. For example: if num is 24 7 | then you can multiply 8 by 3 which produces 24, so your program should return 2 8 | because there is a total of only 2 digits that are needed. Another example: if 9 | num is 90, you can multiply 10 * 9, so in this case your program should output 3 10 | because you cannot reach 90 without using a total of 3 digits in your 11 | multiplication. 12 | 13 | Sample test cases: 14 | 15 | Input: 6 16 | Output: 2 17 | 18 | Input: 23 19 | Output: 3 20 | */ 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | using namespace std; 27 | 28 | constexpr size_t get_number_length(size_t number) { 29 | size_t len{}; 30 | 31 | while (0U != number) { 32 | number /= 10; 33 | ++len; 34 | } 35 | 36 | return len; 37 | } 38 | 39 | size_t ProductDigits_v1(const size_t num) { 40 | const size_t num_len{to_string(num).length()}; 41 | size_t min_len{num_len + 1}; 42 | 43 | for (size_t i{2U}; i < static_cast(sqrt(num)) + 1; ++i) { 44 | if (num % i == 0U) { 45 | const size_t factor{num / i}; 46 | const string result{to_string(i) + to_string(factor)}; 47 | if (result.length() < min_len) { 48 | min_len = result.length(); 49 | if (num_len == min_len) 50 | return min_len; 51 | } else if (result.length() > min_len) 52 | break; 53 | } 54 | } 55 | 56 | return min_len; 57 | } 58 | 59 | size_t ProductDigits_v2(const size_t num) { 60 | const size_t num_len{get_number_length(num)}; 61 | size_t min_len{num_len + 1}; 62 | 63 | for (size_t i{2U}; i < static_cast(sqrt(num)) + 1; ++i) { 64 | if (num % i == 0U) { 65 | const size_t factor{num / i}; 66 | const size_t current_len{get_number_length(i) + 67 | get_number_length(factor)}; 68 | if (current_len < min_len) { 69 | min_len = current_len; 70 | if (num_len == min_len) 71 | return min_len; 72 | } else if (current_len > min_len) 73 | break; 74 | } 75 | } 76 | 77 | return min_len; 78 | } 79 | 80 | int main() { 81 | // cout << ProductDigits_v2(gets(stdin)); 82 | cout << ProductDigits_v2(24) << '\n'; // expected output: 2 83 | cout << ProductDigits_v2(90) << '\n'; // expected output: 3 84 | cout << ProductDigits_v2(6) << '\n'; // expected output: 2 85 | cout << ProductDigits_v2(23) << '\n'; // expected output: 3 86 | cout << ProductDigits_v2(1) << '\n'; // expected output: 2 87 | cout << ProductDigits_v2(79) << '\n'; // expected output: 3 88 | cout << ProductDigits_v2(1764) << '\n'; // expected output: 4 89 | cout << ProductDigits_v2(1024) << '\n'; // expected output: 4 90 | 91 | return 0; 92 | } 93 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/59_triangle_row.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Triangle Row 3 | 4 | Using the C++ language, have the function TriangleRow(num) take num which will 5 | be a positive integer representing some row from Pascal's triangle. Pascal's 6 | triangle starts with a [1] at the 0th row of the triangle. Then the first row is 7 | [1, 1] and the second row is [1, 2, 1]. The next row begins with 1 and ends with 8 | 1, and the inside of the row is determined by adding the k-1 and kth elements 9 | from the previous row. The next row in the triangle would then be [1, 3, 3, 1], 10 | and so on. The input will be some positive integer and your goal is to return 11 | the sum of that row. For example: if num is 4 then your program should return 12 | the sum of 1 + 4 + 6 + 4 + 1 which is 16. 13 | 14 | 1. 1 15 | 2. 1 1 16 | 3. 1 2 1 17 | 4. 1 3 3 1 18 | 5. 1 4 6 4 1 19 | 6. 1 5 10 10 5 1 20 | 7. 1 6 15 20 15 6 1 21 | 22 | Sample test cases: 23 | 24 | Input: 1 25 | Output: 2 26 | 27 | Input: 2 28 | Output: 4 29 | */ 30 | 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | using namespace std; 37 | 38 | int TriangleRow(int num) { 39 | num = abs(num); 40 | 41 | if (num <= 2) { 42 | switch (num) { 43 | case 0: 44 | return 1; 45 | 46 | case 1: 47 | return 2; 48 | 49 | case 2: 50 | return 4; 51 | } 52 | } 53 | 54 | vector prev_pascal_triangle{1, 2, 1}; 55 | 56 | vector next_pascal_triangle{1}; 57 | 58 | for (int i{3}; i <= num; i++) { 59 | for (size_t j{}; j < prev_pascal_triangle.size() - 1; j++) 60 | next_pascal_triangle.emplace_back(prev_pascal_triangle[j] + 61 | prev_pascal_triangle[j + 1]); 62 | 63 | next_pascal_triangle.emplace_back(1); 64 | 65 | prev_pascal_triangle = {1}; // prev_pascal_triangle.assign({1}); 66 | 67 | swap(prev_pascal_triangle, next_pascal_triangle); 68 | } 69 | 70 | return accumulate(begin(prev_pascal_triangle), end(prev_pascal_triangle), 0); 71 | } 72 | 73 | int main() { 74 | // cout << TriangleRow(gets(stdin)); 75 | cout << TriangleRow(4) << '\n'; // expected output: 16 76 | cout << TriangleRow(1) << '\n'; // expected output: 2 77 | cout << TriangleRow(2) << '\n'; // expected output: 4 78 | cout << TriangleRow(3) << '\n'; // expected output: 8 79 | 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/64_largest_four.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Largest Four 3 | 4 | Using the C++ language, have the function LargestFour(arr) take the array of 5 | integers stored in arr, and find the four largest elements and return their sum. 6 | For example: if arr is [4, 5, -2, 3, 1, 2, 6, 6] then the four largest elements 7 | in this array are 6, 6, 4, and 5 and the total sum of these numbers is 21, so 8 | your program should return 21. If there are less than four numbers in the array 9 | your program should return the sum of all the numbers in the array. 10 | 11 | Sample test cases: 12 | 13 | Input: 1, 1, 1, -5 14 | Output: -2 15 | 16 | Input: 0, 0, 2, 3, 7, 1 17 | Output: 13 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | using namespace std; 25 | 26 | int LargestFour(int* arr, const size_t arr_size) { 27 | sort(arr, arr + arr_size, [](const int ln, const int rn) { return ln > rn; }); 28 | 29 | const size_t last_index{arr_size >= 4 ? 4 : arr_size}; 30 | 31 | return accumulate(arr, arr + last_index, 0); 32 | } 33 | 34 | int main() { 35 | // int A[] = gets(stdin); 36 | // cout << LargestFour(A, sizeof(A)/sizeof(*A)); 37 | int B[] = {4, 5, -2, 3, 1, 2, 6, 6}; 38 | cout << LargestFour(B, sizeof(B) / sizeof(*B)) 39 | << '\n'; // expected output: 21 40 | int C[] = {1, 1, 1, -5}; 41 | cout << LargestFour(C, sizeof(C) / sizeof(*C)) 42 | << '\n'; // expected output: -2 43 | int D[] = {0, 0, 2, 3, 7, 1}; 44 | cout << LargestFour(D, sizeof(D) / sizeof(*D)) 45 | << '\n'; // expected output: 13 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/65_distinct_characters.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Distinct Characters 3 | 4 | Using the C++ language, have the function DistinctCharacters(str) take the str 5 | parameter being passed and determine if it contains at least 10 distinct 6 | characters, if so, then your program should return the string true, otherwise it 7 | should return the string false. For example: if str is "abc123kkmmmm?" then your 8 | program should return the string false because this string contains only 9 9 | distinct characters: a, b, c, 1, 2, 3, k, m, ? adds up to 9. 10 | 11 | Sample test cases: 12 | 13 | Input: "12334bbmma:=6" 14 | Output: "true" 15 | 16 | Input: "eeeemmmmmmmmm1000" 17 | Output: "false" 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | using namespace std; 26 | 27 | string trim(const string& str) { 28 | const size_t str_len{str.length()}; 29 | 30 | if (!str_len) 31 | return string{}; 32 | 33 | size_t first{}, last{str_len - 1}; 34 | 35 | for (; first <= last; ++first) { 36 | if (!isspace(str[first])) 37 | break; 38 | } 39 | 40 | if (first > last) 41 | return string{}; 42 | 43 | for (; last > first; --last) { 44 | if (!isspace(str[last])) 45 | break; 46 | } 47 | 48 | return str.substr(first, last - first + 1); 49 | } 50 | 51 | string DistinctCharacters_v1(string str) { 52 | str = trim(str); 53 | 54 | unordered_set distinct_characters{}; 55 | 56 | for (const char ch : str) { 57 | distinct_characters.insert(ch); 58 | 59 | if (distinct_characters.size() >= 10) 60 | return "true"; 61 | } 62 | 63 | return "false"; 64 | } 65 | 66 | string DistinctCharacters_v2(string str) { 67 | str = trim(str); 68 | 69 | unordered_set distinct_characters(begin(str), end(str)); 70 | 71 | if (distinct_characters.size() >= 10) 72 | return "true"; 73 | 74 | return "false"; 75 | } 76 | 77 | int main() { 78 | // cout << DistinctCharacters_v2(move(string{gets(stdin)})); 79 | cout << DistinctCharacters_v2(move(string{"abc123kkmmmm?"})) 80 | << '\n'; // expected output: "false" 81 | cout << DistinctCharacters_v2(move(string{"12334bbmma:=6"})) 82 | << '\n'; // expected output: "true" 83 | cout << DistinctCharacters_v2(move(string{"eeeemmmmmmmmm1000"})) 84 | << '\n'; // expected output: "false" 85 | return 0; 86 | } 87 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/69_simple_evens.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Simple Evens 3 | 4 | Using the C++ language, have the function SimpleEvens(num) check whether every 5 | single number in the passed in parameter is even. If so, return the string true, 6 | otherwise return the string false. For example: if num is 4602225 your program 7 | should return the string false because 5 is not an even number. 8 | 9 | Sample test cases: 10 | 11 | Input: 2222220222 12 | Output: "true" 13 | 14 | Input: 20864646452 15 | Output: "false" 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | using namespace std; 24 | 25 | string SimpleEvens_v1(const int64_t num) { 26 | const string num_str{to_string(num)}; 27 | 28 | for (const char ch : num_str) { 29 | if (ch % 2 == 1) 30 | return "false"; // if ((ch - '0') % 2 == 1) return "false"; 31 | } 32 | 33 | return "true"; 34 | } 35 | 36 | string SimpleEvens_v2(const int64_t num) { 37 | const string num_str{to_string(num)}; 38 | 39 | return (all_of(begin(num_str), end(num_str), 40 | [](const char ch) { return ch % 2 == 0; }) 41 | ? "true" 42 | : "false"); 43 | } 44 | 45 | int main() { 46 | // cout << SimpleEvens_v2(gets(stdin)); 47 | cout << SimpleEvens_v2(4602225LL) << '\n'; // expected output: "false" 48 | cout << SimpleEvens_v2(2222220222LL) << '\n'; // expected output: "true" 49 | cout << SimpleEvens_v2(20864646452LL) << '\n'; // expected output: "false" 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/71_sum_multiplier.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Sum Multiplier 3 | 4 | Using the C++ language, have the function SumMultiplier(arr) take the array of 5 | numbers stored in arr and return the string true if any two numbers can be 6 | multiplied so that the answer is greater than double the sum of all the elements 7 | in the array. If not, return the string false. For example: if arr is [2, 5, 6, 8 | -6, 16, 2, 3, 6, 5, 3] then the sum of all these elements is 42 and doubling it 9 | is 84. There are two elements in the array, 16 * 6 = 96 and 96 is greater than 10 | 84, so your program should return the string true. 11 | 12 | Sample test cases: 13 | 14 | Input: 2, 2, 2, 2, 4, 1 15 | Output: "false" 16 | 17 | Input: 1, 1, 2, 10, 3, 1, 12 18 | Output: "true" 19 | */ 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | using namespace std; 27 | 28 | string SumMultiplier(int* arr, const size_t arr_size) { 29 | if (arr_size < 2) 30 | return "false"; 31 | 32 | sort(arr, arr + arr_size, 33 | [](const int ln, const int rn) { return (ln > rn); }); 34 | 35 | const int double_sum{2 * accumulate(arr, arr + arr_size, 0)}; 36 | 37 | if (arr[0] * arr[1] > double_sum) 38 | return "true"; 39 | 40 | return "false"; 41 | } 42 | 43 | int main() { 44 | // int A[] = gets(stdin); 45 | // cout << SumMultiplier(A, sizeof(A)/sizeof(*A)); 46 | int B[] = {2, 5, 6, -6, 16, 2, 3, 6, 5, 3}; 47 | cout << SumMultiplier(B, sizeof(B) / sizeof(*B)) 48 | << '\n'; // expected output: "true" 49 | int C[] = {2, 2, 2, 2, 4, 1}; 50 | cout << SumMultiplier(C, sizeof(C) / sizeof(*C)) 51 | << '\n'; // expected output: "false" 52 | int D[] = {1, 1, 2, 10, 3, 1, 12}; 53 | cout << SumMultiplier(D, sizeof(D) / sizeof(*D)) 54 | << '\n'; // expected output: "true" 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/72_string_merge.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: String Merge 3 | 4 | Using the C++ language, have the function StringMerge(str) read the str 5 | parameter being passed which will contain a large string of alphanumeric 6 | characters with a single asterisk character splitting the string evenly into two 7 | separate strings. Your goal is to return a new string by pairing up the 8 | characters in the corresponding locations in both strings. For example: if str 9 | is "abc1*kyoo" then your program should return the string akbyco1o because a 10 | pairs with k, b pairs with y, etc. The string will always split evenly with the 11 | asterisk in the center. 12 | 13 | Sample test cases: 14 | 15 | Input: "aaa*bbb" 16 | Output: "ababab" 17 | 18 | Input: "123hg*aaabb" 19 | Output: "1a2a3ahbgb" 20 | */ 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | using namespace std; 28 | 29 | string trim(const string& str) { 30 | const size_t str_len{str.length()}; 31 | 32 | if (!str_len) 33 | return string{}; 34 | 35 | size_t first{}, last{str_len - 1}; 36 | 37 | for (; first <= last; ++first) { 38 | if (!isspace(str[first])) 39 | break; 40 | } 41 | 42 | if (first > last) 43 | return string{}; 44 | 45 | for (; last > first; --last) { 46 | if (!isspace(str[last])) 47 | break; 48 | } 49 | 50 | return str.substr(first, last - first + 1); 51 | } 52 | 53 | string StringMerge_v1(string str) { 54 | str = trim(str); 55 | 56 | const size_t str_len{str.length()}; 57 | 58 | if (str_len < 3) 59 | return "not possible"; 60 | 61 | const size_t delim_pos{str.find('*', 1)}; 62 | 63 | if (string::npos == delim_pos) 64 | return "not possible"; 65 | 66 | if (2 * delim_pos != str_len - 1) 67 | return "not possible"; 68 | 69 | string result{}; 70 | result.reserve(str_len - 1); 71 | 72 | for (size_t i{}, j{delim_pos + 1}; i != delim_pos; i++, j++) { 73 | result.push_back(str[i]); 74 | result.push_back(str[j]); 75 | } 76 | 77 | return result; 78 | } 79 | 80 | string StringMerge_v2(string str) { 81 | str = trim(str); 82 | 83 | const size_t str_len{str.length()}; 84 | 85 | if (str_len < 3) 86 | return "not possible"; 87 | 88 | const size_t delim_pos{str.find('*', 1)}; 89 | 90 | if (string::npos == delim_pos) 91 | return "not possible"; 92 | 93 | if (2 * delim_pos != str_len - 1) 94 | return "not possible"; 95 | 96 | ostringstream oss{}; 97 | 98 | for (size_t i{}, j{delim_pos + 1}; i != delim_pos; i++, j++) 99 | oss << str[i] << str[j]; 100 | 101 | return oss.str(); 102 | } 103 | 104 | int main() { 105 | // cout << StringMerge_v1(move(string{gets(stdin)})); 106 | cout << StringMerge_v1(move(string{"abc1*kyoo"})) 107 | << '\n'; // expected output: "akbyco1o" 108 | cout << StringMerge_v1(move(string{"aaa*bbb"})) 109 | << '\n'; // expected output: "ababab" 110 | cout << StringMerge_v1(move(string{"123hg*aaabb"})) 111 | << '\n'; // expected output: "1a2a3ahbgb" 112 | return 0; 113 | } 114 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/73_one_decremented.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: One Decremented 3 | 4 | Using the C++ language, have the function OneDecremented(num) count how many 5 | times a digit appears that is exactly one less than the previous digit. For 6 | example: if num is 5655984 then your program should return 2 because 5 appears 7 | directly after 6 and 8 appears directly after 9. The input will always contain 8 | at least 1 digit. 9 | 10 | Sample test cases: 11 | 12 | Input: 56 13 | Output: 0 14 | 15 | Input: 9876541110 16 | Output: 6 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | using namespace std; 24 | 25 | size_t OneDecremented(const int64_t num) { 26 | const string num_str{to_string(num)}; 27 | 28 | if (num_str.length() < 2) 29 | return 0; 30 | 31 | size_t freq{}; 32 | 33 | for (size_t i{}; i < num_str.length() - 1; i++) { 34 | if (1 == static_cast(num_str[i] - num_str[i + 1])) 35 | freq++; 36 | } 37 | 38 | return freq; 39 | } 40 | 41 | int main() { 42 | // cout << OneDecremented(gets(stdin)); 43 | cout << OneDecremented(5655984LL) << '\n'; // expected output: 2 44 | cout << OneDecremented(56LL) << '\n'; // expected output: 0 45 | cout << OneDecremented(9876541110LL) << '\n'; // expected output: 6 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/74_element_merger.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Element Merger 3 | 4 | Using the C++ language, have the function ElementMerger(arr) take the array of 5 | positive integers stored in arr and perform the following algorithm: 6 | continuously get the difference of adjacent integers to create a new array of 7 | integers, then do the same for the new array until a single number is left and 8 | return that number. For example: if arr is [4, 5, 1, 2, 7] then taking the 9 | difference of each pair of elements produces the following new array: [1, 4, 1, 10 | 5]. Then do the same for this new array to produce [3, 3, 4] -> [0, 1] -> 1. So 11 | for this example your program should return the number 1 because that is what's 12 | left at the end. 13 | 14 | Sample test cases: 15 | 16 | Input: 5, 7, 16, 1, 2 17 | Output: 7 18 | 19 | Input: 1, 1, 1, 2 20 | Output: 1 21 | */ 22 | 23 | #include 24 | #include 25 | 26 | using namespace std; 27 | 28 | int ElementMerger(int* arr, size_t arr_size) { 29 | while (arr_size > 1) { 30 | for (size_t i{}; i < arr_size - 1; i++) 31 | arr[i] = abs(arr[i + 1] - arr[i]); 32 | 33 | arr_size--; 34 | } 35 | 36 | return arr[0]; 37 | } 38 | 39 | int main() { 40 | // int A[] = gets(stdin); 41 | // cout << ElementMerger(A, sizeof(A)/sizeof(*A)); 42 | int A1[] = {4, 5, 1, 2, 7}; 43 | cout << ElementMerger(A1, sizeof(A1) / sizeof(*A1)) 44 | << '\n'; // expected output: 1 45 | int A2[] = {5, 7, 16, 1, 2}; 46 | cout << ElementMerger(A2, sizeof(A2) / sizeof(*A2)) 47 | << '\n'; // expected output: 7 48 | int A3[] = {1, 1, 1, 2}; 49 | cout << ElementMerger(A3, sizeof(A3) / sizeof(*A3)) 50 | << '\n'; // expected output: 1 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/75_gcf.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: GCF 3 | 4 | Using the C++ language, have the function GCF(arr) take the array of numbers 5 | stored in arr which will always contain only two positive integers, and return 6 | the greatest common factor of them. For example: if arr is [45, 12] then your 7 | program should return 3. There will always be two elements in the array and they 8 | will be positive integers. 9 | 10 | Sample test cases: 11 | 12 | Input: 1, 6 13 | Output: 1 14 | 15 | Input: 12, 28 16 | Output: 4 17 | */ 18 | 19 | #include 20 | #include 21 | 22 | using namespace std; 23 | 24 | int GCF(const int* arr, const size_t arr_size) { 25 | if (!arr) 26 | return 0; 27 | 28 | if (arr_size < 2) 29 | return *arr; 30 | 31 | const int min_number{min(arr[0], arr[1])}; 32 | 33 | for (int gcf{min_number}; gcf > 1; gcf--) { 34 | if ((arr[0] % gcf == 0) && (arr[1] % gcf == 0)) 35 | return gcf; 36 | } 37 | 38 | return 1; 39 | } 40 | 41 | int main() { 42 | // const int A[] = gets(stdin); 43 | // cout << GCF(A, sizeof(A)/sizeof(*A)); 44 | const int B[] = {45, 12}; 45 | cout << GCF(B, sizeof(B) / sizeof(*B)) << '\n'; // expected output: 3 46 | const int C[] = {1, 6}; 47 | cout << GCF(C, sizeof(C) / sizeof(*C)) << '\n'; // expected output: 1 48 | const int D[] = {12, 28}; 49 | cout << GCF(D, sizeof(D) / sizeof(*D)) << '\n'; // expected output: 4 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/78_palindrome_swapper.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Palindrome Swapper 3 | 4 | Using the C++ language, have the function PalindromeSwapper(str) take the str 5 | parameter being passed and determine if a palindrome can be created by swapping 6 | two adjacent characters in the string. If it is possible to create a palindrome, 7 | then your program should return the palindrome, if not then return the string 8 | -1. The input string will only contain alphabetic characters. 9 | 10 | For example: if str is "rcaecar" then you can create a palindrome by swapping 11 | the second and third characters, so your program should return the string 12 | racecar which is the final palindromic string. 13 | 14 | Sample test cases: 15 | 16 | Input: "anna" 17 | Output: "anna" 18 | 19 | Input: "kyaak" 20 | Output: "kayak" 21 | */ 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | using namespace std; 29 | 30 | string trim(const string& str) { 31 | const size_t str_len{str.length()}; 32 | 33 | if (!str_len) 34 | return string{}; 35 | 36 | size_t first{}, last{str_len - 1}; 37 | 38 | for (; first <= last; ++first) { 39 | if (!isspace(str[first])) 40 | break; 41 | } 42 | 43 | if (first > last) 44 | return string{}; 45 | 46 | for (; last > first; --last) { 47 | if (!isspace(str[last])) 48 | break; 49 | } 50 | 51 | return str.substr(first, last - first + 1); 52 | } 53 | 54 | bool is_string_palindrome(const string& str) { 55 | string reversed_str{str}; 56 | 57 | reverse(begin(reversed_str), end(reversed_str)); 58 | 59 | if (str == reversed_str) 60 | return true; 61 | 62 | return false; 63 | } 64 | 65 | string palindrome_swapper(string str) { 66 | str = trim(str); 67 | 68 | const size_t str_len{str.length()}; 69 | 70 | if (str_len < 2) 71 | return "-1"; 72 | 73 | if ((2 == str_len) && (str[0] == str[1])) 74 | return str; 75 | 76 | // if (is_string_palindrome(str)) return str; 77 | 78 | for (size_t i{}; i < str_len - 1; i++) { 79 | swap(str[i], str[i + 1]); 80 | 81 | if (is_string_palindrome(str)) 82 | return str; 83 | 84 | swap(str[i], str[i + 1]); 85 | } 86 | 87 | return "-1"; 88 | } 89 | 90 | int main() { 91 | // cout << palindrome_swapper(move(string{gets(stdin)})); 92 | cout << palindrome_swapper(move(string{"madam"})) 93 | << '\n'; // expected output: "madam" 94 | cout << palindrome_swapper(move(string{"rcaecar"})) 95 | << '\n'; // expected output: "racecar" 96 | cout << palindrome_swapper(move(string{"anna"})) 97 | << '\n'; // expected output: "anna" 98 | cout << palindrome_swapper(move(string{"kyaak"})) 99 | << '\n'; // expected output: "kayak" 100 | return 0; 101 | } 102 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/79_remove_brackets.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coding challenge: Remove Brackets 3 | 4 | Using the C++ language, have the function RemoveBrackets(str) take the str 5 | string parameter being passed, which will contain only the characters "(" and 6 | ")", and determine the minimum number of brackets that need to be removed to 7 | create a string of correctly matched brackets. For example: if str is "(()))" 8 | then your program should return the number 1. The answer could potentially be 0, 9 | and there will always be at least one set of matching brackets in the string. 10 | 11 | Sample test cases: 12 | 13 | Input: "(())()(((" 14 | Output: 3 15 | 16 | Input: "(()(" 17 | Output: 2 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | using namespace std; 26 | 27 | string trim(const string& str) { 28 | const size_t str_len{str.length()}; 29 | 30 | if (!str_len) 31 | return string{}; 32 | 33 | size_t first{}, last{str_len - 1}; 34 | 35 | for (; first <= last; ++first) { 36 | if (!isspace(str[first])) 37 | break; 38 | } 39 | 40 | if (first > last) 41 | return string{}; 42 | 43 | for (; last > first; --last) { 44 | if (!isspace(str[last])) 45 | break; 46 | } 47 | 48 | return str.substr(first, last - first + 1); 49 | } 50 | 51 | string RemoveBrackets(string str) { 52 | str = trim(str); 53 | 54 | int balance_count{}; 55 | 56 | size_t start{}; 57 | 58 | while (')' == str[start]) { 59 | start++; 60 | balance_count++; 61 | } 62 | 63 | size_t last{str.length() - 1}; 64 | 65 | while ('(' == str[last]) { 66 | last--; 67 | balance_count++; 68 | } 69 | 70 | for (size_t i{start}; i <= last; i++) { 71 | if ('(' == str[i]) 72 | balance_count++; 73 | else if (')' == str[i]) 74 | balance_count--; 75 | } 76 | 77 | return to_string(abs(balance_count)); 78 | } 79 | 80 | int main() { 81 | // cout << RemoveBrackets(move(string{gets(stdin)})); 82 | cout << RemoveBrackets(move(string{"(()))"})) << '\n'; // expected output: 1 83 | cout << RemoveBrackets(move(string{"(())()((("})) 84 | << '\n'; // expected output: 3 85 | cout << RemoveBrackets(move(string{"(()("})) << '\n'; // expected output: 2 86 | cout << RemoveBrackets(move(string{")(()"})) << '\n'; // expected output: 2 87 | 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/82_moving_median.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Moving Median 3 | 4 | Using the C++ language, have the function MovingMedian(arr) read the array of 5 | numbers stored in arr which will contain a sliding window size, N, as the first 6 | element in the array and the rest will be a list of numbers. Your program should 7 | return the Moving Median for each element based on the element and its N-1 8 | predecessors, where N is the sliding window size. The final output should be a 9 | string with the moving median corresponding to each entry in the original array 10 | separated by commas. 11 | 12 | Note that for the first few elements (until the window size is reached), the 13 | median is computed on a smaller number of entries. For example: if arr is [3, 1, 14 | 3, 5, 10, 6, 4, 3, 1] then your program should output "1,2,3,5,6,6,4,3" 15 | 16 | Sample test cases: 17 | 18 | Input: 5, 2, 4, 6 19 | Output: "2,3,4" 20 | 21 | Input: 3, 0, 0, -2, 0, 2, 0, -2 22 | Output: "0,0,0,0,0,0,0" 23 | */ 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | using namespace std; 32 | 33 | string MovingMedian_v1(vector numbers) { 34 | const size_t numbers_size{numbers.size()}; 35 | 36 | const size_t window_size{static_cast(numbers[0])}; 37 | 38 | string answer{}; 39 | 40 | for (size_t i{1}; i < numbers_size; i++) { 41 | vector window{}; 42 | 43 | for (size_t j{}; (j < window_size) && (j < i); j++) { 44 | window.emplace_back(numbers[i - j]); 45 | } 46 | 47 | sort(begin(window), end(window)); 48 | 49 | const size_t ws{window.size()}; 50 | 51 | if (ws % 2 == 1) 52 | answer += to_string(window[ws / 2]); 53 | else 54 | answer += to_string((window[ws / 2 - 1] + window[ws / 2]) / 2); 55 | 56 | if (i < numbers_size - 1) 57 | answer.push_back(','); 58 | } 59 | 60 | return answer; 61 | } 62 | 63 | string MovingMedian_v2(vector numbers) { 64 | const size_t numbers_size{numbers.size()}; 65 | 66 | const size_t max_window_size{static_cast(numbers[0])}; 67 | 68 | size_t cws{1}; 69 | 70 | ostringstream oss{}; 71 | 72 | for (size_t i{1}; i < numbers_size; i++) { 73 | vector window{}; 74 | 75 | for (size_t j{i + 1 - cws}; j < i + 1; j++) { 76 | window.emplace_back(numbers[j]); 77 | } 78 | 79 | sort(begin(window), end(window)); 80 | 81 | if (cws % 2 == 1) 82 | oss << window[cws / 2]; 83 | else 84 | oss << (window[cws / 2 - 1] + window[cws / 2]) / 2; 85 | 86 | if (i < numbers_size - 1) 87 | oss << ','; 88 | 89 | if (cws < max_window_size) 90 | cws++; 91 | } 92 | 93 | return oss.str(); 94 | } 95 | 96 | int main() { 97 | // cout << MovingMedian_v2(move(vector{gets(stdin)})); 98 | cout << MovingMedian_v2(move(vector{3, 1, 3, 5, 10, 6, 4, 3, 1})) 99 | << '\n'; // expected output: "1,2,3,5,6,6,4,3" 100 | cout << MovingMedian_v2(move(vector{5, 2, 4, 6})) 101 | << '\n'; // expected output: "2,3,4" 102 | cout << MovingMedian_v2(move(vector{3, 0, 0, -2, 0, 2, 0, -2})) 103 | << '\n'; // expected output: "0,0,0,0,0,0,0" 104 | 105 | return 0; 106 | } 107 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/83_group_totals.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Group Totals 3 | 4 | Using the C++ language, have the function GroupTotals(strArr) read in the strArr 5 | parameter containing key:value pairs where the key is a string and the value is 6 | an integer. Your program should return a string with new key:value pairs 7 | separated by a comma such that each key appears only once with the total values 8 | summed up. For example: if strArr is ["B:-1", "A:1", "B:3", "A:5"] then your 9 | program should return the string A:6,B:2. 10 | 11 | Your final output string should return the keys in alphabetical order. Exclude 12 | keys that have a value of 0 after being summed up. 13 | 14 | Sample test cases: 15 | 16 | Input: "X:-1", "Y:1", "X:-4", "B:3", "X:5" 17 | Output: "B:3,Y:1" 18 | 19 | Input: "Z:0", "A:-1" 20 | Output: "A:-1" 21 | */ 22 | #define _CRT_SECURE_NO_WARNINGS 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | using namespace std; 29 | 30 | string GroupTotals(const string* str_arr, const size_t str_arr_size) { 31 | map group_totals{}; 32 | 33 | for (size_t i{}; i < str_arr_size; i++) { 34 | const size_t sep_char_pos{str_arr[i].find(':')}; 35 | const string key{str_arr[i].substr(0, sep_char_pos)}; 36 | const int value{stoi(str_arr[i].substr(sep_char_pos + 1))}; 37 | group_totals[move(key)] += value; 38 | } 39 | 40 | string output{}; 41 | output.reserve(1024); 42 | char buffer[32]; 43 | 44 | for (const pair& total : group_totals) { 45 | if (!total.second) 46 | continue; 47 | sprintf(buffer, "%s:%d,", total.first.c_str(), total.second); 48 | output.append(buffer); 49 | } 50 | 51 | output.pop_back(); 52 | 53 | return output; 54 | } 55 | 56 | int main() { 57 | // const string A[] = gets(stdin); 58 | // cout << GroupTotals(A, sizeof(A) / sizeof(*A)) << '\n'; 59 | const string B[] = {"B:-1", "A:1", "B:3", "A:5"}; 60 | cout << GroupTotals(B, sizeof(B) / sizeof(*B)) 61 | << '\n'; // expected output: "A:6,B:2" 62 | const string C[] = {"X:-1", "Y:1", "X:-4", "B:3", "X:5"}; 63 | cout << GroupTotals(C, sizeof(C) / sizeof(*C)) 64 | << '\n'; // expected output: "B:3,Y:1" 65 | const string D[] = {"Z:0", "A:-1"}; 66 | cout << GroupTotals(D, sizeof(D) / sizeof(*D)) 67 | << '\n'; // expected output: "A:-1" 68 | 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/84_string_changes.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 84. Coderbyte coding challenge: String changes (difficulty level: easy) 3 | 4 | Using the C++ language, have the function StringChanges(str) take the str 5 | parameter being passed, which will be a string containing letters from the 6 | alphabet, and return a new string based on the following rules. Whenever a 7 | capital M is encountered, duplicate the previous character (then remove the M), 8 | and whenever a capital N is encountered remove the next character from the 9 | string (then remove the N). All other characters in the string will be lowercase 10 | letters. For example: "abcNdgM" should return "abcgg". The final string will 11 | never be empty. Sample Test Cases 12 | 13 | Input: "MrtyNNgMM" 14 | Output: "rtyggg" 15 | 16 | Input: "oMoMkkNrrN" 17 | Output: "ooookkr" 18 | */ 19 | 20 | #include 21 | #include 22 | 23 | using namespace std; 24 | 25 | string StringChanges(const string& str) { 26 | const size_t str_len{str.length()}; 27 | string output{}; 28 | output.reserve(str_len); 29 | 30 | size_t i{}; 31 | char prev_char{}; 32 | 33 | while (i < str_len) { 34 | if ('M' == str[i]) { 35 | if (prev_char) 36 | output.push_back(prev_char); 37 | } else if ('N' == str[i]) 38 | i++; 39 | else { 40 | prev_char = str[i]; 41 | output.push_back(str[i]); 42 | } 43 | i++; 44 | } 45 | 46 | return output; 47 | } 48 | 49 | int main() { 50 | // cout << StringChanges(gets(stdin)); 51 | cout << StringChanges("abcNdgM") << '\n'; // expected output: "abcgg" 52 | cout << StringChanges("MrtyNNgMM") << '\n'; // expected output: "rtyggg" 53 | cout << StringChanges("oMoMkkNrrN") << '\n'; // expected output: "ooookkr" 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/85_h_distance.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: H-distance 3 | 4 | Using the C++ language, have the function HDistance(strArr) take the array of 5 | strings stored in strArr, which will only contain two strings of equal length 6 | and return the number of characters at each position that are different between 7 | them. For example: if strArr is ["house", "hours"] then your program should 8 | return 2. The string will always be of equal length and will only contain 9 | lowercase characters from the alphabet and numbers. 10 | 11 | Sample test cases: 12 | 13 | Input: "10011", "10100" 14 | Output: 3 15 | 16 | Input: "abcdef", "defabc" 17 | Output: 6 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | using namespace std; 26 | 27 | string trim(const string& str) { 28 | const size_t str_len{str.length()}; 29 | 30 | if (!str_len) 31 | return string{}; 32 | 33 | size_t first{}, last{str_len - 1}; 34 | 35 | for (; first <= last; ++first) { 36 | if (!isspace(str[first])) 37 | break; 38 | } 39 | 40 | if (first > last) 41 | return string{}; 42 | 43 | for (; last > first; --last) { 44 | if (!isspace(str[last])) 45 | break; 46 | } 47 | 48 | return str.substr(first, last - first + 1); 49 | } 50 | 51 | string HDistance(const string* str_arr, const size_t str_arr_size) { 52 | if (str_arr_size < 2) 53 | return "Not possible!"; 54 | 55 | size_t number_of_ch_differences{}; 56 | 57 | const string str1{trim(str_arr[0])}; 58 | const string str2{trim(str_arr[1])}; 59 | 60 | const size_t str1_len{str_arr[0].length()}; 61 | const size_t str2_len{str_arr[1].length()}; 62 | 63 | const size_t diff_len{str1_len < str2_len ? str2_len - str1_len 64 | : str1_len - str2_len}; 65 | 66 | for (size_t i{}; i < min(str1_len, str2_len); i++) { 67 | if (str1[i] != str2[i]) 68 | number_of_ch_differences++; 69 | } 70 | 71 | if (diff_len) 72 | number_of_ch_differences += diff_len; 73 | 74 | return to_string(number_of_ch_differences); 75 | } 76 | 77 | int main() { 78 | // const string A[] = gets(stdin); 79 | // cout << HDistance(A, sizeof(A)/sizeof(*A)); 80 | const string B[] = {"10011", "10100"}; 81 | cout << HDistance(B, sizeof(B) / sizeof(*B)) << '\n'; // expected output: 3 82 | const string C[] = {"abcdef", "defabc"}; 83 | cout << HDistance(C, sizeof(C) / sizeof(*C)) << '\n'; // expected output: 6 84 | 85 | return 0; 86 | } 87 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/87_h_distance.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: H-distance 3 | 4 | Using the C++ language, have the function HDistance(strArr) take the array of 5 | strings stored in strArr, which will only contain two strings of equal length 6 | and return the number of characters at each position that are different between 7 | them. For example: if strArr is ["house", "hours"] then your program should 8 | return 2. The string will always be of equal length and will only contain 9 | lowercase characters from the alphabet and numbers. 10 | 11 | Sample test cases: 12 | 13 | Input: "10011", "10100" 14 | Output: 3 15 | 16 | Input: "abcdef", "defabc" 17 | Output: 6 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | using namespace std; 26 | 27 | string trim(const string& str) { 28 | const size_t str_len{str.length()}; 29 | 30 | if (!str_len) 31 | return string{}; 32 | 33 | size_t first{}, last{str_len - 1}; 34 | 35 | for (; first <= last; ++first) { 36 | if (!isspace(str[first])) 37 | break; 38 | } 39 | 40 | if (first > last) 41 | return string{}; 42 | 43 | for (; last > first; --last) { 44 | if (!isspace(str[last])) 45 | break; 46 | } 47 | 48 | return str.substr(first, last - first + 1); 49 | } 50 | 51 | string HDistance(const string* str_arr, const size_t str_arr_size) { 52 | if (str_arr_size < 2) 53 | return "Not possible!"; 54 | 55 | size_t number_of_ch_differences{}; 56 | 57 | const string str1{trim(str_arr[0])}; 58 | const string str2{trim(str_arr[1])}; 59 | 60 | const size_t str1_len{str_arr[0].length()}; 61 | const size_t str2_len{str_arr[1].length()}; 62 | 63 | const size_t diff_len{str1_len < str2_len ? str2_len - str1_len 64 | : str1_len - str2_len}; 65 | 66 | for (size_t i{}; i < min(str1_len, str2_len); i++) { 67 | if (str1[i] != str2[i]) 68 | number_of_ch_differences++; 69 | } 70 | 71 | if (diff_len) 72 | number_of_ch_differences += diff_len; 73 | 74 | return to_string(number_of_ch_differences); 75 | } 76 | 77 | int main() { 78 | // const string A[] = gets(stdin); 79 | // cout << HDistance(A, sizeof(A)/sizeof(*A)); 80 | const string B[] = {"10011", "10100"}; 81 | cout << HDistance(B, sizeof(B) / sizeof(*B)) << '\n'; // expected output: 3 82 | const string C[] = {"abcdef", "defabc"}; 83 | cout << HDistance(C, sizeof(C) / sizeof(*C)) << '\n'; // expected output: 6 84 | 85 | return 0; 86 | } 87 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/custom_templates_ex1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | size_t find_missing_number(const size_t* first, const size_t* last) { 9 | if (last - first == 1) 10 | return *first + 1; 11 | 12 | const std::ptrdiff_t middle = (last - first)/2; 13 | if (*(first + middle) == *first + middle) 14 | return find_missing_number(first + middle, last); 15 | return find_missing_number(first, first + middle); 16 | } 17 | 18 | template 19 | typename std::iterator_traits::difference_type 20 | bsearch(ForwardIt first, ForwardIt last, const T& value) { 21 | const ForwardIt lb_pos = std::lower_bound(first, last, value); 22 | return !(lb_pos == last) && (*lb_pos == value) ? std::distance(first, lb_pos) 23 | : -1; 24 | } 25 | 26 | template 27 | bool next_permutation(const BidirIt first, const BidirIt last) { 28 | if (first == last) 29 | return false; 30 | BidirIt i{last}; 31 | if (first == --i) 32 | return false; 33 | 34 | while (true) { 35 | BidirIt i1{i}; 36 | 37 | if (*--i < *i1) { 38 | BidirIt i2{last}; 39 | while (!(*i < *--i2)) 40 | ; 41 | std::iter_swap(i, i2); 42 | std::reverse(i1, last); 43 | return true; 44 | } 45 | if (first == i) { 46 | std::reverse(first, last); 47 | return false; 48 | } 49 | } 50 | } 51 | 52 | template 53 | bool prev_permutation(const BidirIt first, const BidirIt last) { 54 | if (first == last) 55 | return false; 56 | 57 | BidirIt i{last}; 58 | 59 | if (first == --i) 60 | return false; 61 | 62 | while (true) { 63 | BidirIt i1{i}; 64 | if (*i1 < *--i) { 65 | BidirIt i2{last}; 66 | while (!(*--i2 < *i)) 67 | ; 68 | std::iter_swap(i, i2); 69 | std::reverse(i1, last); 70 | return true; 71 | } 72 | if (first == i) { 73 | std::reverse(first, last); 74 | return false; 75 | } 76 | } 77 | } 78 | 79 | using namespace std; 80 | 81 | int main() { 82 | vector numbers{1, 4, 2, 6, 12, 8, 5, 25, 26, 17, 35, 53, 3}; 83 | 84 | sort(begin(numbers), end(numbers)); 85 | 86 | const array values{1, 15, 17, 23, 26, 52, 53}; 87 | 88 | for (const auto target : values) { 89 | const std::iterator_traits::difference_type 90 | position = bsearch(cbegin(numbers), cend(numbers), target); 91 | if (-1 != position) 92 | cout << "Found target number: " << target << " at position: " << position 93 | << '\n'; 94 | else 95 | cout << "Target number (" << target << ") could not be found!\n"; 96 | } 97 | 98 | return 0; 99 | } 100 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/dp_find_sets_of_numbers_ex1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Dynamic programming related coding challenge: 3 | 4 | Find the maximum number of subsets of numbers given in the input array of whole 5 | numbers that add up to the given sum value specified as the first number in the 6 | input array. 7 | 8 | Sample test cases: 9 | 10 | Input: "16", "2", "4", "6", "10" 11 | Output: 2 12 | 13 | Input: "13", "1", "3", "2", "5", "6", "7", "8", "4" 14 | Output: 6 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | using namespace std; 24 | 25 | string trim(const string& str) { 26 | const size_t str_len{str.length()}; 27 | 28 | if (!str_len) 29 | return string{}; 30 | 31 | size_t first{}, last{str_len - 1}; 32 | 33 | for (; first <= last; ++first) { 34 | if (!isspace(str[first])) 35 | break; 36 | } 37 | 38 | if (first > last) 39 | return string{}; 40 | 41 | for (; last > first; --last) { 42 | if (!isspace(str[last])) 43 | break; 44 | } 45 | 46 | return str.substr(first, last - first + 1); 47 | } 48 | 49 | size_t find_number_of_subsets(const char** str_arr, const size_t str_arr_size) { 50 | const int sum{stoi(trim(string{str_arr[0]}))}; 51 | 52 | if (1 == str_arr_size || 0 == sum) 53 | return 1; 54 | 55 | if ((2 == str_arr_size) && (str_arr[0] == str_arr[1])) 56 | return 1; 57 | 58 | vector numbers{}; 59 | 60 | for (size_t i{1}; i < str_arr_size; i++) { 61 | numbers.emplace_back(stoi(trim(string{str_arr[i]}))); 62 | } 63 | 64 | sort(begin(numbers), end(numbers), 65 | [](const int ln, const int rn) { return ln > rn; }); 66 | 67 | size_t number_of_subsets{}; 68 | 69 | for (size_t i{}; i < numbers.size() - 1; i++) { 70 | int current_sum{}; 71 | bool is_continue_search{}; 72 | 73 | for (size_t j{i}; j < numbers.size(); j++) { 74 | if (current_sum + numbers[j] == sum) { 75 | number_of_subsets++; 76 | is_continue_search = true; 77 | continue; 78 | } 79 | 80 | if (current_sum + numbers[j] > sum) { 81 | is_continue_search = true; 82 | continue; 83 | } 84 | 85 | current_sum += numbers[j]; 86 | } 87 | 88 | if (!is_continue_search) 89 | break; 90 | } 91 | 92 | return number_of_subsets; 93 | } 94 | 95 | int main() { 96 | const char* arr1[] = {"16", "2", "4", "6", "10"}; 97 | cout << find_number_of_subsets(arr1, sizeof(arr1) / sizeof(*arr1)) 98 | << '\n'; // expected output: 2 99 | const char* arr2[] = {"13", "1", "3", "2", "5", "6", "7", "8", "4"}; 100 | cout << find_number_of_subsets(arr2, sizeof(arr2) / sizeof(*arr2)) 101 | << '\n'; // expected output: 6 102 | 103 | return 0; 104 | } 105 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/optimal_binary_search_ex1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | template 7 | ForwardIter optimal_bsearch(ForwardIter first, 8 | ForwardIter last, 9 | const T& value) { 10 | const ForwardIter orig_last{last}; 11 | ForwardIter next{first}; 12 | 13 | while (++next != last) { 14 | ForwardIter current{first}; 15 | 16 | std::advance(current, std::distance(first, last) / 2); 17 | 18 | if (value < *current) 19 | last = current; 20 | else 21 | first = current; 22 | 23 | next = first; 24 | } 25 | 26 | return value == *first ? first : orig_last; 27 | } 28 | 29 | int main() { 30 | std::vector numbers{1, 4, 2, 6, 12, 8, 5, 25, 26, 17, 35, 53, 3}; 31 | 32 | std::sort(std::begin(numbers), std::end(numbers)); 33 | 34 | for (decltype(numbers[0]) i{numbers[0]}; i <= numbers.back(); ++i) { 35 | if (std::cend(numbers) != 36 | optimal_bsearch(std::cbegin(numbers), std::cend(numbers), i)) 37 | std::cout << "Successfully located specified number: " << i << '\n'; 38 | else 39 | std::cout << "Failed to locate specified number: " << i << '\n'; 40 | } 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /1_easy_difficulty_level/vowel_substring.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | /* 8 | * Description: 9 | * Given a string 's' and a whole number 'k' find the first substring of length 10 | k that contains the most vowels (number of vowels <= k). If you cannot find any 11 | substring of length k that contains at least 1 vowel return the string "Not 12 | found!" 13 | 14 | * 15 | * Complete the 'findSubstring' function below. 16 | * 17 | * 18 | * The function is expected to return a string. 19 | * The function accepts the following parameters: 20 | * 1. string s 21 | * 2. int k 22 | */ 23 | 24 | static int allowed_vowels[256]{}; 25 | 26 | static int _ = []() { 27 | allowed_vowels['a'] = allowed_vowels['e'] = allowed_vowels['i'] = allowed_vowels['o'] = allowed_vowels['u'] = 1; 28 | return 0; 29 | }(); 30 | 31 | string findSubstring(const string& s, size_t k) { 32 | 33 | size_t start_pos{string::npos}; 34 | 35 | k = min(k, s.length()); 36 | 37 | size_t max_vowel_count = 38 | count_if(cbegin(s), cbegin(s) + k, [&](const char ch) { 39 | return 1 == allowed_vowels[static_cast(ch)]; 40 | }); 41 | size_t vowel_count{max_vowel_count}, offset{1}; 42 | 43 | if (0U != max_vowel_count) 44 | start_pos = 0U; 45 | else 46 | offset = k; 47 | 48 | if (k == max_vowel_count) 49 | return s.substr(0, k); 50 | 51 | for (size_t i{offset}; i + k <= s.length(); ++i) { 52 | vowel_count -= allowed_vowels[static_cast(s[i - 1])]; 53 | vowel_count += allowed_vowels[static_cast(s[i + k - 1])]; 54 | 55 | if (vowel_count > max_vowel_count) { 56 | if (k == vowel_count) 57 | return s.substr(i, k); 58 | max_vowel_count = vowel_count; 59 | start_pos = i; 60 | } 61 | } 62 | 63 | if (string::npos != start_pos) 64 | return s.substr(start_pos, k); 65 | 66 | return "Not found!"; 67 | } 68 | 69 | int main() { 70 | cout << findSubstring("caberqiitefg", 5) << '\n'; // expected output: "erqii" 71 | cout << findSubstring("aeiouia", 3) << '\n'; // expected output: "aei" 72 | cout << findSubstring("qwdftr", 2) << '\n'; // expected output: "Not found!" 73 | 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/01_prime_time.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Prime Time 3 | 4 | Using the C++ language, have the function PrimeTime(num) take the num parameter 5 | being passed and return the string true if the parameter is a prime number, 6 | otherwise return the string false. The range will be between 1 and 2^16. 7 | 8 | Sample test cases: 9 | 10 | Input: 19 11 | Output: "true" 12 | 13 | Input: 110 14 | Output: "false" 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | using namespace std; 23 | 24 | string PrimeTime(int64_t num) { 25 | num = abs(num); 26 | 27 | if (2 == num || 3 == num || 5 == num) 28 | return "true"; 29 | 30 | if (num % 2 == 0 || num % 3 == 0 || num % 5 == 0) 31 | return "false"; 32 | 33 | const int64_t highest_factor{static_cast(sqrt(num)) + 1}; 34 | 35 | for (int64_t i{7}; i < highest_factor; i += 2) { 36 | if (num % i == 0) 37 | return "false"; 38 | } 39 | 40 | return "true"; 41 | } 42 | 43 | int main() { 44 | // cout << PrimeTime(gets(stdin)); 45 | cout << PrimeTime(3) << '\n'; // expected output: "true" 46 | cout << PrimeTime(4) << '\n'; // expected output: "false" 47 | cout << PrimeTime(5) << '\n'; // expected output: "true" 48 | cout << PrimeTime(7) << '\n'; // expected output: "true" 49 | cout << PrimeTime(19) << '\n'; // expected output: "true" 50 | cout << PrimeTime(110) << '\n'; // expected output: "false" 51 | cout << PrimeTime(71) << '\n'; // expected output: "true" 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/02_run_length.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Run Length 3 | 4 | Using the C++ language, have the function RunLength(str) take the str parameter 5 | being passed and return a compressed version of the string using the Run-length 6 | encoding algorithm. This algorithm works by taking the occurrence of each 7 | repeating character and outputting that number along with a single character of 8 | the repeating sequence. For example: "wwwggopp" would return 3w2g1o2p. The 9 | string will not contain any numbers, punctuation, or symbols. 10 | 11 | Sample test cases: 12 | 13 | Input: "aabbcde" 14 | Output: "2a2b1c1d1e" 15 | 16 | Input: "wwwbbbw" 17 | Output: "3w3b1w" 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | using namespace std; 27 | 28 | string trim(const string& input) { 29 | string output{input}; 30 | output.erase(begin(output), 31 | find_if(begin(output), end(output), 32 | [](const char ch) { return !isspace(ch); })); 33 | 34 | output.erase(find_if(output.rbegin(), output.rend(), 35 | [](const char ch) { return !isspace(ch); }) 36 | .base(), 37 | end(output)); 38 | 39 | return output; 40 | } 41 | 42 | string run_length_v1(string str) { 43 | str = trim(str); 44 | 45 | if (str.empty()) 46 | return string{}; 47 | 48 | const size_t str_len{str.length()}; 49 | 50 | ostringstream oss{}; 51 | 52 | if (1 == str_len) { 53 | oss << '1' << str[0]; 54 | return oss.str(); 55 | } 56 | 57 | char current_ch{str[0]}; 58 | size_t current_ch_count{1}; 59 | 60 | for (size_t i{1}; i < str_len; i++) { 61 | if (current_ch == str[i]) { 62 | current_ch_count++; 63 | continue; 64 | } 65 | 66 | oss << current_ch_count << current_ch; 67 | current_ch = str[i]; 68 | current_ch_count = 1; 69 | } 70 | 71 | oss << current_ch_count << current_ch; 72 | 73 | return oss.str(); 74 | } 75 | 76 | string run_length_v2(string str) { 77 | str = trim(str); 78 | 79 | if (str.empty()) 80 | return string{}; 81 | 82 | const size_t str_len{str.length()}; 83 | 84 | if (1 == str_len) 85 | return string({'1', str.front()}); 86 | 87 | ostringstream oss{}; 88 | size_t start_pos{}, end_pos{1}; 89 | 90 | while (true) { 91 | end_pos = str.find_first_not_of(str[start_pos], end_pos); 92 | 93 | if (string::npos == end_pos) { 94 | oss << (str_len - start_pos) << str[start_pos]; 95 | break; 96 | } 97 | 98 | oss << (end_pos - start_pos) << str[start_pos]; 99 | start_pos = end_pos; 100 | } 101 | 102 | return oss.str(); 103 | } 104 | 105 | int main() { 106 | // cout << run_length_v2(move(string{gets(stdin)})); 107 | cout << run_length_v2(move(string{"wwwggopp"})) 108 | << '\n'; // expected output: "3w2g1o2p" 109 | cout << run_length_v2(move(string{"aabbcde"})) 110 | << '\n'; // expected output: "2a2b1c1d1e" 111 | cout << run_length_v2(move(string{"wwwbbbw"})) 112 | << '\n'; // expected output: "3w3b1w" 113 | 114 | return 0; 115 | } 116 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/03_prime_mover.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Prime Mover 3 | 4 | Using the C++ language, have the function PrimeMover(num) return the nth prime 5 | number. The range will be from 1 to 10^4. For example: if num is 16 the output 6 | should be 53 as 53 is the 16th prime number. 7 | 8 | Sample test cases: 9 | 10 | Input: 9 11 | Output: 23 12 | 13 | Input: 100 14 | Output: 541 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | using namespace std; 24 | 25 | bool is_prime_number(const int64_t n) { 26 | if (n % 2 == 0 || n % 3 == 0 || n % 5 == 0) 27 | return false; 28 | 29 | const int64_t upper_limit = static_cast(sqrt(n)) + 1; 30 | 31 | for (int64_t i{7}; i < upper_limit; i += 2) { 32 | if (n % i == 0) 33 | return false; 34 | } 35 | 36 | return true; 37 | } 38 | 39 | int64_t PrimeMover(int64_t num) { 40 | num = abs(num); 41 | 42 | static unordered_map precalculated_prime_numbers{ 43 | {1, 2}, {2, 3}, {3, 5}, {4, 7}, {5, 11}}; 44 | 45 | if (precalculated_prime_numbers.find(num) != end(precalculated_prime_numbers)) 46 | return precalculated_prime_numbers.find(num)->second; 47 | 48 | int n{6}; 49 | 50 | int current_prime_number{11}; 51 | 52 | const int increment_factor[4] = {2, 4, 2, 2}; 53 | 54 | size_t i{}; 55 | 56 | while (n <= num) { 57 | do { 58 | current_prime_number += increment_factor[i]; 59 | 60 | i++; 61 | 62 | if (4 == i) 63 | i = 0; 64 | 65 | } while (!is_prime_number(current_prime_number)); 66 | 67 | n++; 68 | } 69 | 70 | return current_prime_number; 71 | } 72 | 73 | int main() { 74 | // cout << PrimeMover(gets(stdin)); 75 | cout << PrimeMover(1) << '\n'; // expected output: 2 76 | cout << PrimeMover(2) << '\n'; // expected output: 3 77 | cout << PrimeMover(3) << '\n'; // expected output: 5 78 | cout << PrimeMover(4) << '\n'; // expected output: 7 79 | cout << PrimeMover(5) << '\n'; // expected output: 11 80 | cout << PrimeMover(16) << '\n'; // expected output: 53 81 | cout << PrimeMover(9) << '\n'; // expected output: 23 82 | cout << PrimeMover(100) << '\n'; // expected output: 541 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/04_palindrome_two.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Palindrome Two 3 | 4 | Using the C++ language, have the function PalindromeTwo(str) take the str 5 | parameter being passed and return the string true if the parameter is a 6 | palindrome, (the string is the same forward as it is backward) otherwise return 7 | the string false. The parameter entered may have punctuation and symbols but 8 | they should not affect whether the string is in fact a palindrome. For example: 9 | "Anne, I vote more cars race Rome-to-Vienna" should return true. 10 | 11 | Sample test cases: 12 | 13 | Input: "Noel - sees Leon" 14 | Output: "true" 15 | 16 | Input: "A war at Tarawa!" 17 | Output: "true" 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | using namespace std; 28 | 29 | string trim(const string& input) { 30 | string output{input}; 31 | output.erase(begin(output), 32 | find_if(begin(output), end(output), 33 | [](const char ch) { return !isspace(ch); })); 34 | 35 | output.erase(find_if(output.rbegin(), output.rend(), 36 | [](const char ch) { return !isspace(ch); }) 37 | .base(), 38 | end(output)); 39 | 40 | return output; 41 | } 42 | 43 | string PalindromeTwo_v1(string str, const locale& loc = locale{}) { 44 | str = trim(str); 45 | 46 | const size_t str_len{str.length()}; 47 | 48 | if (1 == str_len) 49 | return "false"; 50 | 51 | if (2 == str_len && str[0] == str[1]) 52 | return "true"; 53 | 54 | ostringstream oss{}; 55 | 56 | for (const char c : str) { 57 | if (isalnum(c)) 58 | oss << static_cast(tolower(c, loc)); 59 | } 60 | 61 | const string str1{oss.str()}; 62 | 63 | string str2{str1}; 64 | 65 | reverse(begin(str2), end(str2)); 66 | 67 | if (str1 == str2) 68 | return "true"; 69 | 70 | return "false"; 71 | } 72 | 73 | string PalindromeTwo_v2(string str, const locale& loc = locale{}) { 74 | str = trim(str); 75 | 76 | const size_t str_len{str.length()}; 77 | 78 | if (1 == str_len) 79 | return "false"; 80 | 81 | if (2 == str_len && str[0] == str[1]) 82 | return "true"; 83 | 84 | size_t i{}, j{str_len - 1}; 85 | 86 | while (i < j) { 87 | if (!isalnum(str[i])) { 88 | i++; 89 | continue; 90 | } 91 | 92 | if (!isalnum(str[j])) { 93 | j--; 94 | continue; 95 | } 96 | 97 | if (tolower(str[i], loc) != tolower(str[j], loc)) 98 | return "false"; 99 | 100 | i++; 101 | j--; 102 | } 103 | 104 | return "true"; 105 | } 106 | 107 | int main() { 108 | // cout << PalindromeTwo_v2(move(string{gets(stdin)})); 109 | cout << PalindromeTwo_v2( 110 | move(string{"Anne, I vote more cars race Rome-to-Vienna"})) 111 | << '\n'; // expected: "true" 112 | cout << PalindromeTwo_v2(move(string{"Noel - sees Leon"})) 113 | << '\n'; // expected: "true" 114 | cout << PalindromeTwo_v2(move(string{"A war at Tarawa!"})) 115 | << '\n'; // expected: "true" 116 | 117 | return 0; 118 | } 119 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/05_division.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Division 3 | 4 | Using the C++ language, have the function Division(num1,num2) take both 5 | parameters being passed and return the Greatest Common Factor. That is, return 6 | the greatest number that evenly goes into both numbers with no remainder. For 7 | example: 12 and 16 both are divisible by 1, 2, and 4 so the output should be 4. 8 | The range for both parameters will be from 1 to 10^3. 9 | 10 | Sample test cases: 11 | 12 | Input: 7 & num2 = 3 13 | Output: 1 14 | 15 | Input: 36 & num2 = 54 16 | Output: 18 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | using namespace std; 24 | 25 | int gcf(const int num1, const int num2) { 26 | const int min_number{min(num1, num2)}; 27 | 28 | for (int gcf{min_number}; gcf > 1; gcf--) { 29 | if ((num1 % gcf == 0) && (num2 % gcf == 0)) 30 | return gcf; 31 | } 32 | 33 | return 1; 34 | } 35 | 36 | int Division(const int* arr, const size_t arr_size) { 37 | if (arr_size < 2) 38 | return -1; 39 | 40 | return gcf(arr[0], arr[1]); 41 | } 42 | 43 | int main() { 44 | // const int A[] = { gets(stdin) }; 45 | // cout << Division(A, sizeof(A)/sizeof(*A)); 46 | const int B[] = {45, 12}; 47 | cout << Division(B, sizeof(B) / sizeof(*B)) << '\n'; // expected output: 3 48 | const int C[] = {1, 6}; 49 | cout << Division(C, sizeof(C) / sizeof(*C)) << '\n'; // expected output: 1 50 | const int D[] = {12, 28}; 51 | cout << Division(D, sizeof(D) / sizeof(*D)) << '\n'; // expected output: 4 52 | const int E[] = {7, 3}; 53 | cout << Division(E, sizeof(E) / sizeof(*E)) << '\n'; // expected output: 1 54 | const int F[] = {36, 54}; 55 | cout << Division(F, sizeof(F) / sizeof(*F)) << '\n'; // expected output: 18 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/07_arith_geo_ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Arith Geo II 3 | 4 | Using the C++ language, have the function ArithGeoII(arr) take the array of 5 | numbers stored in arr and return the string "Arithmetic" if the sequence follows 6 | an arithmetic pattern or return "Geometric" if it follows a geometric pattern. 7 | If the sequence doesn't follow either pattern return -1. 8 | An arithmetic sequence is one where the difference between each of the numbers 9 | is consistent, where as in a geometric sequence, each term after the first is 10 | multiplied by some constant or common ratio. Arithmetic example: [2, 4, 6, 8] 11 | and Geometric example: [2, 6, 18, 54]. Negative numbers may be entered as 12 | parameters, 0 will not be entered, and no array will contain all the same 13 | elements. 14 | 15 | Sample test cases: 16 | 17 | Input: 5,10,15 18 | Output: "Arithmetic" 19 | 20 | Input: 2,4,16,24 21 | Output: "-1" 22 | */ 23 | 24 | #include 25 | #include 26 | 27 | using namespace std; 28 | 29 | string ArithGeoII(const int* arr, const size_t arr_size) { 30 | if (arr_size < 2) 31 | return "-1"; 32 | 33 | bool is_arithmetic{true}; 34 | bool is_geometric{true}; 35 | 36 | const int diff{arr[1] - arr[0]}; 37 | int factor{1}; 38 | 39 | if (!arr[0] || !arr[1]) 40 | is_geometric = false; 41 | else 42 | factor = arr[1] / arr[0]; 43 | 44 | for (size_t i{1}; (i < arr_size && (is_arithmetic || is_geometric)); i++) { 45 | if (is_arithmetic && (diff != (arr[i] - arr[i - 1]))) { 46 | is_arithmetic = false; 47 | } 48 | 49 | if (is_geometric && (factor != (arr[i] / arr[i - 1]))) { 50 | is_geometric = false; 51 | } 52 | } 53 | 54 | if (is_arithmetic) 55 | return "Arithmetic"; 56 | if (is_geometric) 57 | return "Geometric"; 58 | 59 | return "-1"; 60 | } 61 | 62 | int main() { 63 | // const int A[] = gets(stdin); 64 | // cout << ArithGeoII(A, sizeof(A)/sizeof(*A)); 65 | const int B[] = {2, 4, 6, 8}; 66 | cout << ArithGeoII(B, sizeof(B) / sizeof(*B)) 67 | << '\n'; // expected output: "Arithmetic" 68 | const int C[] = {2, 6, 18, 54}; 69 | cout << ArithGeoII(C, sizeof(C) / sizeof(*C)) 70 | << '\n'; // expected output: "Geometric" 71 | const int D[] = {5, 10, 15}; 72 | cout << ArithGeoII(D, sizeof(D) / sizeof(*D)) 73 | << '\n'; // expected output: "Arithmetic" 74 | const int E[] = {2, 4, 16, 24}; 75 | cout << ArithGeoII(E, sizeof(E) / sizeof(*E)) 76 | << '\n'; // expected output: "-1" 77 | 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/09_binary_converter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Binary Converter 3 | 4 | Using the C++ language, have the function BinaryConverter(str) return the 5 | decimal form of the binary value. For example: if 101 is passed return 5, or if 6 | 1000 is passed return 8. 7 | 8 | Sample test cases: 9 | 10 | Input: "100101" 11 | Output: "37" 12 | 13 | Input: "011" 14 | Output: "3" 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | using namespace std; 23 | 24 | string trim(const string& input) { 25 | string output{input}; 26 | output.erase(begin(output), 27 | find_if(begin(output), end(output), 28 | [](const char ch) { return !isspace(ch); })); 29 | 30 | output.erase(find_if(output.rbegin(), output.rend(), 31 | [](const char ch) { return !isspace(ch); }) 32 | .base(), 33 | end(output)); 34 | 35 | return output; 36 | } 37 | 38 | bool is_valid_binary_string(const string& str) { 39 | return all_of(begin(str), end(str), 40 | [](const char ch) { return (ch == '0' || ch == '1'); }); 41 | } 42 | 43 | string BinaryConverter_v1(string str) { 44 | str = trim(str); 45 | 46 | if (!is_valid_binary_string(str)) 47 | return "not possible"; 48 | 49 | int decimal_number{}; 50 | 51 | for (const char ch : str) { 52 | decimal_number <<= 1; 53 | decimal_number |= static_cast(ch - '0'); 54 | } 55 | 56 | return to_string(decimal_number); 57 | } 58 | 59 | string BinaryConverter_v2(string str) { 60 | str = trim(str); 61 | 62 | if (!is_valid_binary_string(str)) 63 | return "not possible"; 64 | 65 | const size_t str_len{str.length()}; 66 | 67 | int decimal_number{}; 68 | 69 | for (size_t i{}; i < str_len; i++) { 70 | if ('0' == str[i]) 71 | continue; 72 | decimal_number += 1 << (str_len - i - 1); 73 | } 74 | 75 | return to_string(decimal_number); 76 | } 77 | 78 | string BinaryConverter_v3(string str) { 79 | str = trim(str); 80 | 81 | if (!is_valid_binary_string(str)) 82 | return "not possible"; 83 | 84 | return to_string(stoi(str, nullptr, 2)); 85 | } 86 | 87 | int main() { 88 | // cout << BinaryConverter_v1(move(string{gets(stdin)})); 89 | cout << BinaryConverter_v1(move(string{"101"})) 90 | << '\n'; // expected output: 5 91 | cout << BinaryConverter_v1(move(string{"1000"})) 92 | << '\n'; // expected output: 8 93 | cout << BinaryConverter_v1(move(string{"100101"})) 94 | << '\n'; // expected output: 37 95 | cout << BinaryConverter_v1(move(string{"011"})) 96 | << '\n'; // expected output: 3 97 | 98 | return 0; 99 | } 100 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/12_caesar_cipher.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Caesar Cipher 3 | 4 | Using the C++ language, have the function CaesarCipher(str,num) take the str 5 | parameter and perform a Caesar Cipher shift on it using the num parameter as the 6 | shifting number. A Caesar Cipher works by shifting each letter in the string N 7 | places down in the alphabet (in this case N will be num). Punctuation, spaces, 8 | and capitalization should remain intact. For example if the string is "Caesar 9 | Cipher" and num is 2 the output should be "Ecguct Ekrjgt". 10 | 11 | Sample test cases: 12 | 13 | Input: "Hello" & num = 4 14 | Output: "Lipps" 15 | 16 | Input: "abc" & num = 0 17 | Output: "abc" 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | using namespace std; 25 | 26 | string trim(const string& str) { 27 | const size_t str_len{str.length()}; 28 | 29 | if (!str_len) 30 | return string{}; 31 | 32 | size_t first{}, last{str_len - 1}; 33 | 34 | for (; first <= last; ++first) { 35 | if (!isspace(str[first])) 36 | break; 37 | } 38 | 39 | if (first > last) 40 | return string{}; 41 | 42 | for (; last > first; --last) { 43 | if (!isspace(str[last])) 44 | break; 45 | } 46 | 47 | return str.substr(first, last - first + 1); 48 | } 49 | 50 | string CaesarCipher(string str, size_t num) { 51 | str = trim(str); 52 | 53 | if (str.empty()) 54 | return str; 55 | 56 | num %= 26; 57 | 58 | for (auto& ch : str) { 59 | if (isalpha(ch)) { 60 | if (islower(ch)) { 61 | if (ch + num > 122) { 62 | const size_t diff = ch + num - 123; 63 | ch = static_cast(97 + diff); 64 | } else { 65 | ch = static_cast(ch + num); 66 | } 67 | 68 | } else { 69 | if (ch + num > 90) { 70 | const size_t diff = ch + num - 91; 71 | ch = static_cast(65 + diff); 72 | } else { 73 | ch = static_cast(ch + num); 74 | } 75 | } 76 | } 77 | } 78 | 79 | return str; 80 | } 81 | 82 | int main() { 83 | // cout << CaesarCipher(gets(stdin)); 84 | cout << CaesarCipher("Caesar Cipher", 2) 85 | << '\n'; // expected output: "Ecguct Ekrjgt" 86 | cout << CaesarCipher("Hello", 4) << '\n'; // expected output: "Lipps" 87 | cout << CaesarCipher("abc", 0) << '\n'; // expected output: "abc" 88 | cout << CaesarCipher("coderBYTE", 2) << '\n'; // expected output: "eqfgtDAVG" 89 | cout << CaesarCipher("dogs", 8) << '\n'; // expected output: "lwoa" 90 | cout << CaesarCipher("byte", 13) << '\n'; // expected output: "olgr" 91 | return 0; 92 | } 93 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/13_formatted_division.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Formatted Division 3 | 4 | Using the C++ language, have the function FormattedDivision(num1,num2) take both 5 | parameters being passed, divide num1 by num2, and return the result as a string 6 | with properly formatted commas and 4 significant digits after the decimal place. 7 | For example: if num1 is 123456789 and num2 is 10000 the output should be 8 | "12,345.6789". The output must contain a number in the one's place even if it is 9 | a zero. 10 | 11 | Sample test cases: 12 | 13 | Input: 2 & num2 = 3 14 | Output: "0.6667" 15 | 16 | Input: 10 & num2 = 10 17 | Output: "1.0000" 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | using namespace std; 26 | 27 | string FormattedDivision(const long num1, const long num2) { 28 | if (!num2) 29 | return !num1 ? "NaN" : num1 < 0 ? "-Inf" : "+Inf"; 30 | 31 | if (!num1) 32 | return "0.0000"; 33 | 34 | const double result{static_cast(num1) / num2}; 35 | // char buffer[32]; 36 | // sprintf(buffer, "%.4lf", result); 37 | 38 | ostringstream oss1{}; 39 | oss1 << fixed << setprecision(4) << result; 40 | 41 | string result_str{oss1.str()}; 42 | 43 | size_t start{result_str.find('.')}; 44 | 45 | if (string::npos == start) 46 | start = result_str.length(); 47 | 48 | int number_of_commas = start / 3; 49 | 50 | int leading_no_of_ch = start % 3; 51 | 52 | if (!leading_no_of_ch) { 53 | number_of_commas--; 54 | leading_no_of_ch = 3; 55 | } 56 | 57 | ostringstream oss2{}; 58 | 59 | oss2 << result_str.substr(0, leading_no_of_ch); 60 | 61 | int j{leading_no_of_ch}; 62 | 63 | while (number_of_commas > 0) { 64 | oss2 << ',' << result_str.substr(j, 3); 65 | j += 3; 66 | number_of_commas--; 67 | } 68 | 69 | if (start < result_str.length()) 70 | oss2 << result_str.substr(start); 71 | 72 | return oss2.str(); 73 | } 74 | 75 | int main() { 76 | // cout << FormattedDivision(gets(stdin)); 77 | cout << FormattedDivision(123456789, 10000) 78 | << '\n'; // expected output: 12,345.6789 79 | cout << FormattedDivision(2, 3) << '\n'; // expected output: 0.6667 80 | cout << FormattedDivision(10, 10) << '\n'; // expected output: 1.0000 81 | 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/14_consecutive.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Consecutive 3 | 4 | Using the C++ language, have the function Consecutive(arr) take the array of 5 | integers stored in arr and return the minimum number of integers needed to make 6 | the contents of arr consecutive from the lowest number to the highest number. 7 | For example: If arr contains [4, 8, 6] then the output should be 2 because two 8 | numbers need to be added to the array (5 and 7) to make it a consecutive array 9 | of numbers from 4 to 8. Negative numbers may be entered as parameters and no 10 | array will have less than 2 elements. 11 | 12 | Sample test cases: 13 | 14 | Input: 5,10,15 15 | Output: 8 16 | 17 | Input: -2,10,4 18 | Output: 10 19 | */ 20 | 21 | #include 22 | #include 23 | 24 | using namespace std; 25 | 26 | int Consecutive(int* arr, const size_t arr_size) { 27 | if (arr_size < 2) 28 | return -1; 29 | 30 | int number_of_missing_numbers{}; 31 | 32 | sort(arr, arr + arr_size); 33 | 34 | for (size_t i{}; i < arr_size - 1; i++) { 35 | if (arr[i + 1] != arr[i]) 36 | number_of_missing_numbers += arr[i + 1] - arr[i] - 1; 37 | } 38 | 39 | return number_of_missing_numbers; 40 | } 41 | 42 | int main() { 43 | // int A[] = gets(stdin); 44 | // cout << Consecutive(A, sizeof(A)/sizeof(A[0])); 45 | int B[] = {4, 8, 6}; 46 | cout << Consecutive(B, sizeof(B) / sizeof(B[0])) 47 | << '\n'; // expected output: 2 48 | int C[] = {5, 10, 15}; 49 | cout << Consecutive(C, sizeof(C) / sizeof(C[0])) 50 | << '\n'; // expected output: 8 51 | int D[] = {-2, 10, 4}; 52 | cout << Consecutive(D, sizeof(D) / sizeof(D[0])) 53 | << '\n'; // expected output: 10 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/16_permutation_step.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Permutation Step 3 | 4 | Using the C++ language, have the function PermutationStep(num) take the num 5 | parameter being passed and return the next number greater than num using the 6 | same digits. For example: if num is 123 return 132, if it's 12453 return 12534. 7 | If a number has no greater permutations, return -1 (ie. 999). 8 | 9 | Sample test Cases: 10 | 11 | Input: 11121 12 | Output: 11211 13 | 14 | Input: 41352 15 | Output: 41523 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | using namespace std; 23 | 24 | int PermutationStep(const int num) { 25 | string num_str{to_string(num)}; 26 | 27 | const bool is_next_permutation_available{ 28 | next_permutation(begin(num_str), end(num_str))}; 29 | 30 | if (!is_next_permutation_available) 31 | return -1; 32 | 33 | return stoi(num_str); 34 | } 35 | 36 | int main() { 37 | // cout << PermutationStep(gets(stdin)); 38 | cout << PermutationStep(123) << '\n'; // expected output: 132 39 | cout << PermutationStep(12453) << '\n'; // expected output: 12534 40 | cout << PermutationStep(11121) << '\n'; // expected output: 11211 41 | cout << PermutationStep(41352) << '\n'; // expected output: 41523 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/17_prime_checker.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Prime Checker 3 | 4 | Using the C++ language, have the function PrimeChecker(num) take num and return 5 | 1 if any arrangement of num comes out to be a prime number, otherwise return 0. 6 | For example: if num is 910, the output should be 1 because 910 can be arranged 7 | into 109 or 019, both of which are primes. 8 | 9 | Sample test cases: 10 | 11 | Input: 98 12 | Output: 1 13 | 14 | Input: 598 15 | Output: 1 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | using namespace std; 25 | 26 | bool is_prime_number(long n) { 27 | if (2 == n || 3 == n || 5 == n) 28 | return true; 29 | 30 | if (n < 2 || n % 2 == 0 || n % 5 == 0) 31 | return false; 32 | 33 | const long upper_limit = static_cast(sqrt(static_cast(n)) + 1); 34 | 35 | vector divisor_factors{3, 7, 9, 11}; 36 | long k{3}; 37 | size_t index{1}; 38 | 39 | while (k <= upper_limit) { 40 | if (n % k == 0) 41 | return false; 42 | 43 | k = divisor_factors[index++]; 44 | 45 | if (index > 3) { 46 | index = 0; 47 | 48 | for (auto& factor : divisor_factors) 49 | factor += 10; 50 | } 51 | } 52 | 53 | return true; 54 | } 55 | 56 | long PrimeChecker(long number) { 57 | string number_str{to_string(number)}; 58 | 59 | if (1 == number_str.length()) { 60 | if (!is_prime_number(number)) 61 | return 0; 62 | return 1; 63 | } 64 | 65 | const char first_digit{number_str.front()}; 66 | 67 | if (all_of(begin(number_str), end(number_str), 68 | [&first_digit](const char d) { return d == first_digit; })) { 69 | return is_prime_number(number) ? 1 : 0; 70 | } 71 | 72 | sort(begin(number_str), end(number_str)); 73 | 74 | do { 75 | long next_num{stol(number_str)}; 76 | if (is_prime_number(next_num)) 77 | return 1; 78 | } while (next_permutation(begin(number_str), end(number_str))); 79 | 80 | return 0; 81 | } 82 | 83 | int main() { 84 | // cout << PrimeChecker(gets(stdin)); 85 | cout << PrimeChecker(910) << '\n'; // expected output: 1 86 | cout << PrimeChecker(9) << '\n'; // expected output: 0 87 | cout << PrimeChecker(44) << '\n'; // expected output: 0 88 | cout << PrimeChecker(100) << '\n'; // expected output: 0 89 | cout << PrimeChecker(104) << '\n'; // expected output: 1 90 | cout << PrimeChecker(908) << '\n'; // expected output: 1 91 | 92 | return 0; 93 | } 94 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/19_swap_ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Swap II 3 | 4 | Using the C++ language, have the function SwapII(str) take the str parameter and 5 | swap the case of each character. Then, if a letter is between two numbers 6 | (without separation), switch the places of the two numbers. For example: if str 7 | is "6Hello4 -8World, 7 yes3" the output should be 4hELLO6 -8wORLD, 7 YES3. 8 | 9 | Sample test cases: 10 | 11 | Input: "Hello -5LOL6" 12 | Output: "hELLO -6lol5" 13 | 14 | Input: "2S 6 du5d4e" 15 | Output: "2s 6 DU4D5E" 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | using namespace std; 25 | 26 | string trim(const string& input) { 27 | string output{input}; 28 | output.erase(begin(output), 29 | find_if(begin(output), end(output), 30 | [](const char ch) { return !isspace(ch); })); 31 | 32 | output.erase(find_if(output.rbegin(), output.rend(), 33 | [](const char ch) { return !isspace(ch); }) 34 | .base(), 35 | end(output)); 36 | 37 | return output; 38 | } 39 | 40 | void flip_character_case(char& ch) { 41 | if (islower(ch)) 42 | ch = static_cast(toupper(ch)); 43 | 44 | else 45 | ch = static_cast(tolower(ch)); 46 | } 47 | 48 | string SwapII(string str) { 49 | str = trim(str); 50 | 51 | const size_t str_len{str.length()}; 52 | 53 | size_t prev_digit_pos{string::npos}; 54 | 55 | for (size_t i{}; i < str_len; i++) { 56 | if (isalpha(str[i])) 57 | flip_character_case(str[i]); 58 | 59 | else if (isdigit(str[i])) { 60 | if ((string::npos != prev_digit_pos) && isalpha(str[i - 1])) { 61 | swap(str[prev_digit_pos], str[i]); 62 | prev_digit_pos = string::npos; 63 | } else 64 | prev_digit_pos = i; 65 | 66 | } else 67 | prev_digit_pos = string::npos; 68 | } 69 | 70 | return str; 71 | } 72 | 73 | int main() { 74 | // cout << SwapII(move(string{gets(stdin)})); 75 | cout << SwapII(move(string{"6Hello4 -8World, 7 yes3"})) 76 | << '\n'; // expected output: "4hELLO6 -8wORLD, 7 YES3" 77 | cout << SwapII(move(string{"Hello -5LOL6"})) 78 | << '\n'; // expected output: "hELLO -6lol5" 79 | cout << SwapII(move(string{"2S 6 du5d4e"})) 80 | << '\n'; // expected output: "2s 6 DU4D5E" 81 | cout << SwapII(move(string{"123gg))(("})) 82 | << '\n'; // expected output: "123GG))((" 83 | 84 | return 0; 85 | } 86 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/20_number_search.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Number Search 3 | 4 | Using the C++ language, have the function NumberSearch(str) take the str 5 | parameter, search for all the numbers in the string, add them together, then 6 | return that final number divided by the total amount of letters in the string. 7 | For example: if str is "Hello6 9World 2, Nic8e D7ay!" the output should be 2. 8 | First if you add up all the numbers, 6 + 9 + 2 + 8 + 7 you get 32. Then there 9 | are 17 letters in the string. 32 / 17 = 1.882, and the final answer should be 10 | rounded to the nearest whole number, so the answer is 2. Only single digit 11 | numbers separated by characters will be used throughout the whole string (So 12 | this won't ever be the case: hello44444 world). Each string will also have at 13 | least one letter. 14 | 15 | Sample test cases: 16 | 17 | Input: "H3ello9-9" 18 | Output: 4 19 | 20 | Input: "One Number*1*" 21 | Output: 0 22 | */ 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | using namespace std; 31 | 32 | string trim(const string& input) { 33 | string output{input}; 34 | output.erase(begin(output), 35 | find_if(begin(output), end(output), 36 | [](const char ch) { return !isspace(ch); })); 37 | 38 | output.erase(find_if(output.rbegin(), output.rend(), 39 | [](const char ch) { return !isspace(ch); }) 40 | .base(), 41 | end(output)); 42 | 43 | return output; 44 | } 45 | 46 | string NumberSearch_v1(string str) { 47 | str = trim(str); 48 | 49 | int sum{}; 50 | int letter_count{}; 51 | 52 | for (const char ch : str) { 53 | if (isdigit(ch)) 54 | sum += ch - '0'; 55 | else if (isalpha(ch)) 56 | letter_count++; 57 | } 58 | 59 | if (!sum) { 60 | if (!letter_count) 61 | return "NaN"; 62 | return "0"; 63 | } 64 | 65 | if (!letter_count) 66 | return "Infinity"; 67 | 68 | return to_string( 69 | static_cast(round(static_cast(sum) / letter_count))); 70 | } 71 | 72 | string NumberSearch_v2(string str) { 73 | str = trim(str); 74 | 75 | int sum{}; 76 | int letter_count{}; 77 | 78 | for_each(begin(str), end(str), [&](const char ch) { 79 | if (isdigit(ch)) 80 | sum += ch - '0'; 81 | else if (isalpha(ch)) 82 | letter_count++; 83 | }); 84 | 85 | if (!sum) { 86 | if (!letter_count) 87 | return "NaN"; 88 | return "0"; 89 | } 90 | 91 | if (!letter_count) 92 | return "Infinity"; 93 | 94 | return to_string( 95 | static_cast(round(static_cast(sum) / letter_count))); 96 | } 97 | 98 | int main() { 99 | // cout << NumberSearch_v2(move(string{gets(stdin)})); 100 | cout << NumberSearch_v2(move(string{"Hello6 9World 2, Nic8e D7ay!"})) 101 | << '\n'; // expected output: 2 102 | cout << NumberSearch_v2(move(string{"H3ello9-9"})) 103 | << '\n'; // expected output: 4 104 | cout << NumberSearch_v2(move(string{"One Number*1*"})) 105 | << '\n'; // expected output: 0 106 | return 0; 107 | } 108 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/22_bracket_matcher.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Bracket Matcher 3 | 4 | Using the C++ language, have the function BracketMatcher(str) take the str 5 | parameter being passed and return 1 if the brackets are correctly matched and 6 | each one is accounted for. Otherwise return 0. For example: if str is "(hello 7 | (world))", then the output should be 1, but if str is "((hello (world))" the the 8 | output should be 0 because the brackets do not correctly match up. Only "(" and 9 | ")" will be used as brackets. If str contains no brackets return 1. 10 | 11 | Sample test cases: 12 | 13 | Input: "(coder)(byte))" 14 | Output: 0 15 | 16 | Input: "(c(oder)) b(yte)" 17 | Output: 1 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | using namespace std; 26 | 27 | string trim(const string& input) { 28 | string output{input}; 29 | output.erase(begin(output), 30 | find_if(begin(output), end(output), 31 | [](const char ch) { return !isspace(ch); })); 32 | 33 | output.erase(find_if(output.rbegin(), output.rend(), 34 | [](const char ch) { return !isspace(ch); }) 35 | .base(), 36 | end(output)); 37 | 38 | return output; 39 | } 40 | 41 | int RemoveBrackets(string str) { 42 | str = trim(str); 43 | 44 | if (!any_of(begin(str), end(str), 45 | [](const char ch) { return (ch == '(' || ch == ')'); })) 46 | return 1; 47 | 48 | int balance_count{}; 49 | size_t start{}; 50 | 51 | while (')' == str[start]) { 52 | start++; 53 | balance_count++; 54 | } 55 | 56 | if (balance_count) 57 | return 0; 58 | 59 | size_t last{str.length() - 1}; 60 | 61 | while ('(' == str[last]) { 62 | last--; 63 | balance_count++; 64 | } 65 | 66 | if (balance_count) 67 | return 0; 68 | 69 | for (size_t i{start}; i <= last; i++) { 70 | if ('(' == str[i]) 71 | balance_count++; 72 | 73 | else if (')' == str[i]) { 74 | if (balance_count <= 0) 75 | return 0; 76 | 77 | balance_count--; 78 | } 79 | } 80 | 81 | if (!balance_count) 82 | return 1; 83 | 84 | return 0; 85 | } 86 | 87 | int main() { 88 | // cout << RemoveBrackets(move(string{gets(stdin)})); 89 | cout << RemoveBrackets(move(string{"(hello (world))"})) 90 | << '\n'; // expected output: 1 91 | cout << RemoveBrackets(move(string{"(coder)(byte))"})) 92 | << '\n'; // expected output: 0 93 | cout << RemoveBrackets(move(string{"(c(oder)) b(yte)"})) 94 | << '\n'; // expected output: 1 95 | cout << RemoveBrackets(move(string{"the color re(d))()(()"})) 96 | << '\n'; // expected output: 0 97 | 98 | return 0; 99 | } 100 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/23_string_reduction.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: String Reduction 3 | 4 | Using the C++ language, have the function StringReduction(str) take the str 5 | parameter being passed and return the smallest number you can get through the 6 | following reduction method. The method is: Only the letters a, b, and c will be 7 | given in str and you must take two different adjacent characters and replace it 8 | with the third. For example "ac" can be replaced with "b" but "aa" cannot be 9 | replaced with anything. This method is done repeatedly until the string cannot 10 | be further reduced, and the length of the resulting string is to be outputted. 11 | For example: if str is "cab", "ca" can be reduced to "b" and you get "bb" (you 12 | can also reduce it to "cc"). The reduction is done so the output should be 2. If 13 | str is "bcab", "bc" reduces to "a", so you have "aab", then "ab" reduces to "c", 14 | and the final string "ac" is reduced to "b" so the output should be 1. 15 | 16 | Sample test cases: 17 | 18 | Input: "abcabc" 19 | Output: 2 20 | 21 | Input: "cccc" 22 | Output: 4 23 | */ 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | using namespace std; 32 | 33 | string trim(const string& input) { 34 | string output{input}; 35 | output.erase(begin(output), 36 | find_if(begin(output), end(output), 37 | [](const char ch) { return !isspace(ch); })); 38 | 39 | output.erase(find_if(output.rbegin(), output.rend(), 40 | [](const char ch) { return !isspace(ch); }) 41 | .base(), 42 | end(output)); 43 | 44 | return output; 45 | } 46 | 47 | string StringReduction(string str) { 48 | str = trim(str); 49 | 50 | const unordered_map reduce_pairs{{"ab", 'c'}, {"ba", 'c'}, 51 | {"ac", 'b'}, {"ca", 'b'}, 52 | {"bc", 'a'}, {"cb", 'a'}}; 53 | 54 | bool is_reduced{}; 55 | 56 | do { 57 | is_reduced = false; 58 | 59 | for (size_t i{}; i < str.length() - 1; i++) { 60 | const string key{str[i], str[i + 1]}; 61 | 62 | if (reduce_pairs.find(key) != end(reduce_pairs)) { 63 | const char rc{reduce_pairs.find(key)->second}; 64 | 65 | str.replace(i, 2, 1, rc); 66 | 67 | is_reduced = true; 68 | } 69 | } 70 | 71 | } while (is_reduced); 72 | 73 | return to_string(str.length()); 74 | } 75 | 76 | int main() { 77 | // cout << StringReduction(move(string{gets(stdin)})); 78 | cout << StringReduction(move(string{"cab"})) << '\n'; // expected output: 2 79 | cout << StringReduction(move(string{"bcab"})) << '\n'; // expected output: 1 80 | cout << StringReduction(move(string{"abcabc"})) 81 | << '\n'; // expected output: 2 82 | cout << StringReduction(move(string{"cccc"})) << '\n'; // expected output: 4 83 | cout << StringReduction(move(string{"aabc"})) << '\n'; // expected output: 1 84 | cout << StringReduction(move(string{"ccaa"})) << '\n'; // expected output: 2 85 | 86 | return 0; 87 | } 88 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/25_threefive_multiples.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: ThreeFive Multiples 3 | 4 | Using the C++ language, have the function ThreeFiveMultiples(num) return the sum 5 | of all the multiples of 3 and 5 that are below num. For example: if num is 10, 6 | the multiples of 3 and 5 that are below 10 are 3, 5, 6, and 9, and adding them 7 | up you get 23, so your program should return 23. The integer being passed will 8 | be between 1 and 100. 9 | 10 | Sample test cases: 11 | 12 | Input: 6 13 | Output: 8 14 | 15 | Input: 1 16 | Output: 0 17 | */ 18 | 19 | #include 20 | 21 | using namespace std; 22 | 23 | int ThreeFiveMultiples(const int num) { 24 | int sum{}; 25 | 26 | for (int i{3}; i < num; i++) { 27 | if (i % 3 == 0 || i % 5 == 0) 28 | sum += i; 29 | } 30 | 31 | return sum; 32 | } 33 | 34 | int main() { 35 | // cout << ThreeFiveMultiples(gets(stdin)); 36 | cout << ThreeFiveMultiples(10) << '\n'; // expected output: 23 37 | cout << ThreeFiveMultiples(6) << '\n'; // expected output: 8 38 | cout << ThreeFiveMultiples(1) << '\n'; // expected output: 0 39 | cout << ThreeFiveMultiples(3) << '\n'; // expected output: 0 40 | cout << ThreeFiveMultiples(31) << '\n'; // expected output: 225 41 | cout << ThreeFiveMultiples(17) << '\n'; // expected output: 60 42 | cout << ThreeFiveMultiples(4) << '\n'; // expected output: 3 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/26_fibonacci_checker.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Fibonacci Checker 3 | 4 | Using the C++ language, have the function FibonacciChecker(num) return the 5 | string yes if the number given is part of the Fibonacci sequence. This sequence 6 | is defined by: Fn = Fn-1 + Fn-2, which means to find Fn you add the previous two 7 | numbers up. The first two numbers are 0 and 1, then comes 1, 2, 3, 5 etc. If num 8 | is not in the Fibonacci sequence, return the string no. 9 | 10 | Sample test cases: 11 | 12 | Input: 34 13 | Output: "yes" 14 | 15 | Input: 54 16 | Output: "no" 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | using namespace std; 24 | 25 | uint64_t fibonacci(const uint64_t num) { 26 | if (num < 2) 27 | return num; 28 | 29 | return fibonacci(num - 1) + fibonacci(num - 2); 30 | } 31 | 32 | string FibonacciChecker(const uint64_t num) { 33 | if (num < 2) 34 | return "yes"; 35 | 36 | uint64_t a{}, b{1}; 37 | 38 | while (true) { 39 | const uint64_t temp{a}; 40 | a = b; 41 | b = temp + b; 42 | if (num == b) 43 | return "yes"; 44 | if (num < b) 45 | return "no"; 46 | } 47 | } 48 | 49 | int main() { 50 | // cout << FibonacciChecker(gets(stdin)); 51 | cout << FibonacciChecker(34) << '\n'; // expected output: "yes" 52 | cout << FibonacciChecker(54) << '\n'; // expected output: "no" 53 | cout << FibonacciChecker(8) << '\n'; // expected output: "yes" 54 | cout << FibonacciChecker(17) << '\n'; // expected output: "no" 55 | cout << FibonacciChecker(21) << '\n'; // expected output: "yes" 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/30_distinct_list.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Distinct List 3 | 4 | Using the C++ language, have the function DistinctList(arr) take the array of 5 | numbers stored in arr and determine the total number of duplicate entries. For 6 | example if the input is [1, 2, 2, 2, 3] then your program should output 2 7 | because there are two duplicates of one of the elements. 8 | 9 | Sample test cases: 10 | 11 | Input: 0,-2,-2,5,5,5 12 | Output: 3 13 | 14 | Input: 100,2,101,4 15 | Output: 0 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | using namespace std; 24 | 25 | int DistinctList_v1(int* arr, const size_t arr_size) { 26 | unordered_set visited_arr_elements(arr, arr + arr_size); 27 | 28 | return arr_size - visited_arr_elements.size(); 29 | } 30 | 31 | int DistinctList_v2(int* arr, const size_t arr_size) { 32 | int* const new_arr_last = unique(arr, arr + arr_size); 33 | 34 | return static_cast(arr + arr_size - new_arr_last); 35 | } 36 | 37 | int main() { 38 | // const int A[] = gets(stdin); 39 | // cout << DistinctList_v2(A, sizeof(A)/sizeof(*A)); 40 | int B[] = {1, 2, 2, 2, 3}; 41 | cout << DistinctList_v2(B, sizeof(B) / sizeof(*B)) 42 | << '\n'; // expected output: 2 43 | int C[] = {0, -2, -2, 5, 5, 5}; 44 | cout << DistinctList_v2(C, sizeof(C) / sizeof(*C)) 45 | << '\n'; // expected output: 3 46 | int D[] = {100, 2, 101, 4}; 47 | cout << DistinctList_v2(D, sizeof(D) / sizeof(*D)) 48 | << '\n'; // expected output: 0 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/31_look_say_sequence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Look Say Sequence 3 | 4 | Using the C++ language, have the function LookSaySequence(num) take the num 5 | parameter being passed and return the next number in the sequence according to 6 | the following rule: to generate the next number in a sequence read off the 7 | digits of the given number, counting the number of digits in groups of the same 8 | digit. For example, the sequence beginning with 1 would be: 1, 11, 21, 1211, ... 9 | The 11 comes from there being "one 1" before it and the 21 comes from there 10 | being "two 1's" before it. So your program should return the next number in the 11 | sequence given num. 12 | 13 | Sample test cases: 14 | 15 | Input: 1211 16 | Output: 111221 17 | 18 | Input: 2466 19 | Output: 121426 20 | */ 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | using namespace std; 28 | 29 | string LookSaySequence_v1(const int num) { 30 | string num_str{to_string(num)}; 31 | const size_t num_str_len{num_str.length()}; 32 | 33 | ostringstream oss{}; 34 | 35 | for (size_t i{}; i < num_str_len; i++) { 36 | size_t count{1}; 37 | 38 | while (i < num_str_len - 1 && num_str[i] == num_str[i + 1]) { 39 | count++; 40 | i++; 41 | } 42 | 43 | oss << count << num_str[i]; 44 | } 45 | 46 | return oss.str(); 47 | } 48 | 49 | string LookSaySequence_v2(const int num) { 50 | string num_str{to_string(num)}; 51 | 52 | ostringstream oss{}; 53 | 54 | char prev_digit{num_str[0]}; 55 | size_t start_pos{}; 56 | 57 | while (true) { 58 | const size_t end_pos{num_str.find_first_not_of(prev_digit, start_pos)}; 59 | 60 | if (string::npos == end_pos) { 61 | oss << num_str.length() - start_pos << prev_digit; 62 | break; 63 | } 64 | 65 | oss << end_pos - start_pos << prev_digit; 66 | prev_digit = num_str[end_pos]; 67 | start_pos = end_pos; 68 | } 69 | 70 | return oss.str(); 71 | } 72 | 73 | string LookSaySequence_v3(int num) { 74 | ostringstream oss{}; 75 | 76 | int prev_digit{num % 10}; 77 | num /= 10; 78 | size_t count{1}; 79 | 80 | while (true) { 81 | const int current_digit{num % 10}; 82 | num /= 10; 83 | 84 | if (current_digit == prev_digit) 85 | count++; 86 | 87 | else { 88 | oss << prev_digit << count; 89 | prev_digit = current_digit; 90 | count = 1; 91 | } 92 | 93 | if (!num) { 94 | oss << prev_digit << count; 95 | break; 96 | } 97 | } 98 | 99 | string result_num{oss.str()}; 100 | reverse(begin(result_num), end(result_num)); 101 | return result_num; 102 | } 103 | 104 | int main() { 105 | // cout << LookSaySequence_v2(gets(stdin)); 106 | cout << LookSaySequence_v2(1211) << '\n'; // expected output: 111221 107 | cout << LookSaySequence_v2(2466) << '\n'; // expected output: 121426 108 | 109 | return 0; 110 | } 111 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/32_number_encoding.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Number Encoding 3 | 4 | Using the C++ language, have the function NumberEncoding(str) take the str 5 | parameter and encode the message according to the following rule: encode every 6 | letter into its corresponding numbered position in the alphabet. Symbols and 7 | spaces will also be used in the input. For example: if str is "af5c a#!" then 8 | your program should return 1653 1#!. 9 | 10 | Sample test cases: 11 | 12 | Input: "hello 45" 13 | Output: 85121215 45 14 | 15 | Input: "jaj-a" 16 | Output: 10110-1 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | using namespace std; 26 | 27 | string trim(const string& str) { 28 | const size_t str_len{str.length()}; 29 | 30 | if (!str_len) 31 | return string{}; 32 | 33 | size_t begin_str{}; 34 | size_t end_str{str_len - 1}; 35 | 36 | while (begin_str <= end_str) { 37 | bool cont{}; 38 | if (isspace(str[begin_str])) { 39 | cont = true; 40 | begin_str++; 41 | } 42 | 43 | if (isspace(str[end_str])) { 44 | cont = true; 45 | end_str--; 46 | } 47 | 48 | if (!cont) 49 | break; 50 | } 51 | 52 | if (begin_str > end_str) 53 | return string{}; 54 | 55 | return str.substr(begin_str, end_str - begin_str + 1); 56 | } 57 | 58 | string NumberEncoding_v1(string str) { 59 | static const string alphabet_str{"abcdefghijklmnopqrstuvwxyz"}; 60 | 61 | str = trim(str); 62 | 63 | ostringstream oss{}; 64 | 65 | for (size_t i{}; i < str.length(); i++) { 66 | str[i] = static_cast(tolower(str[i])); 67 | const size_t index{alphabet_str.find(str[i])}; 68 | 69 | if (string::npos == index) { 70 | oss << str[i]; 71 | continue; 72 | } 73 | 74 | oss << index + 1; 75 | } 76 | 77 | return oss.str(); 78 | } 79 | 80 | string NumberEncoding_v2(string str) { 81 | str = trim(str); 82 | 83 | ostringstream oss{}; 84 | 85 | for (size_t i{}; i < str.length(); i++) { 86 | const char ch{static_cast(tolower(str[i]))}; 87 | 88 | if (isalpha(ch)) { 89 | oss << static_cast(ch - 'a') + 1; 90 | continue; 91 | } 92 | 93 | oss << str[i]; 94 | } 95 | 96 | return oss.str(); 97 | } 98 | 99 | int main() { 100 | // cout << NumberEncoding_v1(move(string{gets(stdin)})); 101 | cout << NumberEncoding_v1(move(string{"af5c a#!"})) 102 | << '\n'; // expected output: 1653 1#! 103 | cout << NumberEncoding_v1(move(string{"hello 45"})) 104 | << '\n'; // expected output: 85121215 45 105 | cout << NumberEncoding_v1(move(string{"jaj-a"})) 106 | << '\n'; // expected output: 10110-1 107 | cout << NumberEncoding_v1(move(string{"km#e"})) 108 | << '\n'; // expected output: 1113#5 109 | cout << NumberEncoding_v1(move(string{"af5c a#!"})) 110 | << '\n'; // expected output: 1653 1#! 111 | cout << NumberEncoding_v1(move(string{"---fc9#"})) 112 | << '\n'; // expected output: ---639# 113 | 114 | return 0; 115 | } 116 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/33_stock_picker.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Stock Picker 3 | 4 | Using the C++ language, have the function StockPicker(arr) take the array of 5 | numbers stored in arr which will contain integers that represent the amount in 6 | dollars that a single stock is worth, and return the maximum profit that could 7 | have been made by buying stock on day x and selling stock on day y where y > x. 8 | For example: if arr is [44, 30, 24, 32, 35, 30, 40, 38, 15] then your program 9 | should return 16 because at index 2 the stock was worth $24 and at index 6 the 10 | stock was then worth $40, so if you bought the stock at 24 and sold it at 40, 11 | you would have made a profit of $16, which is the maximum profit that could have 12 | been made with this list of stock prices. 13 | 14 | If there is not profit that could have been made with the stock prices, then 15 | your program should return -1. For example: arr is [10, 9, 8, 2] then your 16 | program should return -1. 17 | 18 | Sample test cases: 19 | 20 | Input: 10,12,4,5,9 21 | Output: 5 22 | 23 | Input: 14,20,4,12,5,11 24 | Output: 8 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | using namespace std; 33 | 34 | int StockPicker_v1(const int* arr, const int arr_size) { 35 | if (arr_size < 2) 36 | return -1; 37 | 38 | int current_max_profit{}; 39 | int current_minimum{arr[0]}; 40 | 41 | for (int i{1}; i < arr_size; i++) { 42 | if (arr[i] - current_minimum > current_max_profit) 43 | current_max_profit = arr[i] - current_minimum; 44 | 45 | else if (arr[i] < current_minimum) 46 | current_minimum = arr[i]; 47 | } 48 | 49 | if (!current_max_profit) 50 | return -1; 51 | 52 | return current_max_profit; 53 | } 54 | 55 | int StockPicker_v2(const int* arr, const size_t arr_size) { 56 | int current_max_profit{-1}; 57 | 58 | for (size_t i{}; i < arr_size - 1; i++) { 59 | for (size_t j{i + 1}; j < arr_size; j++) { 60 | if (arr[j] <= arr[i]) 61 | continue; 62 | 63 | if (arr[j] - arr[i] > current_max_profit) 64 | current_max_profit = arr[j] - arr[i]; 65 | } 66 | } 67 | 68 | return current_max_profit; 69 | } 70 | 71 | int main() { 72 | // const int A[] = gets(stdin); 73 | // cout << StockPicker_v2(A, sizeof(A)/sizeof(*A)); 74 | const int B[] = {44, 30, 24, 32, 35, 30, 40, 38, 15}; 75 | cout << StockPicker_v2(B, sizeof(B) / sizeof(*B)) 76 | << '\n'; // expected output: 16 77 | const int C[] = {10, 12, 4, 5, 9}; 78 | cout << StockPicker_v2(C, sizeof(C) / sizeof(*C)) 79 | << '\n'; // expected output: 5 80 | const int D[] = {14, 20, 4, 12, 5, 11}; 81 | cout << StockPicker_v2(D, sizeof(D) / sizeof(*D)) 82 | << '\n'; // expected output: 8 83 | const int E[] = {5, 10, 4, 6, 7}; // 4, 5, 6, 7, 10 84 | cout << StockPicker_v2(E, sizeof(E) / sizeof(*E)) 85 | << '\n'; // expected output: 5 86 | 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/34_max_subarray.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Max Subarray 3 | 4 | Using the C++ language, have the function MaxSubarray(arr) take the array of 5 | numbers stored in arr and determine the largest sum that can be formed by any 6 | contiguous subarray in the array. For example, if arr is [-2, 5, -1, 7, -3] then 7 | your program should return 11 because the sum is formed by the subarray [5, -1, 8 | 7]. Adding any element before or after this subarray would make the sum smaller. 9 | 10 | Sample test cases: 11 | 12 | Input: 1, -2, 0, 3 13 | Output: 3 14 | 15 | Input: 3, -1, -1, 4, 3, -1 16 | Output: 8 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | using namespace std; 24 | 25 | int MaxSubarray(const int* arr, const size_t arr_size) { 26 | int current_largest_sum{*max_element(arr, arr + arr_size)}; 27 | size_t current_sub_array_size{arr_size}; 28 | 29 | if (arr + arr_size == 30 | find_if(arr, arr + arr_size, [](const int n) { return n < 0; })) 31 | return accumulate(arr, arr + arr_size, 0); 32 | 33 | while (current_sub_array_size != 0) { 34 | for (size_t i{}; i + current_sub_array_size <= arr_size; i++) { 35 | int current_sum{}; 36 | for (size_t j{i}; j < i + current_sub_array_size; j++) { 37 | current_sum += arr[j]; 38 | } 39 | 40 | if (current_sum > current_largest_sum) 41 | current_largest_sum = current_sum; 42 | } 43 | 44 | current_sub_array_size--; 45 | } 46 | 47 | return current_largest_sum; 48 | } 49 | 50 | int main() { 51 | // const int A[] = gets(stdin); 52 | // cout << MaxSubarray(A, sizeof(A)/sizeof(*A)); 53 | const int B[] = {-2, 5, -1, 7, -3}; 54 | cout << MaxSubarray(B, sizeof(B) / sizeof(*B)) << '\n'; // 11 55 | const int C[] = {1, -2, 0, 3}; 56 | cout << MaxSubarray(C, sizeof(C) / sizeof(*C)) << '\n'; // 3 57 | const int D[] = {3, -1, -1, 4, 3, -1}; 58 | cout << MaxSubarray(D, sizeof(D) / sizeof(*D)) << '\n'; // 8 59 | const int E[] = {1, 2, 3, 4, 5}; 60 | cout << MaxSubarray(E, sizeof(E) / sizeof(*E)) << '\n'; // 15 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/38_symmetric_tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Symmetric Tree 3 | 4 | Using the C++ language, have the function SymmetricTree(strArr) take the array 5 | of strings stored in strArr, which will represent a binary tree, and determine 6 | if the tree is symmetric (a mirror image of itself). The array will be 7 | implemented similar to how a binary heap is implemented, except the tree may not 8 | be complete and NULL nodes on any level of the tree will be represented with a 9 | #. For example: if strArr is ["1", "2", "2", "3", "#", "#", "3"] then this tree 10 | looks like the following: 11 | 12 | [1] 13 | [2] [2] 14 | [3][#] [#][3] 15 | 16 | For the input above, your program should return the string true because the 17 | binary tree is symmetric. 18 | 19 | Sample test cases: 20 | 21 | Input: "4", "3", "4" 22 | Output: "false" 23 | 24 | Input: "10", "2", "2", "#", "1", "1", "#" 25 | Output: "true" 26 | */ 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | using namespace std; 35 | 36 | string SymmetricTree_v1(const string* str_arr, const size_t str_arr_size) { 37 | for (size_t i{1}; i < str_arr_size; i = 2 * i + 1) { 38 | for (size_t j{i}, k{2 * i}; j < k; j++, k--) { 39 | if (str_arr[j] != str_arr[k]) 40 | return "false"; 41 | } 42 | } 43 | 44 | return "true"; 45 | } 46 | 47 | string SymmetricTree_v2(const string* str_arr, const size_t str_arr_size) { 48 | size_t current_level{1}; 49 | 50 | while (current_level < str_arr_size) { 51 | queue q{}; 52 | stack s{}; 53 | 54 | for (size_t i{current_level}; i < min(2 * current_level + 1, str_arr_size); 55 | i++) { 56 | q.emplace(str_arr[i]); 57 | s.emplace(str_arr[i]); 58 | } 59 | 60 | while (!q.empty() && !s.empty()) { 61 | if (q.front() != s.top()) 62 | return "false"; 63 | q.pop(); 64 | s.pop(); 65 | } 66 | 67 | current_level = 2 * current_level + 1; 68 | } 69 | 70 | return "true"; 71 | } 72 | 73 | int main() { 74 | // const string A[] = gets(stdin); 75 | // cout << SymmetricTree_v2(A, sizeof(A)/sizeof(*A)); 76 | const string B[] = {"1", "2", "2", "3", "#", "#", "3"}; 77 | cout << SymmetricTree_v2(B, sizeof(B) / sizeof(*B)) 78 | << '\n'; // expected output: "true" 79 | const string C[] = {"10", "2", "2", "#", "1", "1", "#"}; 80 | cout << SymmetricTree_v2(C, sizeof(C) / sizeof(*C)) 81 | << '\n'; // expected output: "true" 82 | const string D[] = {"4", "3", "4"}; 83 | cout << SymmetricTree_v2(D, sizeof(D) / sizeof(*D)) 84 | << '\n'; // expected output: "false" 85 | 86 | return 0; 87 | } 88 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/43_array_min_jumps_v1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Array Min Jumps 3 | 4 | Using the C++ language, have the function ArrayMinJumps(arr) take the array of 5 | integers stored in arr, where each integer represents the maximum number of 6 | steps that can be made from that position, and determine the least amount of 7 | jumps that can be made to reach the end of the array. For example: if arr is [1, 8 | 5, 4, 6, 9, 3, 0, 0, 1, 3] then your program should output the number 3 because 9 | you can reach the end of the array from the beginning via the following steps: 1 10 | -> 5 -> 9 -> END or 1 -> 5 -> 6 -> END. Both of these combinations produce a 11 | series of 3 steps. And as you can see, you don't always have to take the maximum 12 | number of jumps at a specific position, you can take less jumps even though the 13 | number is higher. 14 | 15 | If it is not possible to reach the end of the array, return -1. 16 | 17 | Sample test cases: 18 | 19 | Input: 3, 4, 2, 1, 1, 100 20 | Output: 2 21 | 22 | Input: 1, 3, 6, 8, 2, 7, 1, 2, 1, 2, 6, 1, 2, 1, 2 23 | Output: 4 24 | */ 25 | 26 | #include 27 | #include 28 | 29 | using namespace std; 30 | 31 | int ArrayMinJumps(const int* arr, const size_t arr_size) { 32 | if (arr_size < 2) 33 | return 0; 34 | 35 | vector result(arr_size); 36 | 37 | for (size_t i{}; i < result.size() - 1; i++) { 38 | const int steps_to_make{arr[i]}; 39 | 40 | if (!steps_to_make && arr_size - 2 == i) 41 | return -1; 42 | 43 | const int current_steps_count{result[i] + 1}; 44 | 45 | for (size_t j{i + 1}; j < result.size() && j < i + 1 + steps_to_make; j++) { 46 | if (!result[j]) 47 | result[j] = current_steps_count; 48 | } 49 | } 50 | 51 | return result[arr_size - 1]; 52 | } 53 | 54 | int main() { 55 | // const int A[] = gets(stdin); 56 | // cout << ArrayMinJumps(A, sizeof(A)/sizeof(*A)); 57 | const int B[] = {1, 5, 4, 6, 9, 3, 0, 0, 1, 3}; 58 | cout << ArrayMinJumps(B, sizeof(B) / sizeof(*B)) 59 | << '\n'; // expected output: 3 60 | const int C[] = {3, 4, 2, 1, 1, 100}; 61 | cout << ArrayMinJumps(C, sizeof(C) / sizeof(*C)) 62 | << '\n'; // expected output: 2 63 | const int D[] = {1, 3, 6, 8, 2, 7, 1, 2, 1, 2, 6, 1, 2, 1, 2}; 64 | cout << ArrayMinJumps(D, sizeof(D) / sizeof(*D)) 65 | << '\n'; // expected output: 4 66 | const int E[] = {1, 0, 0, 2}; 67 | cout << ArrayMinJumps(E, sizeof(E) / sizeof(*E)) 68 | << '\n'; // expected output: -1 69 | const int F[] = {4}; 70 | cout << ArrayMinJumps(F, sizeof(F) / sizeof(*F)) 71 | << '\n'; // expected output: 0 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/51_palindromic_substring.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Palindromic Substring 3 | 4 | Using the C++ language, have the function PalindromicSubstring(str) take the str 5 | parameter being passed and find the longest palindromic substring, which means 6 | the longest substring which is read the same forwards as it is backwards. For 7 | example: if str is "abracecars" then your program should return the string 8 | racecar because it is the longest palindrome within the input string. 9 | 10 | The input will only contain lowercase alphabetic characters. The longest 11 | palindromic substring will always be unique, but if there is none that is longer 12 | than 2 characters, return the string none. 13 | 14 | Sample test cases: 15 | 16 | Input: "hellosannasmith" 17 | Output: "sannas" 18 | 19 | Input: "abcdefgg" 20 | Output: "none" 21 | */ 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | using namespace std; 29 | 30 | string trim(const string& input) { 31 | string output{input}; 32 | output.erase(begin(output), 33 | find_if(begin(output), end(output), 34 | [](const char ch) { return !isspace(ch); })); 35 | 36 | output.erase(find_if(output.rbegin(), output.rend(), 37 | [](const char ch) { return !isspace(ch); }) 38 | .base(), 39 | end(output)); 40 | 41 | return output; 42 | } 43 | 44 | bool check_if_current_substring_is_palindromic( 45 | const string& str, 46 | const size_t start_index, 47 | const size_t current_substr_len) { 48 | for (size_t i{start_index}, j{start_index + current_substr_len - 1}; i < j; 49 | i++, j--) { 50 | if (str[i] != str[j]) 51 | return false; 52 | } 53 | 54 | return true; 55 | } 56 | 57 | string PalindromicSubstring(string str) { 58 | str = trim(str); 59 | 60 | const size_t str_len{str.length()}; 61 | 62 | if (str_len < 3) 63 | return "none"; 64 | 65 | size_t current_substr_len{str_len}; 66 | size_t current_substring_start_index{}; 67 | 68 | while (current_substr_len > 2) { 69 | if (current_substring_start_index + current_substr_len > str_len) { 70 | current_substr_len--; 71 | 72 | if (current_substr_len < 3) 73 | return "none"; 74 | 75 | current_substring_start_index = 0; 76 | } 77 | 78 | if (check_if_current_substring_is_palindromic( 79 | str, current_substring_start_index, current_substr_len)) { 80 | return str.substr(current_substring_start_index, current_substr_len); 81 | } 82 | 83 | current_substring_start_index++; 84 | } 85 | 86 | return "none"; 87 | } 88 | 89 | int main() { 90 | // cout << PalindromicSubstring(move(string{gets(stdin)})); 91 | cout << PalindromicSubstring(move(string{"abracecars"})) 92 | << '\n'; // expected output: "racecar" 93 | cout << PalindromicSubstring(move(string{"hellosannasmith"})) 94 | << '\n'; // expected output: "sannas" 95 | cout << PalindromicSubstring(move(string{"abcdefgg"})) 96 | << '\n'; // expected output: "none" 97 | return 0; 98 | } 99 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/54_seating_students.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Seating Students 3 | 4 | Using the C++ language, have the function SeatingStudents(arr) read the array of 5 | integers stored in arr which will be in the following format: [K, r1, r2, r3, 6 | ...] where K represents the number of desks in a classroom, and the rest of the 7 | integers in the array will be in sorted order and will represent the desks that 8 | are already occupied. All of the desks will be arranged in 2 columns, where desk 9 | #1 is at the top left, desk #2 is at the top right, desk #3 is below #1, desk #4 10 | is below #2, etc. Your program should return the number of ways 2 students can 11 | be seated next to each other. This means 1 student is on the left and 1 student 12 | on the right, or 1 student is directly above or below the other student. 13 | 14 | For example: if arr is [12, 2, 6, 7, 11] then this classrooms looks like the 15 | following picture: 16 | 17 | Based on above arrangement of occupied desks, there are a total of 6 ways to 18 | seat 2 new students next to each other. The combinations are: [1, 3], [3, 4], 19 | [3, 5], [8, 10], [9, 10], [10, 12]. So for this input your program should 20 | return 6. K will range from 2 to 24 and will always be an even number. After K, 21 | the number of occupied desks in the array can range from 0 to K. 22 | 23 | Sample test cases: 24 | 25 | Input: 6, 4 26 | Output: 4 27 | 28 | Input: 8, 1, 8 29 | Output: 6 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | using namespace std; 37 | 38 | size_t SeatingStudents(const size_t* arr, const size_t arr_size) { 39 | if (1 == arr_size) 40 | return (arr[0] / 2 * arr[0] / 2); 41 | 42 | const size_t number_of_desks{arr[0]}; 43 | 44 | vector seats(number_of_desks); 45 | 46 | for (size_t i{1}; i < arr_size; i++) 47 | seats[arr[i] - 1] = true; 48 | 49 | size_t count{}; 50 | 51 | for (size_t i{}; i < number_of_desks; i++) { 52 | if (!seats[i]) { 53 | if (i % 2 == 0) { 54 | if (i + 1 < number_of_desks && !seats[i + 1]) 55 | count++; 56 | if (i + 2 < number_of_desks && !seats[i + 2]) 57 | count++; 58 | } else if (i + 2 < number_of_desks && !seats[i + 2]) 59 | count++; 60 | } 61 | } 62 | 63 | return count; 64 | } 65 | 66 | int main() { 67 | // const size_t A[] = gets(stdin); 68 | // cout << SeatingStudents(A, sizeof(A)/sizeof(*A)); 69 | const size_t B[] = {12, 2, 6, 7, 11}; 70 | cout << SeatingStudents(B, sizeof(B) / sizeof(*B)) 71 | << '\n'; // expected output: 6 72 | const size_t C[] = {6, 4}; 73 | cout << SeatingStudents(C, sizeof(C) / sizeof(*C)) 74 | << '\n'; // expected output: 4 75 | const size_t D[] = {8, 1, 8}; 76 | cout << SeatingStudents(D, sizeof(D) / sizeof(*D)) 77 | << '\n'; // expected output: 6 78 | const size_t E[] = {4}; 79 | cout << SeatingStudents(E, sizeof(E) / sizeof(*E)) 80 | << '\n'; // expected output: 4 81 | const size_t F[] = {2}; 82 | cout << SeatingStudents(F, sizeof(F) / sizeof(*F)) 83 | << '\n'; // expected output: 1 84 | 85 | return 0; 86 | } 87 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/57_matrix_chains_v2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Matrix Chains 3 | 4 | Using the C++ language, have the function MatrixChains(arr) read the array of 5 | positive integers stored in arr where every pair will represent an NxM matrix. 6 | For example: if arr is [1, 2, 3, 4] this means you have a 1x2, 2x3, and a 3x4 7 | matrix. So there are N-1 total matrices where N is the length of the array. Your 8 | goal is to determine the least number of multiplications possible after 9 | multiplying all the matrices. Matrix multiplication is associative so (A*B)*C is 10 | equal to A*(B*C). 11 | 12 | For the above example, let us assume the following letters represent the 13 | different matrices: A = 1x2, B = 2x3, and C = 3x4. Then we can multiply the 14 | matrices in the following orders: (AB)C or A(BC). The first ordering requires 15 | (1*2*3) = 6 then we multiply this new 1x3 matrix by the 3x4 matrix and we get 16 | (1*3*4) = 12. So in total, this ordering required 6 + 12 = 18 multiplications. 17 | Your program should therefore return 18 because the second ordering produces 18 | more multiplications. The input array will contain between 3 and 30 elements. 19 | 20 | See the Cormen book for details of the following algorithm. 21 | 22 | Sample test cases: 23 | 24 | Input: 2, 3, 4 25 | Output: 24 26 | 27 | Input: 1, 4, 5, 6, 8 28 | Output: 98 29 | */ 30 | 31 | #include 32 | #include 33 | 34 | using namespace std; 35 | 36 | size_t MatrixChainOrder_v2(const int* matrices, 37 | const size_t i, 38 | const size_t j) { 39 | if (i == j) 40 | return 0; 41 | 42 | size_t min_number_of_multiplications{string::npos}; 43 | 44 | for (size_t k{i}; k < j; k++) { 45 | const size_t count{MatrixChainOrder_v2(matrices, i, k) + 46 | MatrixChainOrder_v2(matrices, k + 1, j) + 47 | matrices[i - 1] * matrices[k] * matrices[j]}; 48 | 49 | if (count < min_number_of_multiplications) 50 | min_number_of_multiplications = count; 51 | } 52 | 53 | return min_number_of_multiplications; 54 | } 55 | 56 | size_t MatrixChains(const int* arr, const size_t arr_size) { 57 | return MatrixChainOrder_v2(arr, 1, arr_size - 1); 58 | } 59 | 60 | int main() { 61 | // const int A[] = gets(stdin); 62 | // cout << MatrixChains(A, sizeof(A)/sizeof(*A)); 63 | const int B[] = {1, 2, 3, 4}; 64 | cout << MatrixChains(B, sizeof(B) / sizeof(*B)) 65 | << '\n'; // expected output: 18 66 | const int C[] = {2, 3, 4}; 67 | cout << MatrixChains(C, sizeof(C) / sizeof(*C)) 68 | << '\n'; // expected output: 24 69 | const int D[] = {1, 4, 5, 6, 8}; 70 | cout << MatrixChains(D, sizeof(D) / sizeof(*D)) 71 | << '\n'; // expected output: 98 72 | const int E[] = {10, 2, 4, 12, 15, 20, 4, 56}; 73 | cout << MatrixChains(E, sizeof(E) / sizeof(*E)) 74 | << '\n'; // expected output: 2784 75 | const int F[] = {10, 20, 100, 5, 4, 6, 9, 1, 2, 3, 5, 10, 9, 71}; 76 | cout << MatrixChains(F, sizeof(F) / sizeof(*F)) 77 | << '\n'; // expected output: 4308 78 | const int G[] = {5, 6, 7, 8, 9, 1, 1, 1, 2}; 79 | cout << MatrixChains(G, sizeof(G) / sizeof(*G)) 80 | << '\n'; // expected output: 213 81 | 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/58_histogram_area.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Histogram Area 3 | 4 | Using the C++ language, have the function HistogramArea(arr) read the array of 5 | non-negative integers stored in arr which will represent the heights of bars on 6 | a graph (where each bar width is 1), and determine the largest area underneath 7 | the entire bar graph. For example: if arr is [2, 1, 3, 4, 1] then this looks 8 | like the following bar graph: 9 | _ 10 | _| | 11 | _ |x|x| 12 | | |_|x|x|_ 13 | | | |x|x| | 14 | 15 | You can see in the above bar graph that the largest area underneath the graph is 16 | covered by the x's. The area of that space is equal to 6 because the entire 17 | width is 2 and the maximum height is 3, therefore 2 * 3 = 6. Your program should 18 | return 6. The array will always contain at least 1 element. 19 | 20 | Sample test cases: 21 | 22 | Input: 6, 3, 1, 4, 12, 4 23 | Output: 12 24 | 25 | Input: 5, 6, 7, 4, 1 26 | Output: 16 27 | */ 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | using namespace std; 34 | 35 | size_t HistogramArea(const size_t* arr, const size_t arr_size) { 36 | unordered_set already_visited_heights{}; 37 | size_t max_area{}; 38 | 39 | for (size_t i{}; i < arr_size; i++) { 40 | if (already_visited_heights.count(arr[i])) 41 | continue; 42 | already_visited_heights.insert(arr[i]); 43 | size_t current_area{}; 44 | size_t current_max_height{arr[i]}; 45 | 46 | for (size_t j{}; j < arr_size; j++) { 47 | if (arr[j] < current_max_height) { 48 | if (current_area > max_area) 49 | max_area = current_area; 50 | current_area = 0; 51 | 52 | } else 53 | current_area += current_max_height; 54 | } 55 | 56 | if (current_area > max_area) 57 | max_area = current_area; 58 | } 59 | 60 | return max_area; 61 | } 62 | 63 | int main() { 64 | // const size_t A[] = gets(stdin); 65 | // cout << HistogramArea(A, sizeof(A)/sizeof(*A)); 66 | const size_t B[] = {2, 1, 3, 4, 1}; 67 | cout << HistogramArea(B, sizeof(B) / sizeof(*B)) 68 | << '\n'; // expected output: 6 69 | const size_t C[] = {6, 3, 1, 4, 12, 4}; 70 | cout << HistogramArea(C, sizeof(C) / sizeof(*C)) 71 | << '\n'; // expected output: 12 72 | const size_t D[] = {5, 6, 7, 4, 1}; 73 | cout << HistogramArea(D, sizeof(D) / sizeof(*D)) 74 | << '\n'; // expected output: 16 75 | const size_t E[] = {1, 2, 3, 4}; 76 | cout << HistogramArea(E, sizeof(E) / sizeof(*E)) 77 | << '\n'; // expected output: 6 78 | const size_t F[] = {1, 2, 3, 100}; 79 | cout << HistogramArea(F, sizeof(F) / sizeof(*F)) 80 | << '\n'; // expected output: 100 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/59_matching_characters.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Matching Characters 3 | 4 | Using the C++ language, have the function MatchingCharacters(str) take the str 5 | parameter being passed and determine the largest number of unique characters 6 | that exists between a pair of matching letters anywhere in the string. For 7 | example: if str is "ahyjakh" then there are only two pairs of matching letters, 8 | the two a's and the two h's. Between the pair of a's there are 3 unique 9 | characters: h, y, and j. Between the h's there are 4 unique characters: y, j, a, 10 | and k. So for this example your program should return 4. 11 | 12 | Another example: if str is "ghececgkaem" then your program should return 5 13 | because the most unique characters exists within the farthest pair of e 14 | characters. The input string may not contain any character pairs, and in that 15 | case your program should just return 0. The input will only consist of lowercase 16 | alphabetic characters. 17 | 18 | Sample test cases: 19 | 20 | Input: "mmmerme" 21 | Output: 3 22 | 23 | Input: "abccdefghi" 24 | Output: 0 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | using namespace std; 33 | 34 | pair find_first_and_last_position_of_specified_char( 35 | const string& str, 36 | const char ch) { 37 | return {str.find(ch), str.rfind(ch)}; 38 | } 39 | 40 | string MatchingCharacters(string str) { 41 | unordered_set already_visited{}; 42 | size_t max_unique_character_count{}; 43 | 44 | for (const char ch : str) { 45 | if (already_visited.count(ch)) 46 | continue; 47 | 48 | already_visited.insert(ch); 49 | 50 | const pair max_character_distance{ 51 | find_first_and_last_position_of_specified_char(str, ch)}; 52 | 53 | if (max_character_distance.second - max_character_distance.first <= 1) 54 | continue; 55 | 56 | unordered_set unique_characters{}; 57 | 58 | size_t unique_ch_count{}; 59 | 60 | for (size_t i{max_character_distance.first + 1}; 61 | i < max_character_distance.second; i++) { 62 | if (!unique_characters.count(str[i])) { 63 | unique_characters.insert(str[i]); 64 | unique_ch_count++; 65 | } 66 | } 67 | 68 | if (unique_ch_count > max_unique_character_count) 69 | max_unique_character_count = unique_ch_count; 70 | } 71 | 72 | return to_string(max_unique_character_count); 73 | } 74 | 75 | int main() { 76 | // cout << MatchingCharacters(gets(stdin)); 77 | cout << MatchingCharacters(move(string{"ahyjakh"})) 78 | << '\n'; // expected output: 4 79 | cout << MatchingCharacters(move(string{"ghececgkaem"})) 80 | << '\n'; // expected output: 5 81 | cout << MatchingCharacters(move(string{"mmmerme"})) 82 | << '\n'; // expected output: 3 83 | cout << MatchingCharacters(move(string{"abccdefghi"})) 84 | << '\n'; // expected output: 0 85 | return 0; 86 | } 87 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/60_ternary_converter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Ternary Converter 3 | 4 | Using the C++ language, have the function TernaryConverter(num) take the num 5 | parameter being passed, which will always be a positive integer, and convert it 6 | into a ternary representation. For example: if num is 12 then your program 7 | should return 110. 8 | 9 | Sample test cases: 10 | 11 | Input: 21 12 | Output: 210 13 | 14 | Input: 67 15 | Output: 2111 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | using namespace std; 24 | 25 | string TernaryConverter_v1(int num) { 26 | string result{}; 27 | 28 | while (num) { 29 | result.insert(begin(result), static_cast(num % 3 + '0')); 30 | 31 | num /= 3; 32 | } 33 | 34 | return result; 35 | } 36 | 37 | string TernaryConverter_v2(int num) { 38 | ostringstream oss{}; 39 | 40 | while (num) { 41 | oss << static_cast(num % 3 + '0'); 42 | 43 | num /= 3; 44 | } 45 | 46 | string result{oss.str()}; 47 | 48 | reverse(begin(result), end(result)); 49 | 50 | return result; 51 | } 52 | 53 | int main() { 54 | // cout << TernaryConverter_v1(gets(stdin)); 55 | cout << TernaryConverter_v1(12) << '\n'; // expected output: 110 56 | cout << TernaryConverter_v1(21) << '\n'; // expected output: 210 57 | cout << TernaryConverter_v1(67) << '\n'; // expected output: 2111 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/61_linear_congruence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Linear Congruence 3 | 4 | Using the C++ language, have the function LinearCongruence(str) read the str 5 | parameter being passed which will be a linear congruence equation in the form: 6 | "ax = b (mod m)" Your goal is to solve for x and return the number of solutions 7 | to x. For example: if str is "32x = 8 (mod 4)" then your program should return 4 8 | because the answers to this equation can be either 0, 1, 2, or 3. 9 | 10 | Sample test cases: 11 | 12 | Input: "12x = 5 (mod 2)" 13 | Output: 0 14 | 15 | Input: "12x = 4 (mod 2)" 16 | Output: 2 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | using namespace std; 25 | 26 | string trim(const string& input) { 27 | string output{input}; 28 | output.erase(begin(output), 29 | find_if(begin(output), end(output), 30 | [](const char ch) { return !isspace(ch); })); 31 | 32 | output.erase(find_if(output.rbegin(), output.rend(), 33 | [](const char ch) { return !isspace(ch); }) 34 | .base(), 35 | end(output)); 36 | 37 | return output; 38 | } 39 | 40 | string LinearCongruence(string str) { 41 | str = trim(str); 42 | 43 | int x{stoi(str.substr(0, str.find('x')))}; 44 | 45 | size_t sep_pos{str.find('=')}; 46 | 47 | if (string::npos == sep_pos) 48 | return "not possible"; 49 | 50 | size_t second_number_start_pos{str.find_first_of("1234567890", sep_pos + 1)}; 51 | 52 | if (string::npos == second_number_start_pos) 53 | return "not possible"; 54 | 55 | sep_pos = str.find_first_not_of("1234567890", second_number_start_pos + 1); 56 | 57 | if (string::npos == sep_pos) 58 | return "not possible"; 59 | 60 | const int y{stoi( 61 | str.substr(second_number_start_pos, sep_pos - second_number_start_pos))}; 62 | 63 | size_t m_start{str.rfind("mod")}; 64 | 65 | m_start = str.find_first_of("1234567890", m_start + 3); 66 | 67 | const size_t m_end{str.find_first_not_of("1234567890", m_start)}; 68 | 69 | int m{stoi(str.substr(m_start, m_end - m_start))}; 70 | 71 | while (m) { 72 | const int temp{m}; 73 | m = x % m; 74 | x = temp; 75 | } 76 | 77 | if (y % x == 0) 78 | return to_string(x); 79 | 80 | return "0"; 81 | } 82 | 83 | int main() { 84 | // cout << LinearCongruence(gets(stdin)); 85 | cout << LinearCongruence(move(string{"32x = 8 (mod 4)"})) 86 | << '\n'; // expected output: 4 87 | cout << LinearCongruence(move(string{"12x = 5 (mod 2)"})) 88 | << '\n'; // expected output: 0 89 | cout << LinearCongruence(move(string{"12x = 4 (mod 2)"})) 90 | << '\n'; // expected output: 2 91 | 92 | return 0; 93 | } 94 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/75_primes.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Primes 3 | 4 | Using the C++ language, have the function Primes(num) take the num parameter 5 | being passed and return the string true if the parameter is a prime number, 6 | otherwise return the string false. The range will be between 1 and 2^16. 7 | 8 | Sample test cases: 9 | 10 | Input: 4 11 | Output: "false" 12 | 13 | Input: 1709 14 | Output: "true" 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | using namespace std; 23 | 24 | string Primes(int64_t num) { 25 | num = abs(num); 26 | 27 | if (2 == num || 3 == num || 5 == num) 28 | return "true"; 29 | 30 | if (num % 2 == 0 || num % 3 == 0 || num % 5 == 0) 31 | return "false"; 32 | 33 | const int64_t highest_factor{static_cast(sqrt(num)) + 1}; 34 | 35 | for (int64_t i{7}; i < highest_factor; i += 2) { 36 | if (num % i == 0) 37 | return "false"; 38 | } 39 | 40 | return "true"; 41 | } 42 | 43 | int main() { 44 | // cout << Primes(gets(stdin)); 45 | cout << Primes(3) << '\n'; // expected output: "true" 46 | cout << Primes(4) << '\n'; // expected output: "false" 47 | cout << Primes(5) << '\n'; // expected output: "true" 48 | cout << Primes(7) << '\n'; // expected output: "true" 49 | cout << Primes(19) << '\n'; // expected output: "true" 50 | cout << Primes(110) << '\n'; // expected output: "false" 51 | cout << Primes(71) << '\n'; // expected output: "true" 52 | cout << Primes(1709) << '\n'; // expected output: "true" 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /2_medium_difficulty_level/build.bat: -------------------------------------------------------------------------------- 1 | @rem Simple build script for compiling and building simple C++ source code files 2 | @echo off 3 | pushd build 4 | echo. 5 | echo Building %* using Visual C++ compiler... 6 | cl ..\%* /nologo /W4 /Fe%~n1 /std:c++latest /EHsc /O2 /Ot /Ox /Ob2 /Oy /Oi /link /subsystem:console 7 | echo. 8 | echo Running %~n1.exe... 9 | echo. 10 | .\%~n1.exe 11 | popd -------------------------------------------------------------------------------- /2_medium_difficulty_level/preorder_traversal_iterative_solution_v1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | struct tree_node { 8 | int val; 9 | tree_node* left; 10 | tree_node* right; 11 | 12 | tree_node(const int x) : val{x}, left{}, right{} {} 13 | }; 14 | 15 | class Solution { 16 | public: 17 | vector preorder_traversal(tree_node* root) { 18 | vector output{}; 19 | 20 | if (!root) 21 | return output; 22 | 23 | stack preorder_nodes; 24 | preorder_nodes.emplace(root); 25 | 26 | while (!preorder_nodes.empty()) { 27 | const tree_node* node{preorder_nodes.top()}; 28 | output.emplace_back(node->val); 29 | preorder_nodes.pop(); 30 | 31 | if (node->right) 32 | preorder_nodes.emplace(node->right); 33 | if (node->left) 34 | preorder_nodes.emplace(node->left); 35 | } 36 | 37 | return output; 38 | } 39 | }; 40 | 41 | int main() { 42 | tree_node root_node{1}; 43 | tree_node tree_right_node{2}; 44 | tree_node tree_right_node_left_child{3}; 45 | root_node.right = &tree_right_node; 46 | tree_right_node.left = &tree_right_node_left_child; 47 | 48 | Solution solution{}; 49 | const auto result = solution.preorder_traversal(&root_node); 50 | 51 | for (const auto value : result) 52 | cout << value << ' '; 53 | 54 | cout << '\n'; 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /3_hard_difficulty_level/17_knight_jumps.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Knight Jumps 3 | 4 | Using the C++ language, have the function KnightJumps(str) read str which will 5 | be a string consisting of the location of a knight on a standard 8x8 chess board 6 | with no other pieces on the board. The structure of str will be the following: 7 | "(x y)" which represents the position of the knight with x and y ranging from 1 8 | to 8. Your program should determine the number of spaces the knight can move to 9 | from a given location. For example: if str is "(4 5)" then your program should 10 | output 8 because the knight can move to 8 different spaces from position x=4 and 11 | y=5. 12 | 13 | Sample test cases: 14 | 15 | Input: "(1 1)" 16 | Output: 2 17 | 18 | Input: "(2 8)" 19 | Output: 3 20 | */ 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | using namespace std; 29 | 30 | string trim(const string& input) { 31 | string output{input}; 32 | output.erase(begin(output), 33 | find_if(begin(output), end(output), 34 | [](const char ch) { return !isspace(ch); })); 35 | 36 | output.erase(find_if(output.rbegin(), output.rend(), 37 | [](const char ch) { return !isspace(ch); }) 38 | .base(), 39 | end(output)); 40 | 41 | return output; 42 | } 43 | 44 | pair parse_coordinate_pair_values(string str) { 45 | str.erase(begin(str)); 46 | str.erase(--end(str)); 47 | return make_pair(stoi(str.substr(0, 1)), stoi(str.substr(2, 1))); 48 | } 49 | 50 | string KnightJumps(string str) { 51 | str = trim(str); 52 | 53 | const pair coord{parse_coordinate_pair_values(move(str))}; 54 | 55 | if ((1 == coord.first && 1 == coord.second) || 56 | (1 == coord.first && 8 == coord.second) || 57 | (8 == coord.first && 1 == coord.second) || 58 | (8 == coord.first && 8 == coord.second)) 59 | return "2"; 60 | 61 | if (coord.first >= 3 && coord.first <= 6 && coord.second >= 3 && 62 | coord.second <= 6) 63 | return "8"; 64 | 65 | size_t moves_count{}; 66 | 67 | if (coord.first >= 3) { 68 | if (coord.second <= 7) 69 | moves_count++; 70 | if (coord.second >= 2) 71 | moves_count++; 72 | } 73 | 74 | if (coord.first <= 6) { 75 | if (coord.second <= 7) 76 | moves_count++; 77 | if (coord.second >= 2) 78 | moves_count++; 79 | } 80 | 81 | if (coord.first <= 7) { 82 | if (coord.second <= 6) 83 | moves_count++; 84 | if (coord.second >= 3) 85 | moves_count++; 86 | } 87 | 88 | if (coord.first >= 2) { 89 | if (coord.second <= 6) 90 | moves_count++; 91 | if (coord.second >= 3) 92 | moves_count++; 93 | } 94 | 95 | return to_string(moves_count); 96 | } 97 | 98 | int main() { 99 | // cout << KnightJumps(string{gets(stdin)}); 100 | cout << KnightJumps(string{"(4 5)"}) << '\n'; // expected output: "8" 101 | cout << KnightJumps(string{"(1 1)"}) << '\n'; // expected output: "2" 102 | cout << KnightJumps(string{"(2 8)"}) << '\n'; // expected output: "3" 103 | 104 | return 0; 105 | } 106 | -------------------------------------------------------------------------------- /3_hard_difficulty_level/28_nim_winner.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Nim Winner 3 | 4 | Using the C++ language, have the function NimWinner(arr) take the array of 5 | integers stored in arr which will represent the amount of coins in each pile. 6 | For example: [2, 4, 1, 3] means there are 4 piles of coins and there are 2 coins 7 | in the first pile, 4 in the second pile, etc. Nim is played by each player 8 | removing any number of coins from only one pile, and the winner is the player 9 | who picks up the last coin. For example: if arr is [1, 2, 3] then for example 10 | player 1 can remove 2 coins from the last pile which results in [1, 2, 1]. Then 11 | player 2 can remove 1 coin from the first pile which results in [0, 2, 1], etc. 12 | The player that has the last possible move taking the last coin(s) from a pile 13 | wins the game. Your program should output either 1 or 2 which represents which 14 | player has a guaranteed win with perfect play for the Nim game. 15 | 16 | Sample test cases: 17 | 18 | Input: 1,2,3 19 | Output: 2 20 | 21 | Input: 1,1,1,4,5,4 22 | Output: 1 23 | */ 24 | 25 | #include 26 | #include 27 | 28 | using namespace std; 29 | 30 | int NimWinner(vector coins) { 31 | for (size_t i{1}; i < coins.size(); i++) 32 | coins[0] ^= coins[i]; 33 | if (coins[0]) 34 | return 1; 35 | return 2; 36 | } 37 | 38 | int main() { 39 | // cout << NimWinner(vector{gets(stdin)}); 40 | cout << NimWinner(vector{2, 4, 1, 3}) << '\n'; // 1 41 | cout << NimWinner(vector{1, 2, 3}) << '\n'; // 2 42 | cout << NimWinner(vector{1, 1, 1, 4, 5, 4}) << '\n'; // 1 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /3_hard_difficulty_level/31_kaprekars_constant.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Kaprekars Constant 3 | 4 | Using the C++ language, have the function KaprekarsConstant(num) take the num 5 | parameter being passed which will be a 4-digit number with at least two distinct 6 | digits. Your program should perform the following routine on the number: Arrange 7 | the digits in descending order and in ascending order (adding zeroes to fit it 8 | to a 4-digit number), and subtract the smaller number from the bigger number. 9 | Then repeat the previous step. Performing this routine will always cause you to 10 | reach a fixed number: 6174. Then performing the routine on 6174 will always give 11 | you 6174 (7641 - 1467 = 6174). Your program should return the number of times 12 | this routine must be performed until 6174 is reached. For example: if num is 13 | 3524 your program should return 3 because of the following steps: (1) 5432 - 14 | 2345 = 3087, (2) 8730 - 0378 = 8352, (3) 8532 - 2358 = 6174. 15 | 16 | Sample test cases: 17 | 18 | Input: 2111 19 | Output: 5 20 | 21 | Input: 9831 22 | Output: 7 23 | */ 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | using namespace std; 30 | 31 | size_t KaprekarsConstant(int num) { 32 | size_t iter_count{}; 33 | 34 | while (num != 6174) { 35 | string number_str{to_string(num)}; 36 | const size_t number_str_len{number_str.length()}; 37 | 38 | if (number_str_len < 4) { 39 | number_str.insert(begin(number_str), 4 - number_str_len, '0'); 40 | } 41 | 42 | sort(begin(number_str), end(number_str), 43 | [](const char lc, const char rc) { return lc > rc; }); 44 | 45 | const int lhs_num{stoi(number_str)}; 46 | reverse(begin(number_str), end(number_str)); 47 | const int rhs_num{stoi(number_str)}; 48 | num = lhs_num - rhs_num; 49 | iter_count++; 50 | } 51 | 52 | return iter_count; 53 | } 54 | 55 | int main() { 56 | // cout << KaprekarsConstant(gets(stdin)); 57 | cout << KaprekarsConstant(3524) << '\n'; // expected output: 3 58 | cout << KaprekarsConstant(2111) << '\n'; // expected output: 5 59 | cout << KaprekarsConstant(9831) << '\n'; // expected output: 7 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /3_hard_difficulty_level/34_step_walking.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Step Walking 3 | 4 | Using the C++ language, have the function StepWalking(num) take the num 5 | parameter being passed which will be an integer between 1 and 15 that represents 6 | the number of stairs you will have to climb. You can climb the set of stairs by 7 | taking either 1 step or 2 steps, and you can combine these in any order. So for 8 | example, to climb 3 steps you can either do: (1 step, 1 step, 1 step) or (2, 1) 9 | or (1, 2). So for 3 steps we have 3 different ways to climb them, so your 10 | program should return 3. Your program should return the number of combinations 11 | of climbing num steps. 12 | 13 | Sample test cases: 14 | 15 | Input: 1 16 | Output: 1 17 | 18 | Input: 3 19 | Output: 3 20 | */ 21 | 22 | #include 23 | 24 | using namespace std; 25 | 26 | size_t find_number_of_combinations(const size_t number_of_stairs, 27 | const size_t current_steps_count = 0) { 28 | if (number_of_stairs == current_steps_count) 29 | return 1; 30 | 31 | size_t combinations{}; 32 | 33 | if (current_steps_count < number_of_stairs) 34 | combinations += 35 | find_number_of_combinations(number_of_stairs, current_steps_count + 1); 36 | 37 | if (current_steps_count < number_of_stairs - 1) 38 | combinations += 39 | find_number_of_combinations(number_of_stairs, current_steps_count + 2); 40 | 41 | return combinations; 42 | } 43 | 44 | size_t StepWalking(const size_t number_of_steps) { 45 | const size_t combinations{find_number_of_combinations(number_of_steps)}; 46 | 47 | return combinations; 48 | } 49 | 50 | int main() { 51 | // cout << StepWalking(gets(stdin)); 52 | cout << StepWalking(1) << '\n'; // expected output: 1 53 | cout << StepWalking(2) << '\n'; // expected output: 2 54 | cout << StepWalking(3) << '\n'; // expected output: 3 55 | cout << StepWalking(4) << '\n'; // expected output: 5 56 | cout << StepWalking(5) << '\n'; // expected output: 8 57 | cout << StepWalking(6) << '\n'; // expected output: 13 58 | cout << StepWalking(7) << '\n'; // expected output: 21 59 | cout << StepWalking(8) << '\n'; // expected output: 34 60 | cout << StepWalking(9) << '\n'; // expected output: 55 61 | cout << StepWalking(10) << '\n'; // expected output: 89 62 | 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /3_hard_difficulty_level/39_matching_couples.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Matching Couples 3 | 4 | Using the C++ language, have the function MatchingCouples(arr) take the arr 5 | parameter being passed which will be an array of integers in the following 6 | format: [B, G, N] where B represents the number of boys, G represents the number 7 | of girls, and N represents how many people you want to pair together. Your 8 | program should return the number of different ways you can match boys with girls 9 | given the different arguments. For example: if arr is [5, 3, 2], N=2 here so you 10 | want to pair together 2 people, so you'll need 1 boy and 1 girl. You have 5 ways 11 | to choose a boy and 3 ways to choose a girl, so your program should return 15. 12 | Another example: if arr is [10, 5, 4], here N=4 so you need 2 boys and 2 girls. 13 | We can choose 2 boys from a possible 10, and we can choose 2 girls from a 14 | possible 5. Then we have 2 different ways to pair the chosen boys and girls. Our 15 | program should therefore return 900 16 | 17 | N will always be an even number and it will never be greater than the maximum of 18 | (B, G). B and G will always be greater than zero. 19 | 20 | Sample test cases: 21 | 22 | Input: 5,5,4 23 | Output: 200 24 | 25 | Input: 2,2,2 26 | Output: 4 27 | */ 28 | 29 | #include 30 | 31 | using namespace std; 32 | 33 | int calculate_permutation(int n, const int k) { 34 | const int j{n - k}; 35 | 36 | for (int i{n - 1}; i > j; i--) { 37 | if (i < 2) 38 | break; 39 | n *= i; 40 | } 41 | 42 | return n; 43 | } 44 | 45 | int MatchingCouples(const int* arr, const size_t arr_size) { 46 | if (arr_size < 3) 47 | return -1; 48 | const int B{arr[0]}; 49 | const int G{arr[1]}; 50 | const int N{arr[2] / 2}; 51 | 52 | const int div_factor{N > 2 && N % 2 == 1 ? 2 * N : N}; 53 | 54 | const int result{(calculate_permutation(B, N) * calculate_permutation(G, N)) / 55 | div_factor}; 56 | 57 | return result; 58 | } 59 | 60 | int main() { 61 | // int A[] = gets(stdin); 62 | // cout << MatchingCouples(A, sizeof(A)/sizeof(*A)); 63 | const int B[] = {5, 3, 2}; 64 | cout << MatchingCouples(B, sizeof(B) / sizeof(*B)) 65 | << '\n'; // expected output: 15 66 | const int C[] = {10, 5, 4}; 67 | cout << MatchingCouples(C, sizeof(C) / sizeof(*C)) 68 | << '\n'; // expected output: 900 69 | const int D[] = {5, 5, 4}; 70 | cout << MatchingCouples(D, sizeof(D) / sizeof(*D)) 71 | << '\n'; // expected output: 200 72 | const int E[] = {2, 2, 2}; 73 | cout << MatchingCouples(E, sizeof(E) / sizeof(*E)) 74 | << '\n'; // expected output: 4 75 | const int F[] = {10, 10, 6}; 76 | cout << MatchingCouples(F, sizeof(F) / sizeof(*F)) 77 | << '\n'; // expected output: 86400 78 | const int G[] = {15, 10, 6}; 79 | cout << MatchingCouples(G, sizeof(G) / sizeof(*G)) 80 | << '\n'; // expected output: 327600 81 | 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /3_hard_difficulty_level/40_approaching_fibonacci.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Approaching Fibonacci 3 | 4 | Using the C++ language, have the function ApproachingFibonacci(arr) take the arr 5 | parameter being passed which will be an array of integers and determine the 6 | smallest positive integer (including zero) that can be added to the array to 7 | make the sum of all of the numbers in the array add up to the next closest 8 | Fibonacci number. For example: if arr is [15, 1, 3], then your program should 9 | output 2 because if you add up 15 + 1 + 3 + 2 you get 21 which is the closest 10 | Fibonacci number. 11 | 12 | Sample test cases: 13 | 14 | Input: 5,2,1 15 | Output: 0 16 | 17 | Input: 1,20,2,5 18 | Output: 6 19 | */ 20 | 21 | #include 22 | #include 23 | 24 | using namespace std; 25 | 26 | int ApproachingFibonacci(const int* arr, const size_t arr_size) { 27 | const int arr_sum{accumulate(arr, arr + arr_size, 0)}; 28 | 29 | int a = 0; 30 | int b = 1; 31 | 32 | while (true) { 33 | const int temp{a}; 34 | a = b; 35 | b = a + temp; 36 | 37 | if (b >= arr_sum) 38 | return b - arr_sum; 39 | } 40 | 41 | return 0; 42 | } 43 | 44 | int main() { 45 | // const int A[] = gets(stdin); 46 | // cout << ApproachingFibonacci(A, sizeof(A)/sizeof(*A)); 47 | const int B[] = {15, 1, 3}; 48 | cout << ApproachingFibonacci(B, sizeof(B) / sizeof(*B)) 49 | << '\n'; // expected output: 2 50 | const int C[] = {5, 2, 1}; 51 | cout << ApproachingFibonacci(C, sizeof(C) / sizeof(*C)) 52 | << '\n'; // expected output: 0 53 | const int D[] = {1, 20, 2, 5}; 54 | cout << ApproachingFibonacci(D, sizeof(D) / sizeof(*D)) 55 | << '\n'; // expected output: 6 56 | const int E[] = {6, 6, 4}; 57 | cout << ApproachingFibonacci(E, sizeof(E) / sizeof(*E)) 58 | << '\n'; // expected output: 5 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /3_hard_difficulty_level/43_array_rotation.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Array Rotation 3 | 4 | Using the C++ language, have the function ArrayRotation(arr) take the arr 5 | parameter being passed which will be an array of non-negative integers and 6 | circularly rotate the array starting from the Nth element where N is equal to 7 | the first integer in the array. For example: if arr is [2, 3, 4, 1, 6, 10] then 8 | your program should rotate the array starting from the 2nd position because the 9 | first element in the array is 2. The final array will therefore be [4, 1, 6, 10, 10 | 2, 3], and your program should return the new array as a string, so for this 11 | example your program would return 4161023. The first element in the array will 12 | always be an integer greater than or equal to 0 and less than the size of the 13 | array. 14 | 15 | Sample test cases: 16 | 17 | Input: 2,3,4,1,6,10 18 | Output: "4161023" 19 | 20 | Input: 3,2,1,6 21 | Output: "6321" 22 | 23 | Input: 4,3,4,3,1,2 24 | Output: "124343" 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | using namespace std; 33 | 34 | template 35 | string join(const _Container& items, const string& needle = "") { 36 | ostringstream oss{}; 37 | 38 | auto start = begin(items); 39 | 40 | const auto last = end(items); 41 | 42 | while (start != last) { 43 | if (!needle.empty()) 44 | oss << *start << needle; 45 | else 46 | oss << *start; 47 | 48 | ++start; 49 | } 50 | 51 | string result{oss.str()}; 52 | 53 | const size_t needle_len{needle.length()}; 54 | 55 | if (!needle_len) 56 | return result; 57 | 58 | result.erase(result.length() - needle_len, needle_len); 59 | 60 | return result; 61 | } 62 | 63 | bool rotate_array_elements(deque& deq, const size_t k) { 64 | if (!k || deq.size() < 2) 65 | return false; 66 | 67 | for (size_t i{}; i < k; i++) { 68 | const int number{deq.front()}; 69 | deq.pop_front(); 70 | deq.emplace_back(number); 71 | } 72 | 73 | return true; 74 | } 75 | 76 | string ArrayRotation(const int* arr, const size_t arr_size) { 77 | deque numbers(arr, arr + arr_size); 78 | 79 | const size_t number_of_shifts{numbers[0] % numbers.size()}; 80 | 81 | rotate_array_elements(numbers, number_of_shifts); 82 | 83 | return join(numbers); 84 | } 85 | 86 | int main() { 87 | // const int A[] = gets(stdin); 88 | // cout << ArrayRotation(A, sizeof(A)/sizeof(*A)); 89 | const int B[] = {2, 3, 4, 1, 6, 10}; 90 | cout << ArrayRotation(B, sizeof(B) / sizeof(*B)) 91 | << '\n'; // expected output: "4161023" 92 | const int C[] = {3, 2, 1, 6}; 93 | cout << ArrayRotation(C, sizeof(C) / sizeof(*C)) 94 | << '\n'; // expected output: "6321" 95 | const int D[] = {4, 3, 4, 3, 1, 2}; 96 | cout << ArrayRotation(D, sizeof(D) / sizeof(*D)) 97 | << '\n'; // expected output: "124343" 98 | 99 | return 0; 100 | } 101 | -------------------------------------------------------------------------------- /3_hard_difficulty_level/44_bracket_combinations_v1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Bracket Combinations 3 | (simplified, terse recursive solution) 4 | 5 | Using the C++ language, have the function BracketCombinations(num) read num 6 | which will be an integer greater than or equal to zero, and return the number of 7 | valid combinations that can be formed with num pairs of parentheses. For 8 | example, if the input is 3, then the possible combinations of 3 pairs of 9 | parenthesis, namely: 10 | ()()(), are ()()(), ()(()), (())(), ((())), and (()()). 11 | There are 5 total combinations when the input is 3, so your program should 12 | return 5. 13 | 14 | Sample test cases: 15 | 16 | Input: 3 17 | Output: 5 18 | 19 | Input: 2 20 | Output: 2 21 | */ 22 | 23 | #include 24 | 25 | using std::cout; 26 | 27 | size_t find_number_of_possible_combinations( 28 | const size_t number_of_pairs, 29 | const size_t used_open_prnths = 0, 30 | const size_t used_close_prnths = 0) { 31 | if (number_of_pairs == used_close_prnths) 32 | return 1; 33 | 34 | size_t combinations{}; 35 | 36 | if (used_open_prnths < number_of_pairs) 37 | combinations += find_number_of_possible_combinations( 38 | number_of_pairs, used_open_prnths + 1, used_close_prnths); 39 | 40 | if (used_close_prnths < used_open_prnths) 41 | combinations += find_number_of_possible_combinations( 42 | number_of_pairs, used_open_prnths, used_close_prnths + 1); 43 | 44 | return combinations; 45 | } 46 | 47 | size_t BracketCombinations_v1(const size_t num) { 48 | const size_t combinations{find_number_of_possible_combinations(num, 0, 0)}; 49 | 50 | return combinations; 51 | } 52 | 53 | int main() { 54 | // cout << BracketCombinations_v1(gets(stdin)); 55 | cout << BracketCombinations_v1(2) << '\n'; // expected output: 2 56 | cout << BracketCombinations_v1(3) << '\n'; // expected output: 5 57 | cout << BracketCombinations_v1(4) << '\n'; // expected output: 14 58 | cout << BracketCombinations_v1(5) << '\n'; // expected output: 42 59 | cout << BracketCombinations_v1(6) << '\n'; // expected output: 132 60 | cout << BracketCombinations_v1(7) << '\n'; // expected output: 429 61 | cout << BracketCombinations_v1(8) << '\n'; // expected output: 1430 62 | cout << BracketCombinations_v1(9) << '\n'; // expected output: 4862 63 | cout << BracketCombinations_v1(10) << '\n'; // expected output: 16796 64 | cout << BracketCombinations_v1(11) << '\n'; // expected output: 58786 65 | cout << BracketCombinations_v1(12) << '\n'; // expected output: 208012 66 | 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /3_hard_difficulty_level/44_bracket_combinations_v2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Bracket Combinations 3 | (iterative solution implemented using brute-force method with the help of 4 | std::next_permutation) 5 | 6 | Using the C++ language, have the function BracketCombinations(num) read num 7 | which will be an integer greater than or equal to zero, and return the number of 8 | valid combinations that can be formed with num pairs of parentheses. For 9 | example, if the input is 3, then the possible combinations of 3 pairs of 10 | parenthesis, namely: 11 | ()()(), are ()()(), ()(()), (())(), ((())), and (()()). 12 | There are 5 total combinations when the input is 3, so your program should 13 | return 5. 14 | 15 | Sample test cases: 16 | 17 | Input: 3 18 | Output: 5 19 | 20 | Input: 2 21 | Output: 2 22 | */ 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | using std::begin; 29 | using std::cout; 30 | using std::end; 31 | using std::next_permutation; 32 | using std::sort; 33 | using std::string; 34 | 35 | bool is_valid_combinations_of_bracket_pairs(const string& str) { 36 | int brackets_balance_count{}; 37 | 38 | if (')' == str[0]) 39 | return false; 40 | 41 | if ('(' == str[str.length() - 1]) 42 | return false; 43 | 44 | for (size_t i{}; i <= str.length(); i++) { 45 | if ('(' == str[i]) 46 | brackets_balance_count++; 47 | 48 | else if (')' == str[i]) { 49 | if (brackets_balance_count <= 0) 50 | return false; 51 | 52 | brackets_balance_count--; 53 | } 54 | } 55 | 56 | if (brackets_balance_count) 57 | return false; 58 | 59 | return true; 60 | } 61 | 62 | size_t find_number_of_possible_combinations(const size_t number_of_pairs) { 63 | size_t combinations{}; 64 | 65 | string brackets{string(number_of_pairs, '(') + string(number_of_pairs, ')')}; 66 | 67 | sort(begin(brackets), end(brackets)); 68 | 69 | do { 70 | if (is_valid_combinations_of_bracket_pairs(brackets)) 71 | combinations++; 72 | } while (next_permutation(begin(brackets), end(brackets))); 73 | 74 | return combinations; 75 | } 76 | 77 | size_t BracketCombinations_v2(const size_t num) { 78 | return find_number_of_possible_combinations(num); 79 | } 80 | 81 | int main() { 82 | // cout << BracketCombinations_v2(gets(stdin)); 83 | cout << BracketCombinations_v2(2) << '\n'; // expected output: 2 84 | cout << BracketCombinations_v2(3) << '\n'; // expected output: 5 85 | cout << BracketCombinations_v2(4) << '\n'; // expected output: 14 86 | cout << BracketCombinations_v2(5) << '\n'; // expected output: 42 87 | cout << BracketCombinations_v2(6) << '\n'; // expected output: 132 88 | cout << BracketCombinations_v2(7) << '\n'; // expected output: 429 89 | cout << BracketCombinations_v2(8) << '\n'; // expected output: 1430 90 | cout << BracketCombinations_v2(9) << '\n'; // expected output: 4862 91 | cout << BracketCombinations_v2(10) << '\n'; // expected output: 16796 92 | cout << BracketCombinations_v2(11) << '\n'; // expected output: 58786 93 | cout << BracketCombinations_v2(12) << '\n'; // expected output: 208012 94 | 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /3_hard_difficulty_level/44_bracket_combinations_v3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Bracket Combinations 3 | (alternative iterative solution implemented by using queue) 4 | 5 | Using the C++ language, have the function BracketCombinations(num) read num 6 | which will be an integer greater than or equal to zero, and return the number of 7 | valid combinations that can be formed with num pairs of parentheses. For 8 | example, if the input is 3, then the possible combinations of 3 pairs of 9 | parenthesis, namely: 10 | ()()(), are ()()(), ()(()), (())(), ((())), and (()()). 11 | There are 5 total combinations when the input is 3, so your program should 12 | return 5. 13 | 14 | Sample test cases: 15 | 16 | Input: 3 17 | Output: 5 18 | 19 | Input: 2 20 | Output: 2 21 | */ 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | using std::cout; 28 | using std::make_pair; 29 | using std::pair; 30 | using std::queue; 31 | 32 | size_t find_number_of_possible_combinations(const size_t number_of_pairs) { 33 | size_t combinations{}; 34 | queue> q{{make_pair(0, 0)}}; 35 | 36 | while (!q.empty()) { 37 | const size_t used_open_prnths{q.front().first}; 38 | const size_t used_close_prnths{q.front().second}; 39 | q.pop(); 40 | 41 | if (number_of_pairs == used_close_prnths) 42 | combinations++; 43 | 44 | else { 45 | if (used_open_prnths < number_of_pairs) 46 | q.emplace(make_pair(used_open_prnths + 1, used_close_prnths)); 47 | if (used_close_prnths < used_open_prnths) 48 | q.emplace(make_pair(used_open_prnths, used_close_prnths + 1)); 49 | } 50 | } 51 | 52 | return combinations; 53 | } 54 | 55 | size_t BracketCombinations_v3(const size_t num) { 56 | return find_number_of_possible_combinations(num); 57 | } 58 | 59 | int main() { 60 | // cout << BracketCombinations_v3(gets(stdin)); 61 | cout << BracketCombinations_v3(2) << '\n'; // expected output: 2 62 | cout << BracketCombinations_v3(3) << '\n'; // expected output: 5 63 | cout << BracketCombinations_v3(4) << '\n'; // expected output: 14 64 | cout << BracketCombinations_v3(5) << '\n'; // expected output: 42 65 | cout << BracketCombinations_v3(6) << '\n'; // expected output: 132 66 | cout << BracketCombinations_v3(7) << '\n'; // expected output: 429 67 | cout << BracketCombinations_v3(8) << '\n'; // expected output: 1430 68 | cout << BracketCombinations_v3(9) << '\n'; // expected output: 4862 69 | cout << BracketCombinations_v3(10) << '\n'; // expected output: 16796 70 | cout << BracketCombinations_v3(11) << '\n'; // expected output: 58786 71 | cout << BracketCombinations_v3(12) << '\n'; // expected output: 208012 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /3_hard_difficulty_level/49_pentagonal_number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Coderbyte coding challenge: Pentagonal Number 3 | 4 | Using the C++ language, have the function PentagonalNumber(num) read num which 5 | will be a positive integer and determine how many dots exist in a pentagonal 6 | shape around a center dot on the Nth iteration. For example, in the image below 7 | you can see that on the first iteration there is only a single dot, on the 8 | second iteration there are 6 dots, on the third there are 16 dots, and on the 9 | fourth there are 31 dots. 10 | 11 | Your program should return the number of dots that exist in the whole pentagon 12 | on the Nth iteration. 13 | 14 | Sample test cases: 15 | 16 | Input: 2 17 | Output: 6 18 | 19 | Input: 5 20 | Output: 51 21 | */ 22 | 23 | #include 24 | #include 25 | 26 | using namespace std; 27 | 28 | size_t pentagonal_number(const size_t number_of_iterations) { 29 | static map precalculated_pentagonal_numbers{ 30 | {1, 1}, {2, 6}, {3, 16}}; 31 | 32 | if (end(precalculated_pentagonal_numbers) != 33 | precalculated_pentagonal_numbers.find(number_of_iterations)) 34 | return precalculated_pentagonal_numbers.at(number_of_iterations); 35 | 36 | auto lb_iter = 37 | precalculated_pentagonal_numbers.upper_bound(number_of_iterations); 38 | 39 | if (lb_iter != end(precalculated_pentagonal_numbers) && 40 | lb_iter != begin(precalculated_pentagonal_numbers)) 41 | --lb_iter; 42 | 43 | size_t number_of_dots{ 44 | lb_iter != end(precalculated_pentagonal_numbers) ? lb_iter->second : 1}; 45 | size_t current_level_dots{lb_iter != end(precalculated_pentagonal_numbers) 46 | ? lb_iter->first * 5 47 | : 5}; 48 | size_t i{lb_iter != end(precalculated_pentagonal_numbers) ? lb_iter->first + 1 49 | : 2}; 50 | 51 | for (; i <= number_of_iterations; i++) { 52 | number_of_dots += current_level_dots; 53 | current_level_dots += 5; 54 | } 55 | 56 | precalculated_pentagonal_numbers[number_of_iterations] = number_of_dots; 57 | 58 | return number_of_dots; 59 | } 60 | 61 | int main() { 62 | // cout << pentagonal_number(gets(stdin)); 63 | cout << pentagonal_number(1) << '\n'; // expected output: 1 64 | cout << pentagonal_number(2) << '\n'; // expected output: 6 65 | cout << pentagonal_number(3) << '\n'; // expected output: 16 66 | cout << pentagonal_number(4) << '\n'; // expected output: 31 67 | cout << pentagonal_number(5) << '\n'; // expected output: 51 68 | cout << pentagonal_number(6) << '\n'; // expected output: 76 69 | cout << pentagonal_number(7) << '\n'; // expected output: 106 70 | cout << pentagonal_number(8) << '\n'; // expected output: 141 71 | 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /3_hard_difficulty_level/build.bat: -------------------------------------------------------------------------------- 1 | @rem Simple build script for compiling and building simple C++ source code files 2 | @echo off 3 | pushd build 4 | echo. 5 | echo Building %* using Visual C++ compiler... 6 | cl ..\%* -nologo -W4 -Fe%~n1 -EHsc -O2 -Ot -Ox -Ob2 -Oy -Oi 7 | echo. 8 | echo Running %~n1.exe... 9 | echo. 10 | .\%~n1.exe 11 | popd -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14 FATAL_ERROR) 2 | set(This solved_coderbyte_coding_challenges) 3 | project(${This} VERSION 0.1.0) 4 | 5 | set(CMAKE_CXX_STANDARD 17) 6 | set(CMAKE_CXX_STANDARD_REQUIRED 17) 7 | 8 | if (MSVC) 9 | add_compile_options(/nologo /std:c++17 /W4 /EHsc /Od) 10 | else () 11 | add_compile_options(-std=c++17 -Wall -Wextra -Wpedantic -O0 -g) 12 | endif () 13 | 14 | add_subdirectory(dependencies) 15 | list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras) 16 | enable_testing() 17 | set(CPACK_PROJECT_NAME ${PROJECT_NAME}) 18 | set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) 19 | include(CPack) 20 | include(CTest) 21 | 22 | set(sources 1_easy_difficulty_level/20_division_stringified.cpp) 23 | 24 | add_executable(${This} ${sources}) 25 | target_include_directories(${This} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) 26 | target_include_directories(${This} PUBLIC Catch2) 27 | target_link_libraries(${This} PUBLIC Catch2::Catch2WithMain) 28 | 29 | add_test( 30 | NAME ${This} 31 | COMMAND ${This} 32 | ) 33 | 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Solved Coderbyte coding challenges 2 | 3 | This code repository's mission is to keep track of all the coderbyte coding challenges I have solved so far. As time goes by it will be continuously updated with newer solved coding challenges. 4 | 5 | The solved Coderbyte coding challenges are categorized into 3 groups accoding to their difficulty levels: 6 | 7 | I. Easy
8 | II. Medium
9 | III. Hard
10 | -------------------------------------------------------------------------------- /dependencies/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14 FATAL_ERROR) 2 | 3 | include(FetchContent) 4 | FetchContent_Declare(Catch2 5 | GIT_REPOSITORY https://github.com/catchorg/Catch2.git 6 | GIT_TAG v3.0.1) 7 | FetchContent_MakeAvailable(Catch2) --------------------------------------------------------------------------------