Skip to content

Instantly share code, notes, and snippets.

@omokehinde
Created February 10, 2022 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omokehinde/02be846a43729c9ac11c80dcb8e637c2 to your computer and use it in GitHub Desktop.
Save omokehinde/02be846a43729c9ac11c80dcb8e637c2 to your computer and use it in GitHub Desktop.
HackerRank Absolute Permutation challenge
# We define to be a permutation of the first natural numbers in the range . Let denote the value at position in permutation using -based indexing.
# is considered to be an absolute permutation if holds true for every .
# Given and , print the lexicographically smallest absolute permutation . If no absolute permutation exists, print -1.
# Example
# Create an array of elements from to , . Using based indexing, create a permutation where every . It can be rearranged to so that all of the absolute differences equal :
# pos[i] i |pos[i] - i|
# 3 1 2
# 4 2 2
# 1 3 2
# 2 4 2
# Function Description
# Complete the absolutePermutation function in the editor below.
# absolutePermutation has the following parameter(s):
# int n: the upper bound of natural numbers to consider, inclusive
# int k: the absolute difference between each element's value and its index
# Returns
# int[n]: the lexicographically smallest permutation, or if there is none
# Input Format
# The first line contains an integer , the number of queries.
# Each of the next lines contains space-separated integers, and .
# Constraints
# Sample Input
# STDIN Function
# ----- --------
# 3 t = 3 (number of queries)
# 2 1 n = 2, k = 1
# 3 0 n = 3, k = 0
# 3 2 n = 3, k = 2
# Sample Output
# 2 1
# 1 2 3
# -1
# Explanation
# Test Case 0:
def absolutePermutation(n, k):
# Write your code here
output = []
for i in range(1,n+1):
for j in range(n,0,-1):
if abs(i-j) == k and j not in output:
output.append(j)
if len(output) == n:
print(*output)
else: print(-1)
absolutePermutation(4,1)
@omokehinde
Copy link
Author

N**2 complexity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment