Max Chunks to Make Sorted in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:26:16

461 Views

Suppose we gave an array arr that is a permutation of [0, 1, ..., arr.length - 1], we have to split the array into some number of "chunks" or partitions, and individually sort each partition. So after concatenating them, the result will be the sorted array. So if the array is like [1, 0, 2, 3, 4], then the output will be 4, as we can split into two partitions like [1, 0] and [2, 3, 4], but this can also be true that [1, 0], [2], [3], [4]. So this is the highest number of chunks possible, so output ... Read More

Find Numbers with Even Number of Digits in Python

Arnab Chakraborty
Updated on 29-Apr-2020 08:24:34

2K+ Views

Suppose we have a list of numbers. We have to count the numbers that has even number of digit count. So if the array is like [12, 345, 2, 6, 7896], the output will be 2, as 12 and 7896 has even number of digitsTo solve this, we will follow these steps −Take the list and convert each integer into stringif the length of string is even, then increase count and finally return the count valueExampleLet us see the following implementation to get better understanding − Live Democlass Solution(object):    def findNumbers(self, nums):       str_num = map(str, nums)   ... Read More

Partition Labels in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:23:37

413 Views

Suppose we have a string S of lowercase letters is given. We will partition this string into as many parts as possible so that each letter appears in at most one part, finally return a list of integers representing the size of these parts. So if the string is like “ababcbacadefegdehijhklij”, output is [9, 7, 8], because the partitions are “ababcbaca”, “defegde”, “hijhklij”. So this is a partition so that each letter occurs in at most one part. A partition like "ababcbacadefegde", "hijhklij" is not correct, because it splits S into less parts.To solve this, we will follow these steps ... Read More

Element Appearing More Than 25% in Sorted Array in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:21:52

290 Views

Suppose we have an array A. There are few elements. Some elements are common. We have to return an element that is appearing more than 25% spaces in the array. So if A = [1, 2, 4, 4, 4, 4, 5, 5, 6, 6, 7, 7], Here 4 has occurred four times. This is more than 25% of 12 (size of the array)To solve this, we will follow these steps −Read elements and store their respective frequenciesIf the frequency is greater than 25% of the array size, then return the result.ExampleLet us see the following implementation to get better understanding ... Read More

Daily Temperatures in Python

Arnab Chakraborty
Updated on 29-Apr-2020 08:20:55

1K+ Views

Suppose we have a list of daily temperatures T, we have to return a list such that, for each day in the input, shows how many days we would have to wait until a warmer temperature. If there is no future day for which this is possible, store 0 instead. For example, if T = [73, 74, 75, 71, 69, 72, 76, 73], output will be [1, 1, 4, 2, 1, 1, 0, 0].To solve this, we will follow these steps −ans := an array of size same as T, and fill this with 0define one stack, and insert 0 ... Read More

Subtract the Product and Sum of Digits of an Integer in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:19:46

500 Views

Suppose we have one number. We have to find the sum of digits and product of digits. After that find the difference between sum and product. So if the number is 5362, then sum is 5 + 3 + 6 + 2 = 16, and 5 * 3 * 6 * 2 = 180. So 180 – 16 = 164To solve this take each digit and add and multiply one by one, then return the difference.ExampleLet us see the following implementation to get better understanding − Live Demo#include using namespace std; class Solution {    public:       int subtractProductAndSum(int n) {          int prod = 1;          int sum = 0;          for(int t = n;t;t/=10){             sum += t % 10;             prod *= t % 10;          }          return prod - sum;       } }; main(){    Solution ob;    cout

Subarray Product Less Than K in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:17:39

367 Views

Suppose we have given an array of positive integers nums. We have to count and print the number of (contiguous) subarrays where the product of each the elements in the subarray is less than k. So if the input is like [10,5,2,6] and k := 100, then the output will be 8. So the subarrays will be [[10], [5], [2], [6], [10, 5], [5, 2], [2, 6] and [5, 2, 6]]To solve this, we will follow these steps −temp := 1, j := 0 and ans := 0for i in range 0 to size of the arraytemp := temp * nums[i]while temp >= k and j = k && j

Minimum Time Visiting All Points in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:17:15

241 Views

Suppose there are some points given as an array. We have to find the minimum time in seconds to visit all points. There are some conditions.In one second, it can move vertically, horizontally and diagonallyWe have to visit the points in the same order as they appear in the array.So if the points are [(1, 1), (3, 4), (-1, 0)], then output will be 7. If we check the sequence for the shortest route, the sequence will be (1, 1), (2, 2), (3, 3), (3, 4), (2, 3), (1, 2), (0, 1), (-1, 0)To solve this we will just find ... Read More

Cells with Odd Values in a Matrix in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:14:01

367 Views

Suppose there are n and m which are the dimensions of a matrix. These are initialized by zeros. And indices are given where indices[i] = [ri, ci]. For each pair of [ri, ci] we have to increment all cells in row ri and column ci by 1. The output will be the number of cells with odd values in the matrix after applying the increment to all indices.To solve this, we will follow these steps −Initialize odd := 0, and x := row count of the matrixcreate a matrix matfor i in range 0 to xr = input[i, 0], c ... Read More

Minimum ASCII Delete Sum for Two Strings in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:12:07

456 Views

Suppose we have two words w1 and w2, we have to find the lowest ASCII sum of deleted characters to make w1 and w2 the same, where in each step we can delete one character in either string. So if the input is like “sea” and “eat”, then the output will be 231, because we need to delete ‘s’ from w1, this will be “ea” and delete “t” from “eat” from w2. Then they are same. Deleting ‘t’ from “eat” adds 116 to the sum, and at the end, both strings are the same and 115 + 116 = 231 ... Read More

Advertisements