Found 33676 Articles for Programming

Number of Distinct Islands II in C++

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

469 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

K Empty Slots in C++

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

388 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

263 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

926 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

435 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

Advertisements