Server Side Programming Articles - Page 1710 of 2651

K Empty Slots in C++

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

391 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

245 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

Coin Path in C++

Arnab Chakraborty
Updated on 11-Jul-2020 11:40:21

265 Views

Suppose we have an array A (index starts at 1) with N numbers: A1, A2, ..., AN and another integer B. The integer B denotes that from any index is i in the array A, we can jump to any one of the places in the array A indexed i+1, i+2, …, i+B if this place can be jumped to. Also, if we step on the index i, we have to pay Ai amount of coins. If Ai is -1, it means we can’t jump to the place indexed i in the array.Now, when we start from the place indexed ... Read More

Maximum Average Subarray II in C++

Arnab Chakraborty
Updated on 11-Jul-2020 11:36:37

927 Views

Suppose we have an array with n integers, we have to find the contiguous subarray whose length is greater than or equal to k that has the maximum average value. We have to find the maximum average value.So, if the input is like [1, 12, -5, -6, 50, 3], k = 4, then the output will be 12.75, as when length is 5, maximum average value is 10.8, when length is 6, maximum average value is 9.16667. Thus output is 12.75.To solve this, we will follow these steps −Define a function ok(), this will take x, an array nums, k, ... Read More

Maximum Vacation Days in C++

Arnab Chakraborty
Updated on 11-Jul-2020 11:33:30

436 Views

Suppose one company wants to give one of its best employees the option to travel among N cities to collect some resources. But employees want some vacations also, we could take vacations in some particular cities and weeks. Our task is to schedule the traveling to maximize the number of vacation days we could take, but there are certain rules and restrictions we have to follow.We can only travel among N cities; they are represented by indexes from 0 to N-1. Firstly, we are in the city indexed 0 on Monday.These cities are connected by flights. We have one N ... Read More

Word Abbreviation in C++

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

1K+ Views

Suppose we have an array of n unique strings, We have to generate minimal possible abbreviations for every word following rules below.Starting with the first character and then the number of characters abbreviated, which followed by the last character.If we find any conflict and that is more than one words share the same abbreviation, a longer prefix can be used instead of only the first character until making the map from word to abbreviation become unique.When the abbreviation doesn't make the word shorter, then keep it as original.So, if the input is like ["like", "god", "internal", "me", "internet", "interval", "intension", ... Read More

trunc() in Python

Hafeezul Kareem
Updated on 11-Jul-2020 08:22:56

304 Views

In this tutorial, we are going to learn about the math.trunc() method.The method math.trunc() is used to truncate the float values. It will act as math.floor() method for positive values and math.ceil() method for negative values.Example Live Demo# importing math module import math # floor value print(math.floor(3.5)) # trunc for positive number print(math.trunc(3.5))OutputIf you run the above code, then you will get the similar result as follows.3 3Example Live Demo# importing math module import math # ceil value print(math.ceil(-3.5)) # trunc for negative number print(math.trunc(-3.5))OutputIf you run the above code, then you will get the similar result as follows.-3 -3ConclusionIf you have ... Read More

time.process_time() function in Python

Hafeezul Kareem
Updated on 11-Jul-2020 08:21:15

2K+ Views

In this tutorial, we are going to learn about the time.process_time() method.The method time.process_time() will return a float value of time in seconds of a system and user CPU time of the current process.Example Live Demo# importing the time module import time # printing the current process time print(time.process_time()) 1.171875OutputIf you run the above code, then you will get the similar result as follows.1.171875Let's say we have a process that prints from in the given range. Let's find the time for that process. Understand the following code and run it.Example Live Demo# importing the time module import time # program to find ... Read More

time.perf_counter() function in Python

Hafeezul Kareem
Updated on 11-Jul-2020 08:19:13

4K+ Views

In this tutorial, we are going to learn about the time.perf_counter() method.The method time.perf_counter() returns a float value of time in seconds. Let's see anExample Live Demo# importing the time module import time # printing the time print(time.perf_counter())OutputIf you run the above code, then you will get the following result.263.3530349We can use the time.perf_counter() method to find the execution time of a program. Let's see an example.Example Live Demo# importing the time module import time # program to find the prime number def is_prime(number):    for i in range(2, number):       if number % i == 0:       ... Read More

The most occurring number in a string using Regex in python

Hafeezul Kareem
Updated on 11-Jul-2020 08:18:27

234 Views

In this tutorial, we are going to write a regex that finds the most occurring number in the string. We will check the regex in Python.Follow the below steps to write the program.Import the re and collections modules.Initialize the string with numbers.4Find all the numbers using regex and store them in the array.Find the most occurring number using Counter from collections module.Example Live Demo# importing the modules import re import collections # initializing the string string = '1222tutorials321232point3442' # regex to find all the numbers regex = r'[0-9]' # getting all the numbers from the string numbers = re.findall(regex, string) # ... Read More

Advertisements