-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bad4f67
commit ab830fc
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); | ||
|
||
|
||
|
||
} |