chore(CPlusPlus): create reverse-array.cpp (#283)
parent
d2205468ef
commit
db41d89a78
|
@ -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<bits/stdc++.h>
|
||||
using namespace std ;
|
||||
void reverse_array(vector<int> 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<<arr[i]<<" ";
|
||||
}
|
||||
}
|
||||
int main(){
|
||||
int n ; //Size of the array
|
||||
cin>>n;
|
||||
int item ;
|
||||
std::vector<int> arr ;
|
||||
while(n--){
|
||||
cin>>item;
|
||||
arr.push_back(item);
|
||||
}
|
||||
reverse_array(arr) ; //Function call
|
||||
return 0;
|
||||
}
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue