Stepping Numbers in C++

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

375 Views

Suppose we have two integers low and high, we have to find and show a sorted list of all the Stepping Numbers in the range [low, high] inclusive. A Stepping Number is an integer means all of its adjacent digits have an absolute difference of exactly 1. For example, 321 is a Stepping Number but 421 is not. So if the input is like low := 0 and high := 21, then the result will be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 21]To solve this, we will follow these steps −create one array tempmake ... Read More

Sort an Array in C++

Arnab Chakraborty
Updated on 30-Apr-2020 12:48:57

297 Views

Suppose we have an array of integers; we have to sort them in ascending order. So if the array is like [5, 2, 3, 1], then the result will be [1, 2, 3, 5]To solve this, we will follow these steps −Make one method called partition, this will take array, low and highset pivot := lowfor i in range low to high – 1if nums[i] < nums[high], then swap(nums[i] and nums[pivot]), increase pivot by 1swap nums[pivot] and nums[high]Define a method called sortArr(), this will take array, low and highif low >= high, then returnpartitionIndex := partition(nums, low, high)sortArr(nums, low, partitionIndex ... Read More

Remove All Adjacent Duplicates in String II in C++

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

709 Views

Suppose a string s is given, a k duplicate removal consists of choosing k adjacent and equal letters from string s and removing them causing the left and the right side of the deleted substring to concatenate together. We will repeatedly make k duplicate removals on the given string s until we cannot change any remaining. We have to find the final string after all such duplicate removals have been made. So if the input is like s = “deeedbbcccbdaa”, and k = 3, then the output will be “aa”, at first delete the “eee” and “ccc” and we will ... Read More

Online Election in C++

Arnab Chakraborty
Updated on 30-Apr-2020 12:44:30

620 Views

Suppose in an election, the i-th vote was cast for persons[i] at time times[i]. Now, we have to implement the following query function: TopVotedCandidate.q(int t) this will find the number of the person that was leading the election at time t. Votes cast at time t will count towards our query. If there is a tie, the most recent vote (among tied candidates) wins.So if we initialize this with TopVotedCandidate([0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]), then call q() like: q(3), q(12), q(25), q(15), q(24), q(8), then the result will be [0, 1, ... Read More

Stone Game II in C++

Arnab Chakraborty
Updated on 30-Apr-2020 12:44:15

463 Views

Suppose there are two persons Alice and Bob, they are continuing their games with piles of stones. There are a number of piles placed in a row, and each pile has a positive integer number of stones in an array piles[i]. Our objective of the game is to end with the most stones. Alice and Bob take the turns, with Alice starting first. Initially, M = 1. On each player's turn, that player can take all the stones in the first X remaining piles, here 1

Smallest Range II in C++

Arnab Chakraborty
Updated on 30-Apr-2020 12:40:40

274 Views

Suppose we have an array A of integers, for each integer A[i] we have to choose either x = -K or x = K, and add x to A[i] (only once). So after this process, we have some array B. We have to find the smallest possible difference between the maximum value of B and the minimum value of B. So if the input is A = [0, 10], K = 2, then the output will be 6, as B = [2, 8].To solve this, we will follow these steps −set ret := 0, n := size of array Asort ... Read More

Longest Well-Performing Interval in Python

Arnab Chakraborty
Updated on 30-Apr-2020 12:40:28

259 Views

Suppose we have hours list, this is a list of the number of hours worked per day for a given employee. Here a day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. One well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. We have to find the length of the longest well-performing interval. So if the input is like [9, 9, 6, 0, 6, 6, 9], so then then the output will be ... Read More

Corporate Flight Bookings in Python

Arnab Chakraborty
Updated on 30-Apr-2020 12:38:07

217 Views

Suppose we have n flights, and they are labeled from 1 to n. We have a list of flight bookings. The i-th booking indicates using bookings[i] = [i, j, k] this means that we booked k seats from flights labeled i to j inclusive. Find an array answer of length n, showing the number of seats booked on each flight in order of their label. So if the input is like [[1, 2, 10], [2, 3, 20], [2, 5, 25]] and n = 5, then the output will be [10, 55, 45, 25, 25].To solve this, we will follow these ... Read More

Car Pooling in Python

Arnab Chakraborty
Updated on 30-Apr-2020 12:35:10

852 Views

Suppose there is a vehicle that has capacity empty seats initially available for passengers. The vehicle only drives east, so we cannot turn around and drive west. We have given a list of trips, trip[i] = [num_passengers, start_location, end_location], that contains information about the ith trip:, so that is the number of passengers that must be picked up, and the locations to pick them up and drop them off. Here the locations are given as the number of kilometers due east from our vehicle's initial location. Our module will return true if and only if it is possible to pick ... Read More

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|

Advertisements