Skip to content

Commit

Permalink
Added more algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
asesh committed Jun 20, 2024
1 parent 6bd3e0d commit db610d1
Show file tree
Hide file tree
Showing 5 changed files with 144 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 = "382"
endingLineNumber = "382"
startingLineNumber = "388"
endingLineNumber = "388"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -30,8 +30,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "348"
endingLineNumber = "348"
startingLineNumber = "354"
endingLineNumber = "354"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -46,8 +46,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "273"
endingLineNumber = "273"
startingLineNumber = "279"
endingLineNumber = "279"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -62,8 +62,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "241"
endingLineNumber = "241"
startingLineNumber = "247"
endingLineNumber = "247"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down Expand Up @@ -126,8 +126,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "199"
endingLineNumber = "199"
startingLineNumber = "205"
endingLineNumber = "205"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -142,8 +142,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "162"
endingLineNumber = "162"
startingLineNumber = "168"
endingLineNumber = "168"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -158,8 +158,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "121"
endingLineNumber = "121"
startingLineNumber = "127"
endingLineNumber = "127"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down Expand Up @@ -190,8 +190,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "110"
endingLineNumber = "110"
startingLineNumber = "116"
endingLineNumber = "116"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -206,8 +206,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "96"
endingLineNumber = "96"
startingLineNumber = "102"
endingLineNumber = "102"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down Expand Up @@ -260,5 +260,21 @@
landmarkType = "9">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "DB4A9C10-0978-4AE5-8591-4C1F8936CBD0"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "algorithm/number.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "936"
endingLineNumber = "936"
landmarkName = "reverse_integer(input)"
landmarkType = "9">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
8 changes: 7 additions & 1 deletion algorithm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@

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

invoke_remove_element();
// invoke_four_number_sum();

invoke_reverse_integer();

// invoke_rotate_array();

// invoke_remove_element();

// invoke_merge_sorted_arrays();

Expand Down
81 changes: 81 additions & 0 deletions algorithm/number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -861,3 +861,84 @@ void invoke_remove_duplicates_from_sorted_array() {
std::cout<<num<<", ";
}
}

/*
I
1 1 1 2 2 3
*
*/
int remove_duplicates_more_than_two(std::vector<int>& nums) {
int index = 0;
for(auto& num: nums) {
if(index == 0 || index == 1 || nums[index - 2] != num) {
nums[index] = num;
++index;
}
}

return index;
}
void invoke_remove_duplicates_more_than_two() {
std::vector<int> nums = {0,0,1,1,1,1,2,3,3};
auto answer = remove_duplicates_more_than_two(nums);
std::cout<<answer<<std::endl;
for(auto& num: nums) {
std::cout<<num<<", ";
}
}

/*
Input: [1, -100, 12, 4, 20, 5, 60, -5, -7] Target: 5
Output: [1, 4, 5, -5], [12, 5, -5, -7]
* * * * * * * *
[1, -100, 12, 4, 20, 5, 60, -5, -7] and [1, -100, 12, 4, 20, 5, 60, -5, -7]
*/
std::vector<std::vector<int>> four_number_sum(std::vector<int>& nums) {
std::vector<std::vector<int>> output;
return output;
}
void invoke_four_number_sum() {
std::vector<int> nums = {1, -100, 12, 4, 20, 5, 60, -5, -7};
auto output = four_number_sum(nums);
for(auto& nums_array: output) {
for(auto& num: nums_array) {
std::cout<<num<<", ";
}
std::cout<<std::endl;
}
}

void rotate_array(std::vector<int> nums, int k) {
k = k % nums.size();
std::reverse(nums.begin(), nums.end()); // Reverse the entire array
std::reverse(nums.begin(), nums.begin() + k); // Reverse the first k elements
std::reverse(nums.begin() + k, nums.end()); // Reverse the k+ elements
}
void invoke_rotate_array() {
std::vector<int> nums = {1,2,3,4,5,6,7};
rotate_array(nums, 3);
for(auto& num: nums) {
std::cout<<num<<", ";
}
}

/*
123
3*100 + 2*10 + 1*1 = 300+20+1 = 321
1234
4*1000 + 3*100 + 2*10 + 1 = 4321
*/
int reverse_integer(int input) {
int output = 0;
do {
auto last_number = input % 10; // 3
input /= 10;
output = (output * 10) + last_number;
} while(input != 0);
return output;
}
void invoke_reverse_integer() {
std::cout<<reverse_integer(-123);
}
22 changes: 22 additions & 0 deletions algorithm/number.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,26 @@ void invoke_remove_element();
*/
void invoke_remove_duplicates_from_sorted_array();


/*
80. Remove Duplicates from Sorted Array II: https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/
*/
void invoke_remove_duplicates_more_than_two();


/*
https://www.algoexpert.io/questions/four-number-sum
*/
void invoke_four_number_sum();

/*
189. https://leetcode.com/problems/rotate-array
*/
void invoke_rotate_array();

/*
7. Reverse Integer https://leetcode.com/problems/reverse-integer/description/
*/
void invoke_reverse_integer();

#endif /* NUMBER_HPP */

0 comments on commit db610d1

Please sign in to comment.