Programming Articles - Page 1024 of 3363

Program to find minimum cost to hire k workers in Python

Arnab Chakraborty
Updated on 07-Oct-2021 09:27:16

378 Views

Suppose we have an array called quality for each different worker, and have another array called wages and a value K. The i-th worker has a quality[i] and a minimum wage expectation wage[i]. We want to hire K workers to form a paid group. When we are hiring a group of K workers, we must pay them according to the following rules:Each worker in the paid group should be paid in the ratio of their quality by comparing with others in the paid group.Every worker in the paid group must be paid at least their minimum wage expectation.We have to ... Read More

Program to check a string can be split into three palindromes or not in Python

Arnab Chakraborty
Updated on 07-Oct-2021 09:22:02

617 Views

Suppose we have a string s. We have to check whether we can split s into three palindromic substring or not.So, if the input is like s = "levelpopracecar", then the output will be True because we can split it like "level", "pop", "racecar", all are palindromes.To solve this, we will follow these steps −n := size of sdp := a matrix of order n x n and fill with falsefor i in range n-1 to 0, decrease by 1, dofor j in range 0 to n - 1, doif i >= j, thendp[i, j] := Trueotherwise when s[i] is ... Read More

Program to find minimum cost to merge stones in Python

Arnab Chakraborty
Updated on 07-Oct-2021 09:18:18

310 Views

Suppose we have N piles of stones arranged in a row. Here the i-th pile has stones[i] number of stones. A move consists of merging K consecutive piles into one pile, now the cost of this move is equal to the total number of stones in these K number of piles. We have to find the minimum cost to merge all piles of stones into one pile. If there is no such solution then, return -1.So, if the input is like nums = [3, 2, 4, 1], K = 2, then the output will be 20, because, initially have [3, ... Read More

Program to find number of squareful arrays in Python

Arnab Chakraborty
Updated on 07-Oct-2021 09:07:41

161 Views

Suppose we want to make a target string of lowercase letters. At first, we have the sequence as n '?' marks (n is the length of target string). We also have a stamp of lowercase letters. On each turn, we can place the stamp over the sequence, and replace every letter in the with the corresponding letter from that stamp. You can make up to 10 * n turns.As an example consider the initial sequence is "?????", and the stamp is "abc", then we may make strings like "abc??", "?abc?", "??abc" in the first turn. If the sequence is possible ... Read More

C++ program to overload addition operator to add two matrices

Arnab Chakraborty
Updated on 07-Oct-2021 08:48:02

4K+ Views

Suppose we have two matrices mat1 and mat2. We shall have to add these two matrices and form the third matrix. We shall have to do this by overloading the addition operator.So, if the input is like589679834763then the output will be131113131312To solve this, we will follow these steps −Overload the addition operator, this will take another matrix mat as second argumentdefine one blank 2d array vvDefine one 2D array vv and load current matrix elements into itfor initialize i := 0, when i < size of vv, update (increase i by 1), do:for initialize j := 0, when j < ... Read More

C++ program to overload extraction operator

Arnab Chakraborty
Updated on 07-Oct-2021 08:45:04

496 Views

Suppose we have a Person class with two attributes first_name and the last_name. It also has two methods called get_first_name() and get_last_name() to retrieve or set first name and last name respectively. We shall have to overload the extraction operator (

Program to find minimum time to finish all jobs in Python

Arnab Chakraborty
Updated on 07-Oct-2021 08:49:16

895 Views

Suppose we have an array called jobs, where jobs[i] indicates the amount of time needed to complete the ith job. We also have another value k, to them we can assign jobs. Each job should be assigned to exactly one worker. And the working time of a worker is the total time it takes to complete all jobs assigned to them. We have to find the minimum possible maximum working time of any assignment.So, if the input is like jobs = [2, 1, 3, 8, 5], k = 2, then the output will be 10 because, we can assign jobs ... Read More

C++ program to find maximum of each k sized contiguous subarray

Arnab Chakraborty
Updated on 07-Oct-2021 08:42:37

466 Views

Suppose we have an array with n elements and a value k. We shall have to find maximum value for each of the contiguous subarray of size k.So, if the input is like arr = [3, 4, 6, 2, 8], k = 3, then the output will be The contiguous subarrays of size 3 are [3, 4, 6], [4, 6, 2], [6, 2, 8], so the maximum elements are 6, 6 and 8.To solve this, we will follow these steps −Define one deque Qi of size kfor initialize i := 0, when i < k, update (increase i by 1), ... Read More

C++ program to define exception for small username, and validate username

Arnab Chakraborty
Updated on 07-Oct-2021 08:40:06

842 Views

Suppose we have a string of usernames and we shall have to check whether username is valid or not based on few conditions. So we shall have to define an exception that is thrown when the length of username is less than 5 characters long. We shall have to return "Valid" for valid username, "Invalid" for invalid username and throw exception for smaller usernames. The valid username conditions are −Username must be five-character longThere should not two consecutive 'w' in the usernameSo, if the input is like unames = ["amit", "to", "paul_tim", "greg_harry", "towwer"], then the output will be [Too ... Read More

C++ program to overload addition operator to add two complex numbers

Arnab Chakraborty
Updated on 31-Oct-2023 02:59:44

36K+ Views

Suppose we have a complex number class with real and imaginary part. We shall have to overload the addition (+) operator to add two complex number. We also have to define a function to return complex number in proper representation.So, if the input is like c1 = 8 - 5i, c2 = 2 + 3i, then the output will be 10 - 2i.To solve this, we will follow these steps −Overload the + operator and take another complex number c2 as argumentdefine a complex number called ret whose real and imag are 0real of ret := own real + real ... Read More

Advertisements