Programming Articles - Page 2152 of 3366

Two City Scheduling in C++

Arnab Chakraborty
Updated on 28-Apr-2020 17:33:00

530 Views

Suppose there are 2N persons. A company wants to organize one interview. The cost for flying the i-th person to city A is costs[i][0], and the cost for flying the i-th person to city B is costs[i][1]. We have to find the minimum cost to fly every person to a city, such that N people arrive in every city. So if the given list is [[10, 20], [30, 200], [400, 50], [30, 20]] The output will be 110. So we will send the person P1 to city A with cost 10, Second person to city A with cost 30, third ... Read More

Partition Array Into Three Parts With Equal Sum in Python

Arnab Chakraborty
Updated on 28-Apr-2020 17:30:30

473 Views

Suppose we have an array A of integers, our output will be true if and only if we can partition the array into three non-empty parts whose sum is equal.Formally, we can partition the array if we can find the indexes i+1 < j with (A[0] + A[1] + ... + A[i] is same as A[i+1] + A[i+2] + ... + A[j-1] and A[j] + A[j-1] + ... + A[A.length - 1])So if the input is [0, 2, 1, -6, 6, -7, 9, 1, 2, 0, 1], then the output will be true. Three arrays will be [0, 2, 1], ... Read More

Pairs of Songs With Total Durations Divisible by 60 in Python

Arnab Chakraborty
Updated on 28-Apr-2020 17:27:04

2K+ Views

Suppose we have a list of songs, the i-th song has a duration of time[i] seconds. We have to find the number of pairs of songs for which their total time in seconds is divisible by 60.So if the time array is like [30, 20, 150, 100, 40], then the answer will be 3. Three pairs will be (3, 150), (20, 100), (20, 40) for all cases the total duration is divisible by 60.To solve this, we will follow these steps −Take a map rem to store remainders. Set ans := 0for all elements i in time −if i is ... Read More

Sum of Even Numbers After Queries in Python

Arnab Chakraborty
Updated on 28-Apr-2020 17:19:35

3K+ Views

Suppose we have an array of integers called A, and an array queries. For the i-th query value = queries[i][0] and index = queries[i][1], we will add value to A[index]. Then, the answer of the i-th query is the sum of the even values of A. We have to find the answer to all queries. We will find an array, that should have answer[i] as the answer to the i-th query. So if the array is like [1, 2, 3, 4], and the query array is like [[1, 0], [-3, 1], [-4, 0], [2, 3]], then the answer array will ... Read More

Long Pressed Name in C++

Arnab Chakraborty
Updated on 28-Apr-2020 17:17:37

300 Views

Suppose a man is typing some name on keyboard. Sometimes some buttons are long-pressed by mistake. So it may type one or more extra character. So we will take two strings, and check whether the second string is long-pressed name or not. So if the name is “Amit”, and second string is “Ammittt” is longpressed name. But “Ammttt” is not, because character i is not present here.To solve this, we will follow these steps −let j := 0for i := 0, i < second.size, increase i by 1 −if j < actual_name.size and actual_name[j] = second[i], thenincrease j by 1return ... Read More

Letter Case Permutation in C++

Arnab Chakraborty
Updated on 28-Apr-2020 16:50:02

293 Views

Suppose we have a string with letters and numbers. We have to generate all possible combinations of that string by taking uppercase and lowercase versions of letters that are present in the string. So if one string has only numbers, only that will be returned. Suppose the string is like “1ab2”, then the strings will be [“1ab2”, “1Ab2”, “1aB2”, “1AB2”]To solve this problem, we will use recursive approach. It takes the index parameter to start work from that index. It also takes a temp string up to which the result is created. When the index is same as the string ... Read More

Jewels and Stones in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:46:58

750 Views

Suppose we have a string J that indicates some letters that are considered as Jewel, and another string S, that represents some stones that we have. Our task is to find how many of stones in S is also jewel. The letters in J and S are case sensitive. So if the J = “aZc”, and S = “catTableZebraPicnic” then there are 7 jewels.To solve this we will convert the string into a list of characters. If the character in J present in S, then increase the count.ExampleLet us see the following implementation to get better understanding − Live Democlass Solution(object): ... Read More

Array Partition I in Python

Sunidhi Bansal
Updated on 03-Nov-2022 06:30:54

2K+ Views

We are given the array, let's say arr[] of 2n integers. We have to make the pair group of the integer elements like(a1, b1), (a2, b2)....(an, bn) which makes the sum of min(ai, bi) for all elements in the array as large as possible. The task is to find the maximum sum of pairs For example arr[] = [1, 2, 3, 4] and output is 4 the maximum sum of pairs is 4. The all possible are − (1, 2) and (3, 4) -> min(1, 2) + min(3, 4) = 1 + 3 = 4. (1, 4) and (2, 3) ... Read More

Diameter of Binary Tree in Python

Arnab Chakraborty
Updated on 28-Apr-2020 16:41:03

604 Views

Suppose we have a binary tree; we have to compute the length of the diameter of the tree. The diameter of a binary tree is actually the length of the longest path between any two nodes in a tree. This path not necessarily pass through the root. So if the tree is like below, then the diameter will be 3.as the length of the path [4, 2, 1, 3] or [5, 2, 1, 3] is 3To solve this, we will follow these steps −We will use the dfs to find the diameter, set answer := 0call the dfs function with ... Read More

Repeated Substring Pattern in C++

Arnab Chakraborty
Updated on 28-Apr-2020 16:38:05

2K+ Views

Suppose we have a non-empty string. We have to check whether it can be constructed by taking a substring of it and appending multiple times of the substring. The string consists of lowercase English letters only and its length will not exceed 10000. So if the input is like “abaabaaba”, then answer will be true, as this is created using “aba”To solve this, we will follow these steps −We will use the dynamic programming approach.Define an array DP of size n. n is the size of the stringi := 1 and j := 0while i < nif str[i] == str[j], ... Read More

Advertisements