Skip to content

Commit

Permalink
Added Find Pivot Index
Browse files Browse the repository at this point in the history
  • Loading branch information
Ace-Krypton committed Sep 30, 2023
1 parent db3002f commit d59f999
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1118,4 +1118,15 @@ add_executable(FindTheHighestAltitude
target_link_libraries(
FindTheHighestAltitude
GTest::gtest_main
)

# Find Pivot Index
add_executable(FindPivotIndex
Easy/FindPivotIndex/include/solution.hpp
Easy/FindPivotIndex/tests/test.cpp
)

target_link_libraries(
FindPivotIndex
GTest::gtest_main
)
23 changes: 23 additions & 0 deletions Easy/FindPivotIndex/include/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <vector>
#include <numeric>
#include <iostream>
#include <algorithm>
#include <gtest/gtest.h>

class Solution {
public:
static auto pivot_index(std::vector<int32_t> &nums) -> int32_t {
int32_t left_sum = 0;
int32_t right_sum = std::accumulate(nums.begin(), nums.end(), 0);

for (size_t i = 0; i < nums.size(); ++i) {
right_sum -= nums[i];
if (right_sum == left_sum) return i;
left_sum += nums[i];
}

return -1;
}
};
38 changes: 38 additions & 0 deletions Easy/FindPivotIndex/tests/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "../include/solution.hpp"

class FindPivotIndex : public ::testing::Test {
protected:
~FindPivotIndex() override = default;
};

TEST_F(FindPivotIndex, FirstTest) {
std::vector<int32_t> nums = {1, 7, 3, 6, 5, 6};

int32_t result = Solution::pivot_index(nums);
int32_t expected = 3;

ASSERT_EQ(result, expected);
}

TEST_F(FindPivotIndex, SecondTest) {
std::vector<int32_t> nums = {1, 2, 3};

int32_t result = Solution::pivot_index(nums);
int32_t expected = -1;

ASSERT_EQ(result, expected);
}

TEST_F(FindPivotIndex, ThirdTest) {
std::vector<int32_t> nums = {2, 1, -1};

int32_t result = Solution::pivot_index(nums);
int32_t expected = 0;

ASSERT_EQ(result, expected);
}

auto main(int argc, char **argv) -> int {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
8 changes: 4 additions & 4 deletions Easy/FindTheHighestAltitude/tests/test.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "../include/solution.hpp"

class ContainsDuplicateII : public ::testing::Test {
class FindTheHighestAltitude : public ::testing::Test {
protected:
~ContainsDuplicateII() override = default;
~FindTheHighestAltitude() override = default;
};

TEST_F(ContainsDuplicateII, FirstTest) {
TEST_F(FindTheHighestAltitude, FirstTest) {
std::vector<int32_t> nums = {-5, 1, 5, 0, -7};

int32_t result = Solution::largest_altitude(nums);
Expand All @@ -14,7 +14,7 @@ TEST_F(ContainsDuplicateII, FirstTest) {
ASSERT_EQ(result, expected);
}

TEST_F(ContainsDuplicateII, SecondTest) {
TEST_F(FindTheHighestAltitude, SecondTest) {
std::vector<int32_t> nums = {-4, -3, -2, -1, 4, 3, 2};

int32_t result = Solution::largest_altitude(nums);
Expand Down

0 comments on commit d59f999

Please sign in to comment.