C++ Articles - Page 419 of 719

Number of Substrings Containing All Three Characters in C++

Arnab Chakraborty
Updated on 29-Apr-2020 12:53:21

703 Views

Suppose we have given a string s consisting only of characters a, b and c. We have to return the number of substrings containing at least one occurrence of all these characters a, b and c. So for example, if the string is “abcabc”, then the output will be 10, as the substring containing at least one occurrence of the characters a, b and c, these are “abc”, “abca”, “abcab”, “abcabc”, “bca”, “bcab”, “cab”, “cabc” and “abc” (again for the last part).To solve this, we will follow these steps −ret := 0, make a map called m, set j := ... Read More

Apply Discount Every n Orders in C++

Arnab Chakraborty
Updated on 29-Apr-2020 12:49:55

343 Views

Suppose there is a sale in a supermarket, there will be a discount every n customer. Consider there are some products in the supermarket where the id of the i-th product is products[i] and the price per unit of this product is prices[i]. Here the system will count the number of customers and when the n-th customer arrives he/she will have a discount on the bill. Then the system will start counting customers again. The customer orders a certain amount of each product where product[i] is the id of the i-th product the customer ordered and amount[i] is the number ... Read More

Maximum Number of Events That Can Be Attended in C++

Arnab Chakraborty
Updated on 29-Apr-2020 12:45:51

864 Views

Suppose we have an array of events where events[i] = [startDayi, endDayi]. Here every event I start at startDayi and ends at endDayi. We can attend an event I at any day d where d in range startTimei and endTimei (both inclusive). We have to keep in mind that we can only attend one event at any time. So find the maximum number of events we can attend. So for example, if the input is like [[1, 4], [4, 4], [2, 2], [3, 4], [1, 1]], then the output will be 1, as we can attend maximum of four events, ... Read More

Product of the Last K Numbers in C++

Arnab Chakraborty
Updated on 29-Apr-2020 12:42:02

364 Views

Suppose we have the implement the class called ProductOfNumbers that supports two methods −add(int num): This adds the number num to the back of the current list of numbers.getProduct(int k): This returns the product of the last k numbers in the current list.We can assume that always the current list has at least k numbers. So for example, if the input is like − add(3), add(0), add(2), add(5), add(4), getProduct(2), getProduct(3), getProduct(4), add(8), getProduct(2), then the output will be (after each function call) −[3], [3, 0], [3, 0, 2], [3, 0, 2, 5], [3, 0, 2, 5, 4], then (5 ... Read More

Minimum Number of Steps to Make Two Strings Anagram in C++

Arnab Chakraborty
Updated on 29-Apr-2020 12:37:48

282 Views

Suppose we have two equal-size strings s and t. In one step we can choose any character of t and replace it with another character. We have to find the minimum number of steps required to make t an anagram of s. Note: An Anagram of a string is a string that contains the same characters with a different (or the same) ordering.So if the input is like - “yxy” and “xyx”, then the output will be 1, as only one character is needed to be replaced.To solve this, we will follow these steps −n := size of characters in ... Read More

Angle Between Hands of a Clock in C++

Ravi Ranjan
Updated on 15-Jul-2025 18:27:16

2K+ Views

In this article, we have values of hours and minutes respectively. Our task is to find a smaller angle formed between the hour and the minute hand. The formula for calculating the angle between them is: angle = |30*h - 5.5min|. Here are some examples given below: Scenario 1 Input: Time = 12:45 Output: 112.5 Explanation: Here, hour = 12, minute = 45 angle = |30*h - 5.5min| = |30*12 - 5.5*45| = |360 - 247| = 112.5 Scenario 2 Input: Time = 10:30 Output: 135 Explanation: Here, hour = 10, minute = 30 ... Read More

Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold in C++

Arnab Chakraborty
Updated on 29-Apr-2020 12:26:21

752 Views

Suppose we have an array of integers arr and two integers k and threshold. We have to find the number of sub-arrays of size k and average greater than or equal to the threshold. So if the input is like: [2, 2, 2, 2, 5, 5, 5, 8] and k = 3 and threshold = 4, then the output will be 3. Because the subarrays [2, 5, 5], [5, 5, 5] and [5, 5, 8] has the averages 4, 5 and 6 respectively.To solve this, we will follow these steps −sum := 0, div := k and n := number ... Read More

Reduce Array Size to The Half in C++

Arnab Chakraborty
Updated on 29-Apr-2020 12:29:34

1K+ Views

Suppose we have an array arr. We can choose a set of integers and remove all the occurrences of these integers in the array. We have to find the minimum size of the set so that at least half of the integers of the array are removed. So for example, if arr = [3, 3, 3, 3, 5, 5, 5, 2, 2, 7], then the output will be 2. This is because if we choose {3, 7} this will make the new array [5, 5, 5, 2, 2] which has size 5 (this is equal to half of the size ... Read More

Find the City With the Smallest Number of Neighbors at a Threshold Distance in C++

Arnab Chakraborty
Updated on 29-Apr-2020 12:19:58

522 Views

Suppose there are n cities numbered from 0 to n-1. If we have the array edges where edges[i] = [fromi, toi, weighti] represents a bidirectional and weighted edge between cities fromi and toi, and given the integer distance threshold. We have to find the city with the smallest number of cities that are reachable through some path and whose distance is at most distance threshold, If there are more than one such cities, return the city with the greatest number.So if the input is like −n is 4 and the distance threshold is also 4, then the output will be ... Read More

Filter Restaurants by Vegan-Friendly, Price and Distance in C++

Arnab Chakraborty
Updated on 29-Apr-2020 12:15:53

153 Views

Suppose we have an array of restaurants where restaurants[i] have [idi, ratingi, vegan friendly, pricei, distancei]. We have to filter the restaurants using three filters.The vegan-friendly filter will be either true (meaning we should only include restaurants with vegan-friendly set to true) or false (meaning we can include any restaurant).The maxPrice filter and max distance filter which are the maximum value for price and distance of restaurants we should consider respectively.We have to find the array of restaurant IDs after filtering, ordered by rating from highest to lowest. For restaurants with the same rating, order them by id in decreasing ... Read More

Advertisements