Saturday, 4 Dec 2021
Category: Natural Numbers

Print all numbers that are divisors of N and are co-prime with the quotient of their divisionGiven a positive integer N, the task is to print all the numbers, say K, such that K is a divisor of N and K and N / K are coprime.Examples:Input: N = 12  Output: 1 3 4 12 […]

Generate a permutation of first N natural numbers from an array of differences between adjacent elementsGiven an array arr[] consisting of (N – 1), the task is to construct a permutation array P[] consisting of the first N Natural Numbers such that arr[i] = (P[i +1] – P[i]). If there exists no such permutation, then […]

Minimize sum of numbers required to convert an array into a permutation of first N natural numbersGiven an array A[] of size N, the task is to find the minimum sum of numbers required to be added to array elements to convert the array into a permutation of 1 to N. If the array can not […]

def Swap(x, y, p, posOfCurrNum):         posOfCurrNum[p[x]], posOfCurrNum[p[y]] = posOfCurrNum[p[y]], posOfCurrNum[p[x]]    p[x], p[y] = p[y], p[x]    return p, posOfCurrNumdef sortArray(p, n):                 posOfCurrNum = [0] * (n + 1)    for i in range(1, n + 1):        posOfCurrNum[p[i]] = i        ans = []        for i in range(1, n + 1):                                 if (p[i] == i):            continue                        j = posOfCurrNum[i]                        if (abs(i – j) * 2 >= n):            p, posOfCurrNum = Swap(i, j, […]

Generate a permutation of first N natural numbers having count of unique adjacent differences equal to KGiven two positive integers N and K, the task is to construct a permutation of the first N natural numbers such that all possible absolute differences between adjacent elements is K.Examples:Input: N = 3, K = 1Output: 1 2 […]

Minimum number of operations required to make a permutation of first N natural numbers equalGiven an array A[] of size N, containing a permutation of first N natural numbers and an integer K, the task is to find the minimum number of operations required to make all array elements equal by selecting K (1 < […]

Minimum operations required to make all elements in an array of first N odd numbers equalGiven an array consisting of first N odd numbers, the task is to find the minimum number of operations required to make all the array elements equal by repeatedly selecting a pair and incrementing one element and decrementing the other […]