Programming Articles - Page 1028 of 3363

Python program to find list of triplets for which i+j+k is not same as n

Arnab Chakraborty
Updated on 06-Oct-2021 13:28:34

224 Views

Suppose we have three numbers i, j and k and another number n. We shall have to find the list of all triplets (i, j, k) for which i+j+k not same as n. We shall have to solve this problem using list comprehension strategy.So, if the input is like i = 1, j = 1, z = 2 and n = 3, then the output will be [[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 2]]To solve this, we will follow these ... Read More

Program to minimize hamming distance after swap operations in Python

Arnab Chakraborty
Updated on 06-Oct-2021 13:21:07

334 Views

Suppose we have two integer arrays, src and tgt, both are of same length. We also have an array allowedSwaps where allowedSwaps[i] contains a pair (ai, bi) indicates that we can swap the elements at index ai with element index bi of the array src. (We can swap elements at a specific pair of indices as many times as we want in any order). As we know the Hamming distance of two arrays of the same length, src and tgt, is the number of positions where the elements are different. We have to find the minimum Hamming distance of src ... Read More

Python program to check a number n is weird or not

Arnab Chakraborty
Updated on 06-Oct-2021 13:25:28

1K+ Views

Suppose we have a number n. We shall have to check whether n is weird or not. Here a number is weird when βˆ’ 1. The number is odd 2. The number is not in range 2 to 5 3. The number is even and in range 6 to 20So, if the input is like n = 18, then the output will be Weird because it is even and in range 6 to 20.To solve this, we will follow these steps βˆ’if n is odd, thenreturn "Weird"otherwise when (n > 1 and n < 6) or n > 20, thenreturn ... Read More

Program to find kpr sum for all queries for a given list of numbers in Python

Arnab Chakraborty
Updated on 06-Oct-2021 13:32:02

339 Views

Suppose we have a list of numbers nums. We also have a list of queries where queries[i] contains three elements [k, p, r], for each query we shall have to find kpr_sum. The formula for kpr_sum is like below.$$\mathrm{{π‘˜π‘π‘Ÿ}\_{π‘ π‘’π‘š} =\sum_{\substack{𝑖=𝑃}}^{π‘…βˆ’1}\sum_{\substack{𝑗=𝑖+1}}^{𝑅}(𝐾 βŠ•(𝐴[𝑖]βŠ•π΄[𝑗]))}$$If the sum is too large, then return sum modulo 10^9+7.So, if the input is like nums = [1, 2, 3] queries = [[1, 1, 3], [2, 1, 3]], then the output will be [5, 4] because for the first element it is (1 XOR (1 XOR 2)) + (1 XOR (1 XOR 3)) + (1 XOR (2 XOR 3)) ... Read More

Program to swap nodes in a linked list in Python

Arnab Chakraborty
Updated on 06-Oct-2021 13:14:20

1K+ Views

Suppose we have a list L and another value k. We have to swap kth node from start and kth node from end and return the final list at end.So, if the input is like L = [1, 5, 6, 7, 1, 6, 3, 9, 12] k = 3, then the output will be [1, 5, 3, 7, 1, 6, 6, 9, 12], the 3rd node from start is 6 and from end is 3, so they are swapped.To solve this, we will follow these steps βˆ’temp := Lfor i in range 0 to k-2, dotemp := next of tempfirstNode ... Read More

Program to find out if k monitoring stations are enough to monitor particular points in Python

Arnab Chakraborty
Updated on 06-Oct-2021 13:08:28

153 Views

Suppose there is a sensor module that can monitor its nearby environment up to a radius of r. There are some things in the lattice point of the module's monitoring circle that needs to be monitored. So, k number of low-powered modules are placed so that they can monitor only those specific points. Given the square of the radius and k number of low-powered modules, we shall have to find out if the points can be monitored correctly. We return true if monitoring is possible, otherwise, we return false.So, if the input is like square of radius (j) = 4, ... Read More

Program to find maximum weighted sum for rotated array in Python

Arnab Chakraborty
Updated on 06-Oct-2021 13:05:51

246 Views

Suppose we have an array of few elements. We shall have to find the maximum weighted sum if the array elements are rotated. The weighted sum of an array nums can be calculated like below βˆ’$$\mathrm{𝑆=\sum_{\substack{𝑖=1}}^{n}π‘–βˆ—π‘›π‘’π‘šπ‘ [𝑖]}$$So, if the input is like L = [5, 3, 4], then the output will be 26 becausearray is [5, 3, 4], sum is 5 + 2*3 + 3*4 = 5 + 6 + 12 = 23array is [3, 4, 5], sum is 3 + 2*4 + 3*5 = 3 + 8 + 15 = 26 (maximum)array is [4, 5, 3], sum is 4 + ... Read More

Program to count average of all special values for all permutations of a list of items in Python

Arnab Chakraborty
Updated on 06-Oct-2021 13:03:17

152 Views

Suppose we have a list of elements we can calculate the value of S by the following algorithm.while size of L > 1 is non-zero, do Β  Β a := L[0] Β  Β b := L[1] Β  Β remove L[1] Β  Β L[0] := a + b + a*b return L[0] mod (10^9 + 7)Here we shall have to find the average of all S values that are calculated from all possible combinations of L.So, if the input is like L = [5, 3, 4], then the output will be 199, because for all permutation of L, the value of S is 119, so ... Read More

Program to construct the lexicographically largest valid sequence in Python

Arnab Chakraborty
Updated on 06-Oct-2021 13:07:41

335 Views

Suppose we have a number n, we have to find a sequence that satisfies all of the following rules βˆ’1 occurs once in the sequence.Each number in between 2 and n occurs twice in the sequence.For every i in range 2 to n, the distance between the two occurrences of i is exactly i.The distance between two numbers on the sequence, a[i] and a[j], is |j - i|. We have to find the lexicographically largest sequence.So, if the input is like n = 4, then the output will be [4, 2, 3, 2, 4, 3, 1]To solve this, we will ... Read More

Program to find out the length between two cities in shortcuts in Python

Arnab Chakraborty
Updated on 06-Oct-2021 12:59:26

510 Views

Suppose there are n number of cities and the cities are connected with two types of roads; highways and shortcuts. Now, there is a map and only the highways are present on the map and all the shortcuts are absent. The transport division of the cities wants to launch a transport that connects the cities utilizing the highways and the shortcuts. We know there is a shortcut between the two cities when there is no highway between them. Our task here is to find the minimum distances in terms of shortcuts from a starting city to all other cities.So, if ... Read More

Advertisements