Skip to content

Commit

Permalink
Create ABS_PERM.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Priyanshiguptaaa authored Dec 4, 2020
1 parent 920b3c0 commit 8d3aca4
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Arrays/ABS_PERM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
vector<int> absolutePermutation(int n, int k)
{
vector<int>res;
if(k != 0)
{
if (n % 2 == 1)
return {-1};
if (n % 2 == 0 && n%(k*2)==0 )
{
int j = 1;
for (int i = 1; i <= n; i++)
{
if(j<= k)
{
res.push_back(i+k);
}
else
{
res.push_back(i-k);
}

j++;
if(j > k*2)j = 1;
}
return res;
}
else
{
return {-1};
}
}
else
{
for (int i = 1; i <= n; i++)
{
res.push_back(i);
}
return res;
}
}

0 comments on commit 8d3aca4

Please sign in to comment.