Programming Articles - Page 1979 of 3366

Largest Values From Labels in Python

Arnab Chakraborty
Updated on 30-Apr-2020 12:32:56

201 Views

Suppose we have a set of items: the i-th item has value values[i] and label labels[i]. Then, we will take a subset S of these items, such that −|S|

Letter Tile Possibilities in Python

Arnab Chakraborty
Updated on 30-Apr-2020 12:24:26

606 Views

Suppose we have a set of tiles, where each tile has one letter tiles[i] printed on it. Find the number of possible non-empty sequences of letters that we can make. So if the input is “AAB”, then the output will be 8. As sequences are “A”, “B”, “AA”, “AB”, “BA”, “AAB”, “ABA”, “BAA”To solve this, we will follow these steps −Define one dfs(), that will take countsum := 0for i in range 1 to 26if count[i] = 0, then go for next iteration, without checking the restdecrease count[i] by 1, and increase sum by 1sum := sum + dfs(count)increase count[i] ... Read More

Flip Columns For Maximum Number of Equal Rows in Python

Arnab Chakraborty
Updated on 30-Apr-2020 12:20:01

267 Views

Suppose we have a matrix consisting of 0s and 1s, we can choose any number of columns in the matrix and flip every cell in that column. converting a cell changes the value of that cell from 0 to 1 or from 1 to 0. we have to find the maximum number of rows that have all values equal after some number of flips. So if the matrix is like −000001110The output will be 2. This is because after converting values in the first two columns, the last two rows have equal values.To solve this, we will follow these steps ... Read More

Distant Barcodes in Python

Arnab Chakraborty
Updated on 30-Apr-2020 12:16:13

174 Views

Suppose in a warehouse, there is a row of barcodes. The i-th barcode is barcodes[i]. We have to rearrange the barcodes so that no two adjacent barcodes are same. So if the input is [1, 1, 1, 2, 2, 2] so output is [2, 1, 2, 1, 2, 1].To solve this, we will follow these steps −make one map named dstore the frequency of numbers present in the barcode array into dx := empty listinsert all key-value pairs into xi := 0res := make a list whose length is same as barcodes, and fill [0]sort x based on the frequencywhile ... Read More

Previous Permutation With One Swap in Python

Arnab Chakraborty
Updated on 30-Apr-2020 12:09:47

434 Views

Suppose we have an array A of positive integers (not necessarily unique), we have to find the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]). If it cannot possible, then return the same array. So if the array is like [3, 2, 1], then the output will be [3, 1, 2], by swapping 2 and 1To solve this, we will follow these steps −n := size of Afor left in range n – 2 down to -1if left = -1, then return ... Read More

Partition Array for Maximum Sum in Python

Arnab Chakraborty
Updated on 30-Apr-2020 12:06:21

1K+ Views

Suppose we have an integer array A, we have to partition the array into (contiguous) subarrays of length at most K. After partitioning, each subarray has their values changed to become the maximum value of that subarray. We have to find the largest sum of the given array after partitioning. So if input is like [1, 15, 7, 9, 2, 5, 10] and k = 3, then the output will be 84. This is because the array becomes [15, 15, 15, 9, 10, 10, 10]To solve this, we will follow these steps −make one array dp of length same as ... Read More

How to implement a Set interface in JShell in Java 9?

raja
Updated on 30-Apr-2020 11:44:59

193 Views

JShell is a command-line tool in Java 9 that has been used to execute simple statements like expressions, classes, interfaces, methods, and etc.A Set is an interface in Java that specifies a contract for collections having unique elements. If object1.equals(object2) returns true, then only one of object1 and object2 have a place in Set implementation.In the below code snippet, we have to use the Set.of() method. The collection returned by the Set.of() method is immutable, so it doesn't support the add() method. If we trying to add an element, throws UnsupportedOperationException. If we want to create a HashSet collection instead, which supports the ... Read More

Differences between Jdeps and Jdeprscan tools in Java 9?

raja
Updated on 30-Apr-2020 08:23:25

635 Views

Jdeps tool can be used to analyze the dependencies of our classes. The running of the "jdeps -jdkinternals jararchive.jar" command prints a list of all classes that use Java internal API. Jdeps tool returns a detailed description of the dependencies while Jdeprscan is another useful tool particularly used in combination with the "-for-removal" flag. This tool shows us all uses of deprecated API by a given jar archive, and only deprecated uses of jdk methods can be shown and can't use this tool to check for deprecation in third party jar.Jdeps tool:"jdeps" is a class dependency analyzer tool can be used for package level ... Read More

Arithmetic Slices in C++

Arnab Chakraborty
Updated on 30-Apr-2020 08:07:41

266 Views

Suppose we have a sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. So for example, these are arithmetic sequence: [1, 3, 5, 7, 9], [7, 7, 7, 7], [3, -1, -5, -9], But the following sequence is not arithmetic. [1, 1, 2, 5, 7]Now a zero-indexed array A consisting of N numbers is given. A slice of that given array is any pair of integers (P, Q) such that 0

Smallest Integer Divisible by K in Python

Arnab Chakraborty
Updated on 30-Apr-2020 07:52:31

355 Views

Suppose we have a positive integer K, we need find the smallest positive integer N such that N is divisible by K, and N only contains the digit 1. We have to find the length of N. If there is no such N, return -1. So if the input is like 3, then the output will be 3. The smallest answer will be N = 111.To solve this, we will follow these steps −if k is even, or k is divisible by 5, then return -1set r := 0 and N = 1for i in range 1 to K + ... Read More

Advertisements