Number of Valid Subarrays in C++

Arnab Chakraborty
Updated on 11-Jul-2020 12:00:02

485 Views

Suppose we have an array A of integers, we have to find the number of non-empty continuous subarrays that satisfy this condition: The leftmost element of the subarray is not larger than other elements in the subarray.So, if the input is like [1, 4, 2, 5, 3], then the output will be 11, as there are 11 valid subarrays, they are like [1], [4], [2], [5], [3], [1, 4], [2, 5], [1, 4, 2], [2, 5, 3], [1, 4, 2, 5], [1, 4, 2, 5, 3].To solve this, we will follow these steps −ret := 0n := size of numsDefine ... Read More

Minimize Maximum Distance to Gas Station in C++

Arnab Chakraborty
Updated on 11-Jul-2020 11:58:12

1K+ Views

Suppose we have one horizontal number line. On that number line, we have gas stations at positions stations[0], stations[1], ..., stations[N-1], where N = size of the stations array. Now, we add K more gas stations so that D, the maximum distance between adjacent gas stations, is minimized. We have to find the smallest possible value of D.So, if the input is like stations = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], K = 9, then the output will be 0.5To solve this, we will follow these steps −Define a function ok(), this will take x, array ... Read More

Rules for Local Variable in Lambda Expression in Java

raja
Updated on 11-Jul-2020 11:57:48

4K+ Views

A lambda expression can capture variables like local and anonymous classes. In other words, they have the same access to local variables of the enclosing scope. We need to follow some rules in case of local variables in lambda expressions.A lambda expression can't define any new scope as an anonymous inner class does, so we can't declare a local variable with the same which is already declared in the enclosing scope of a lambda expression.Inside lambda expression, we can't assign any value to some local variable declared outside the lambda expression. Because the local variables declared outside the lambda expression can be final or effectively final.The rule of ... Read More

Find the Sum of All Items in a Dictionary in Python

Pavitra
Updated on 11-Jul-2020 11:56:02

1K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a dictionary, and we need to print the 3 highest value in a dictionary.Three approaches to the problem statement are given below:Approach 1 − Calculating sum from the dictionary iterableExample Live Demo# sum function def Sum(myDict):    sum_ = 0    for i in myDict:       sum_ = sum_ + myDict[i]    return sum_ # Driver Function dict = {'T': 1, 'U':2, 'T':3, 'O':4, 'R':5} print("Sum of dictionary values :", Sum(dict))OutputSum of dictionary values : 14Approach 2 − Calculating the ... Read More

Basic Calculator III in C++

Arnab Chakraborty
Updated on 11-Jul-2020 11:55:18

869 Views

Suppose we have a simple expression string and we have to Implement a basic calculator to evaluate that expression. The expression string may contain opening and closing parentheses, the plus + or minus sign -, non-negative integers and empty spaces. The expression string contains only non-negative integers, +, -, *, / operators, opening and closing parentheses and empty spaces. The integer division should truncate toward zero.So, if the input is like "6-4 / 2", then the output will be 4To solve this, we will follow these steps −l1 := 0, l2 := 1o1 := 1, o2 := 1Define one stack ... Read More

Employee Free Time in C++

Arnab Chakraborty
Updated on 11-Jul-2020 11:52:29

586 Views

Suppose we have given a list of schedules of employees; this represents the working time for each employee. Now suppose each employee has a list of non-overlapping Intervals, these intervals are sorted. We have to find the list of finite intervals representing the common, positive-length free time for all employees, and that will also be in sorted order. We are representing Intervals in the form [x, y], For example, schedule [0][0].start = 1, schedule[0][0].end = 2.So, if the input is like schedule = [[[1, 2], [5, 6]], [[1, 3]], [[4, 10]]], then one of the output will be [[3, 4]].To ... Read More

Minimum Window Subsequence in C++

Arnab Chakraborty
Updated on 11-Jul-2020 11:49:46

1K+ Views

Suppose we have two strings S and T, we have to find the minimum substring W of S, so that T is a subsequence of W. If there is no such window in S that covers all characters in T, then return empty string. If there are multiple such windows, we have to return the one with the left-most starting index.So, if the input is like S = "abcdebdde", T = "bde", then the output will be "bcde" as it occurs before "bdde". "deb" is not a smaller window because the elements of T in the window must occur in ... Read More

Number of Distinct Islands II in C++

Arnab Chakraborty
Updated on 11-Jul-2020 11:47:19

497 Views

Suppose we have a non-empty 2D binary array called grid, here an island is a group of 1's (representing land) connected 4-directionally. We can also assume all four edges of the grid are surrounded by water.We have to count the number of distinct islands. An island is considered to be the same as another if they have the same shape, or have the same shape after rotation of 90, 180, or 270 degrees only or reflection of left/right direction or up/down direction.So, if the input is like, 11000100000000100011then the output will be 1To solve this, we will follow these steps ... Read More

K Empty Slots in C++

Arnab Chakraborty
Updated on 11-Jul-2020 11:44:22

428 Views

Suppose we have N bulbs in a row and they are numbered from 1 to N. At first, all the bulbs are off. We can turn on exactly one bulb everyday until all bulbs are on after N days. If we have an array bulbs of length N where bulbs[i] = x this indicates that on the (i+1)th day, we will turn on the bulb at position x. If we have another integer K, such that out the minimum day number such that there exists two turned on bulbs that have exactly K bulbs between them that are all off. ... Read More

Remove 9 in C++

Arnab Chakraborty
Updated on 11-Jul-2020 11:42:01

262 Views

Suppose we have an integer n, we have to return nth integer after following this operation: Start from integer 1, remove any integer that contains 9 such as 9, 19, 29... So now, we will have a new integer sequence like 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, ... We have to keep in mind that 1 will be the first integer.So, if the input is like 9, then the output will be 10To solve this, we will follow these steps −ret := 0s := 1while n is non-zero, do −ret := ret + (n mod 9) ... Read More

Advertisements