Digit Count in Range

Arnab Chakraborty
Updated on 11-Jul-2020 12:03:48

1K+ Views

Suppose we have an integer d between 0 and 9, we also have two positive integers low and high as lower and upper bounds, respectively. We have to find the number of times that d occurs as a digit in all integers between low and high, including the bounds low and high.So, if the input is like d = 1, low = 1, high = 13, then the output will be 6, as digit d=1 occurs 6 times like 1, 10, 11, 12, 13.To solve this, we will follow these steps −Define a function zero(), this will take n, ret ... Read More

Interchange First and Last Elements in a List using Python

Pavitra
Updated on 11-Jul-2020 12:01:53

745 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a list, we need to swap the last element with the first element.There are 4 approaches to solve the problem as discussed below−Approach 1 − The brute-force approachExample Live Demodef swapLast(List):    size = len(List)    # Swap operation    temp = List[0]    List[0] = List[size - 1]    List[size - 1] = temp    return List # Driver code List = ['t', 'u', 't', 'o', 'r', 'i', 'a', 'l'] print(swapLast(List))Output['t', 'u', 't', 'o', 'r', 'i', 'a', 'l']Approach 2 − The ... Read More

Number of Valid Subarrays in C++

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

471 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

846 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

556 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

473 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

Advertisements