Skip to content

Commit

Permalink
Added solution for 167. Two Sum II - Input Array Is Sorted: https://l…
Browse files Browse the repository at this point in the history
  • Loading branch information
asesh committed Oct 17, 2024
1 parent ef09820 commit 6c15f38
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 19 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "411"
endingLineNumber = "411"
startingLineNumber = "413"
endingLineNumber = "413"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -30,8 +30,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "377"
endingLineNumber = "377"
startingLineNumber = "379"
endingLineNumber = "379"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -46,8 +46,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "302"
endingLineNumber = "302"
startingLineNumber = "304"
endingLineNumber = "304"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -62,8 +62,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "270"
endingLineNumber = "270"
startingLineNumber = "272"
endingLineNumber = "272"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down Expand Up @@ -126,8 +126,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "230"
endingLineNumber = "230"
startingLineNumber = "232"
endingLineNumber = "232"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -142,8 +142,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "193"
endingLineNumber = "193"
startingLineNumber = "195"
endingLineNumber = "195"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -158,8 +158,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "152"
endingLineNumber = "152"
startingLineNumber = "154"
endingLineNumber = "154"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down Expand Up @@ -190,8 +190,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "141"
endingLineNumber = "141"
startingLineNumber = "143"
endingLineNumber = "143"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -206,8 +206,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "127"
endingLineNumber = "127"
startingLineNumber = "129"
endingLineNumber = "129"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down
4 changes: 3 additions & 1 deletion algorithm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
#include "graph.hpp"

int main(int argc, const char* argv[]) {
invoke_find_three_number_sum();
invoke_two_sum_ii();

// invoke_find_three_number_sum();

// invoke_separate_black_and_white_balls();

Expand Down
27 changes: 27 additions & 0 deletions algorithm/number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1415,3 +1415,30 @@ void invoke_separate_black_and_white_balls() {
<<separate_black_and_white_balls(input)
<<std::endl;
}

/*
Input: [2,7,11,15], Target: 22
Output: [1, 3]
*/
std::vector<int> two_sum_ii(std::vector<int>& numbers, int target) {
int low = 0, high = numbers.size() - 1;
while(low < high) {
auto sum = numbers[low] + numbers[high];
if(sum == target) {
return {low + 1, high + 1};
} else if(sum < target) {
++low;
} else {
--high;
}
}
return {};
}
void invoke_two_sum_ii() {
std::vector<int> input = {2,7,11,15};
auto output = two_sum_ii(input, 22);
std::cout<<"The indices of two sum ii are: ";
std::for_each(output.begin(), output.end(), [](auto& number) {
std::cout<<number<<", ";
});
}
5 changes: 5 additions & 0 deletions algorithm/number.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,9 @@ void invoke_set_matrix_zeroes();
*/
void invoke_separate_black_and_white_balls();

/*
167. Two Sum II - Input Array Is Sorted
*/
void invoke_two_sum_ii();

#endif /* NUMBER_HPP */
22 changes: 22 additions & 0 deletions algorithm/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,28 @@ void invoke_reverse_words_in_a_string() {
std::cout<<reverse_words_in_a_string(input)<<std::endl;
}

/*
Input: a = 1, b = 1, c = 7
Output: "ccaccbcc"
cc -> c = 5
a -> a = 0
cc -> c = 3
b -> b = 0
cc
Input: a = 7, b = 7, c = 7
Output: "abcabcabcabcabcabcabc"
Pseudocode:
output = ""
max1, max2, max3
while(max1 >= 0) {
}
Input: a = 7, b = 1, c = 0
Output: "aabaa"
*/
std::string longest_happy_string(int a, int b, int c) {
return "";
}
Expand Down

0 comments on commit 6c15f38

Please sign in to comment.