Sort by

recency

|

526 Discussions

|

  • + 1 comment

    Python

    def absolutePermutation(n, k):

    if k !=0 and n% (2*k) !=0:
        return [-1]
    if k == 0:
        return list(range(1, n+1))
    return [(u + k if (u-1) % (2*k) + 1 <= k else u - k) for u in range(1, n+1)]
    
  • + 0 comments

    swift

    func absolutePermutation(n: Int, k: Int) -> [Int] {
        var results: [Int] = [Int]()
        var isUpper: Bool = false
        
        if k == 0 { return Array(1 ... n) }
        
        for number in 0 ... n - 1 {        
            if number % k == 0 { isUpper = !isUpper }
            
            if isUpper { results.append((number + 1) + k) }
            else { results.append((number + 1) - k) }
        
            if results.last! > n { return [-1] }
        }
        return results
    }
    
  • + 0 comments

    Haskell

    module Main where
    
    import Control.Monad (replicateM_)
    import qualified Data.Set  as S
    
    solve :: Int -> Int -> Maybe [Int]
    solve n k = go S.empty [] [1 .. n]
      where
        go _ acc [] = Just $ reverse acc
        go used acc (x : xs)
            | x - k >= 1
                && (x - k) `S.notMember` used =
                go (S.insert (x - k) used) (x - k : acc) xs
            | x + k <= n
                && (x + k) `S.notMember` used =
                go (S.insert (x + k) used) (x + k : acc) xs
            | otherwise = Nothing
    
    main :: IO ()
    main = do
        cases <- readLn :: IO Int
        replicateM_ cases $ do
            [n, k] <- map read . words <$> getLine :: IO [Int]
            case solve n k of
                Just ps -> putStrLn $ unwords $ map show ps
                Nothing -> putStrLn "-1"
    
  • + 0 comments

    Typescript

    let map: Map<number, number> = new Map();
        for(let i = 1; i <= n; i++) {
            if (i - k > 0 && !map.has(i - k)) {
                map.set(i - k, i);
            } else if (i + k <= n) {
                map.set(i + k, i);
            } else{
                return [-1];
            }
        }
        return [...map.keys()];
    
  • + 1 comment

    I think there is wrong answer in test case n = 69660 and k = 1620 this shouldn't be -1, why test case #12 shows it -1 ?

    this my code in c

    int* absolutePermutation(int n, int k, int* result_count) {
        
        int * pos = calloc(n, sizeof(int)) , i = 0;
        
        if(k == 0)
        {
            for( i = 0 ; i<n ; i++)
            {
                pos[i] = i+1;   
            }
            *result_count = n;
        }
        else if( ( (n%2 == 0) && (n%k == 0) && (n != k)))
        {
            for(i = 0 ; i<n ; i++)
            {
                if ( (i/k)%2 == 0 ) {
                    pos[i] = (i+1+k);          
                }
                else
                {
                    pos[i] = (i+1-k);
                }
            }   
            
            *result_count = n;
        }
        else {
            pos = realloc(pos, sizeof(int));
            pos[0] = -1;
            * result_count = 1;
        }
        
        return pos;
    }