Server Side Programming Articles - Page 1792 of 2646

Bulb Switcher II in C++

Arnab Chakraborty
Updated on 04-May-2020 13:40:34

327 Views

Suppose there is a room with n lights which are switched on initially and 4 buttons present on the wall. After performing exactly m unknown operations towards buttons, we need to return how many different kinds of status of the n lights could be. So consider n lights are labeled as number [1, 2, 3 ..., n], function of these 4 buttons are as follows −Flip all the lights.Flip lights with even numbers.Flip lights with odd numbers.Flip lights with (3k + 1) numbers, k = 0, 1, 2, ...Now if n = 3 and m = 1, then there will ... Read More

Beautiful Arrangement II in C++

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

220 Views

Suppose we have two integers n and k, we need to construct a list that contains n different positive integers ranging from 1 to n and obeys the following rule −Consider the list is [a1, a2, a3, ... , an], then the list [|a1 - a2|, |a2 - a3|, |a3 - a4|, ... , |an-1 - an|] has exactly k unique integers. So if there are multiple answers, display any of them.If the input is like n = 3 and k = 2, then the result will be [1, 3, 2]. The [1, 3, 2] has three different positive integers ... Read More

Split Array into Consecutive Subsequences in C++

Arnab Chakraborty
Updated on 04-May-2020 13:34:44

496 Views

Suppose we have an array nums that is sorted in ascending order. We have to return true if and only if we can split it into 1 or more subsequences such that each subsequence consists of consecutive integers and whose length at least 3. So if the input is like [1, 2, 3, 3, 4, 4, 5, 5], then the output will be True, as we have two consecutive sequences. These are [1, 2, 3, 4, 5] and [3, 4, 5].To solve this, we will follow these steps −Make a map m and store the frequency of nums into m, ... Read More

Print Binary Tree in C++

Arnab Chakraborty
Updated on 04-May-2020 13:31:26

6K+ Views

Suppose we have to display a binary tree in an m*n 2D string array based on these rules −The row number m should be same as the height of the given binary tree.The column number n should be always an odd number.The value of the root node should be put in the exactly middle of the first row it can be put. The column and the row where the root node resides, will separate the rest space into two parts. These are left-bottom part and right-bottom part. We should print the left subtree in the left-bottom part and print the ... Read More

Find Duplicate Subtrees in C++

Arnab Chakraborty
Updated on 04-May-2020 13:25:29

166 Views

Suppose we have a binary tree. We have to find all duplicate subtrees. So for each kind of duplicate subtrees, we have to return the root node of any one of them. So suppose we have a tree like −The duplicate subtrees are −To solve this, we will follow these steps −Create an array ret, make a map mDefine a recursive method solve(). This will take node as input. This works as −if node is null, then return -1x := value of node as string, then concatenate “#” with it.left := solve(left of node), right := solve(right of node)x := ... Read More

Valid Triangle Number in C++

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

640 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

Difference of two lists including duplicates in Python

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

320 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

869 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

Array Nesting in C++

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

583 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

Dictionary to list of tuple conversion in Python

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

511 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

Advertisements