Server Side Programming Articles - Page 1850 of 2650

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

520 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

Sort the Matrix Diagonally in C++

Arnab Chakraborty
Updated on 29-Apr-2020 11:06:15

371 Views

Suppose we have N x M matrix, we have to sort this diagonally in increasing order from top-left to the bottom right. So if the matrix is like −331122121112The output matrix will be −111112221233To solve this, we will follow these steps −Define a method called solve(), this will take si, sj and matrix matn := number of rows and m := number of columnsmake an array called tempi:= si and j := sj, and index := 0while i < n and j < minsert m[i, j] into temp, then increase i and j by 1sort temp arrayset index := 0, ... Read More

Break a Palindrome in C++

Ravi Ranjan
Updated on 17-Jun-2025 17:57:32

2K+ Views

In this article, our task is to break a palindromic string. We have to replace exactly one character with any lowercase English letter such that the string becomes a lexicographically smallest possible string that isn't a palindrome. Now after doing so, we have to find the final string. If there is no way to do so, then return the empty string. Example Here is an example of breaking the given palindrome string by replacing one letter and the output is a non-palindromic lexicographically smallest string: Input: abccba Output: aaccba In the above ... Read More

Delete Leaves With a Given Value in C++

Arnab Chakraborty
Updated on 29-Apr-2020 10:24:59

162 Views

Suppose we have a binary tree and an integer target, we have to delete all the leaf nodes with value target. We have to keep in mind that once we delete a leaf node with a value target if it's parent node becomes a leaf node and has the value target, it should also be deleted (we need to continue doing that until we can't). So if the tree is like below, and the target is 2, then the final tree will be like the last one −To solve this, we will follow these steps −Define a recursive method called ... Read More

Print Words Vertically in Python

Arnab Chakraborty
Updated on 29-Apr-2020 09:50:19

5K+ Views

Suppose we have a string s. We have to find all the words vertically in the same order in which they appear in s. Here words are returned as a list of strings, we have to complete with spaces when is necessary. (Trailing spaces are not allowed). Each word would be put on only one column and that in one column there will be only one word. So if the input string is “HOW ARE YOU”, then the output will be [“HAY”, “ORO”, “WEU”]To solve this, we will follow these steps −s := make a list of strings split by ... Read More

Delete elements in C++ STL list

Ayush Gupta
Updated on 23-Mar-2020 08:48:31

241 Views

IIn this tutorial, we will be discussing a program to understand how to delete elements in the C++ STL list.For this, we will be using the pop_back() and pop_front() function to delete the element from last and the front respectively.Example Live Demo#include #include using namespace std; int main(){    listlist1={10,15,20,25,30,35};    cout

Advertisements