Saturday, 4 Dec 2021
Category: Natural Numbers

Given an array arr[] of size N, where arr[i] is natural numbers less than or equal to N, the task is to find all the numbers in the range [1, N] that are not present in the given array.Examples:Input: arr[ ] = {5, 5, 4, 4, 2}Output: 1 3Explanation: For all numbers in the range [1, […]

Given an array arr[] of length N and an integer K, the task is to find the maximize the length of the array by appending at most K elements such that the array becomes a permutation of consecutive numbers starting from 1. Once K elements are added, sum of two or more array elements can […]

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 =  * (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, […]

Count inversions in a permutation of first N natural numbersGiven an array, arr[] of size N denoting a permutation of numbers from 1 to N, the task is to count the number of inversions in the array. Note: Two array elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j.Examples:Input: arr[] […]

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 […]