-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverse_Array_byRecursion_using2Pointers.cpp.cpp
More file actions
52 lines (28 loc) · 1.41 KB
/
Copy pathReverse_Array_byRecursion_using2Pointers.cpp.cpp
File metadata and controls
52 lines (28 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<bits/stdc++.h>
using namespace std;
// Lets reverse the array using Recursion
//1st Method- By taking two pointers concept, here will take 2 different variables, one pointing to first index and one point to last index.
// One thing to keep in mind is that, Array is passed by Reference, means the value changes in its original memory.
//This means the function receives the memory address of the array's beginning, not a copy of the entire array.
//Passing arrays by value (making a full copy) would be inefficient, especially for large arrays, as it would consume significant memory and processing time. Passing a pointer (the address) is much more efficient. Just unlike variables we dont use '&' symbol for array as its automatically takes the address.
void f(int i , int j , int arr[]){
if(i>=j) return;
swap(arr[i] , arr[j]);
f(i+1, j-1, arr);
}
int main(){
int n ;
cout<<"Enter the size of array you want: ";
cin>>n;
int arr[n];
// lets insert the value in array now
cout<<"Enter the values in the array : ";
for(int i=0 ; i<n ; i++){
cin>>arr[i];
}
f(0,n-1,arr); // passed 1st index , last index and array
// Now lets print the values of Array
for(int i=0 ; i<n ; i++){
cout<<arr[i]<<" ";
}
}