-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwoPointer3.cpp
49 lines (44 loc) · 909 Bytes
/
TwoPointer3.cpp
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
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void SquareNumberEach(vector<int> &v)
{
int left_ptr=0;
int right_ptr=v.size()-1;
vector<int> ans;
while(left_ptr<=right_ptr)
{
if(abs(v[left_ptr])>abs(v[right_ptr]))
{
ans.push_back(v[left_ptr] * v[left_ptr]);
left_ptr++;
}
else
{
ans.push_back(v[right_ptr]*v[right_ptr]);
right_ptr--;
}
}
reverse(ans.begin(),ans.end());
cout<<"Output here:"<<endl;
for(int i=0;i<ans.size();i++)
{
cout<<ans[i]<<" ";
}
}
int main()
{
cout<<"Given all the integer array 'a' sorted in non decreasing order return an rray of the square f eachnumber in non decreasing order"<<endl;
cout<<endl;
int size;
cout<<"Enter the size of the vector: "<<endl;
cin>>size;
vector<int> v(size);
cout<<"Enter the values for the vector:"<<endl;
for(int i=0;i<size;i++)
{
cin>>v[i];
}
SquareNumberEach(v);
}