2 Keys Keyboard in C++

Arnab Chakraborty
Updated on 04-May-2020 13:17:10

235 Views

Suppose we have only one character 'A' in a text editor. We can perform two operations on this letter for each step −Copy All − We can copy all the characters present on the notepadPaste − We can paste the characters which are copied last time.Now suppose we have a number n. We have to get exactly n 'A' on the notepad by performing the minimum number of steps permitted. We have to find the result in the minimum number of steps to get n 'A'. So if the given n is 3, then answer will be 3, so initially ... Read More

Valid Triangle Number in C++

Arnab Chakraborty
Updated on 04-May-2020 13:14:50

611 Views

Suppose we have an array consists of non-negative integers, our task is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle. So if the input is like [2, 2, 3, 4], then the result will be 3 as [2, 3, 4] using first 2, [2, 3, 4] using second 2, and [2, 2, 3].To solve this, we will follow these steps −ret := 0, n := size of nums, sort numsfor i in range n – 1 down to 0right := i – 1, left ... Read More

Array Nesting in C++

Arnab Chakraborty
Updated on 04-May-2020 13:12:17

553 Views

Suppose we have a zero-indexed array A of length N that contains all integers from 0 to N-1. We have to find and return the longest length of set S, where S[i] = {A[i], A[A[i]], A[A[A[i]]], ... } subjected to the rule below. Now consider the first element in S starts with the selection of element A[i] of index = i, the next element in S should be A[A[i]], and then A[A[A[i]]]… By that analogy, we stop adding right before a duplicate element occurs in S. So if the array is like A = [5, 4, 0, 3, 1, 6, ... Read More

Difference of Two Lists Including Duplicates in Python

Pradeep Elance
Updated on 04-May-2020 13:11:55

305 Views

Sometimes we need to find the differences between two lists. It will also mean a mathematical subtraction in which the elements from the first list are removed if they are present in the second list. Duplicates are preserved. Below is the approach through which we can achieve this.We can use the Counter method from the collections module which will keep track of the count of elements. A straight mathematical subtraction gives the desired result. In the final result the number of occurrences of an element between the first and the second list will decide the elements.Example Live Demofrom collections import Counter ... Read More

Dictionary with Index as Value in Python

Pradeep Elance
Updated on 04-May-2020 13:10:53

848 Views

In this article we will learn how to create a dictionary from another frequently used python collection namely list. An index or key is not part a list content. But in dictionary we need to have a key or index attached to every element which is referred as value.Using enumerateThe enumerate function adds a counter as the key of the enumerate object. SO we apply it to a given list and using a for loop. That creates the required dictionary where the keys are generated by the enumerate function.Example Live DemoAlist = ['Mon', 'Tue', 'Wed', 'Wed', 11, 11] # Given ... Read More

Next Greater Element III in C++

Arnab Chakraborty
Updated on 04-May-2020 13:09:59

271 Views

Suppose we have a positive 32-bit integer n, we need to find the smallest 32-bit integer which has exactly the same digits existing in the integer n and is greater in value than n. If we have no such positive 32-bit integer number, then return -1.So if the number is 213, then the result will be 231.To solve this, we will follow these steps −s := n as string, sz := size of s, ok := falsefor i in range sz – 2 to 0if s[i] < s[i + 1], then ok := true and break the loopif of is ... Read More

Dictionary to List of Tuple Conversion in Python

Pradeep Elance
Updated on 04-May-2020 13:09:44

491 Views

ging the collection type from one type to another is a very frequent need in python. In this article we will see how we create a tuple from the key value pairs that are present in a dictionary. Each of the key value pair becomes a tuple. So the final list is a list whose elements are tuples.With items()We sue the items method of the dictionary which allows us to iterate through each of the key value pairs. Then we use a for loop to pack those values in to a tuple. We put all these tuples to a final ... Read More

Dictionary Creation Using List Contents in Python

Pradeep Elance
Updated on 04-May-2020 13:08:06

189 Views

Changing the collection type from one type to another is a very frequent need in python. In this article we will see how we create a dictionary when multiple lists are given. The challenge is to be able to combine all these lists to create one dictionary accommodating all these values in a dictionary key value format.With zipThe zip function can be used to combine the values of different lists as shown below. In the below example we have taken three lists as input and combine them to form a single dictionary. One of the list supplies the keys for ... Read More

Optimal Division in C++

Arnab Chakraborty
Updated on 04-May-2020 13:06:53

330 Views

Suppose we have a list of positive integers; the adjacent integers will perform the float division. So for example, [2, 3, 4] -> 2 / 3 / 4. Now, we can add any number of parenthesis at any position to change the priority of these operations. We should find out how to add parenthesis to get the maximum result, we have to find the corresponding expression in string format. Our expression should NOT contain redundant parenthesis. So if the input is like [1000, 100, 10, 2], then the result will be “1000/(100/10/2)”.To solve this, we will follow these steps −n ... Read More

Delete Items from Dictionary While Iterating in Python

Pradeep Elance
Updated on 04-May-2020 13:06:48

510 Views

A python dictionary is a collection which is unordered, changeable and indexed. They have keys and values and each item is referred using the key. In this article we will explore the ways to delete the items form a dictionary.Using del with keysIn this approach we capture the key values that are needed to be deleted. Once we apply the del function, the key value pairs for those keys get deleted.Example Live Demo# Given dictionary ADict = {1: 'Mon', 2: 'Tue', 3: 'Wed', 4:'Thu', 5:'Fri'} # Get keys with value in 2, 3. to_del = [key for key in ADict ... Read More

Advertisements