diff --git a/algorithms/CPlusPlus/Arrays/reverse-array.cpp b/algorithms/CPlusPlus/Arrays/reverse-array.cpp new file mode 100644 index 00000000..d43f5feb --- /dev/null +++ b/algorithms/CPlusPlus/Arrays/reverse-array.cpp @@ -0,0 +1,33 @@ +//Description : This program reverses array elements using two pointer method. +//Time Complexity : O(n), where n is the array size +//Auxiliary Space : O(1) +#include +using namespace std ; +void reverse_array(vector arr){ +// Here we will use two pointer approach +int low = 0 ; //First pointer at the beginning of the array +int high = arr.size()-1; //Second pointer at the end of the array + +while(low < high){ + //Swapping array items at the low and high index + swap(arr[low],arr[high]); + low++; //Incrementing the low pointer + high--; //Decrementing the high pointer +} + + for(int i = 0; i < arr.size() ; i++){ + cout<>n; + int item ; + std::vector arr ; + while(n--){ + cin>>item; + arr.push_back(item); + } + reverse_array(arr) ; //Function call + return 0; +} diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index ae89c2ce..71353eb9 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -7,6 +7,7 @@ 4. [Max Subarray Sum](Arrays/max-subarray-sum.cpp) 5. [Shift Negatives](Arrays/shift-negatives.cpp) 6. [Boyer–Moore Voting Algorithm](Arrays/boyer_more.cpp) +7. [Reverse Array](Arrays/reverse-array.cpp) ## Graphs 1. [Bellman Ford Algorithm](Graphs/bellmam-ford.cpp)