Check If We Can Form Array from Pieces in Python

Arnab Chakraborty
Updated on 17-May-2021 13:09:09

192 Views

Suppose we have an array nums where all elements are unique and have another array with different smaller arrays called pieces. We have to check whether we can get the main array nums by concatenating the arrays in pieces in any order or not. But we are not allowed to reorder the elements present in each array pieces[i].So, if the input is like nums = [5, 1, 12, 36, 2, 47, 6] pieces = [[2, 47, 6], [12, 36], [1], [5]], then the output will be True because we can concatenate them in this order [[5], [1], [12, 36], [2, ... Read More

Sort Array by Increasing Frequency of Elements in Python

Arnab Chakraborty
Updated on 17-May-2021 13:07:55

2K+ Views

Suppose we have an array with some elements where elements may appear multiple times. We have to sort the array such that elements are sorted according to their increase of frequency. So which element appears less number of time will come first and so on.So, if the input is like nums = [1, 5, 3, 1, 3, 1, 2, 5], then the output will be [2, 5, 5, 3, 3, 1, 1, 1]To solve this, we will follow these steps −mp := a new mapfor each distinct element i from nums, dox:= number of i present in numsif x is ... Read More

Find Largest Substring Between Two Equal Characters in Python

Arnab Chakraborty
Updated on 17-May-2021 13:07:36

259 Views

Suppose we have a string s, we have to find the length of the longest substring between two equal letters or elements, excluding the two characters. If we cannot find such substring, then return -1.So, if the input is like s = "level", then the output will be 3 as optimal substrings can be either "lev" or "vel".To solve this, we will follow these steps −memo := a new mapfor i in range 0 to size of s - 1, doif s[i] is in memo, theninsert i at the end of memo[s[i]]otherwise, memo[s[i]] := a list with only one element ... Read More

Find Mean of Array After Removing Elements in Python

Arnab Chakraborty
Updated on 17-May-2021 13:07:17

296 Views

Suppose we have array called nums, we have to find the mean of the remaining values after removing the smallest 5% and the largest 5% of the elements.So, if the input is like nums = [2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8], then the output will be 4.0 because after removing smallest and largest values, all are same, then the median isTo solve this, we will follow these steps −sort the list numsn := size of numsper := quotient of (n*5/100)l2 := subarray of nums from index ... Read More

Find X for Special Array with X Elements Greater Than or Equal to X in Python

Arnab Chakraborty
Updated on 17-May-2021 13:05:12

498 Views

Suppose we have an array called nums where all elements are either 0 or positive. The nums is considered special array if there exists a number x such that there are exactly x numbers in nums which are larger than or equal to x. And x does not have to be an element in nums. Here we have to find x if the array is special, otherwise, return -1.So, if the input is like nums = [4, 6, 7, 7, 1, 0], then the output will be 4 as there are 4 numbers which are greater or equal to 4.To ... Read More

Design Parking System in Python

Arnab Chakraborty
Updated on 17-May-2021 13:02:23

3K+ Views

Suppose you want to design a parking system. A parking lot has three different kinds of parking spaces − big, medium, and small. And there are fixed number of slots for each size. Make a class called OurParkingSystem with of two methods −constructor(big, medium, small) − This constructor is taking the number of slots available for different spaces and initializes object of the OurParkingSystem class.addCar(carType) − This method checks whether there is a parking space of given carType for the car that wants to put inside the parking lot.The three slots big, medium, or small, are represented by 1, 2, ... Read More

Minimum Jumps to Return Home from a Folder in Python

Arnab Chakraborty
Updated on 17-May-2021 13:01:57

369 Views

Suppose we have a logs where we have path to enter into folders, there may be different symbols like −"../" : Move to the parent folder from current one. (If we are at main folder, do not change location)."./" : Remain in the current folder."x/" : Move to the child folder named x.From the logs we have to find minimum number of operations needed to come back from last folder where we stop to the main folder.So, if the input is like logs = ["Dir1/", "Dir2/", "../", "Dir2/", "Dir3/", "./"], then the output will be 3from the image we can ... Read More

Rearrange Spaces Between Words in Python

Arnab Chakraborty
Updated on 17-May-2021 13:01:33

430 Views

Suppose we have a string s with some words that are placed among some number of spaces. Each words are separated by at least one space. We have to rearrange the spaces so that there are same number of spaces between every pair of adjacent words and the number of spaces between each word is maximized. If we are unable to redistribute all the spaces equally, we can place the extra spaces at the end.So, if the input is like s = " I love programming ", then the output will be "I love programming ", see the spaces are ... Read More

Find Sum of All Odd Length Subarrays in Python

Arnab Chakraborty
Updated on 17-May-2021 13:01:13

520 Views

Suppose we have an array of positive values called nums, we have to find the sum of all possible odd-length subarrays. As we know a subarray is a contiguous subsequence of the array. We have to find the sum of all odd-length subarrays of nums.So, if the input is like nums = [3, 8, 2, 5, 7], then the output will be The odd-length subarrays are −nums[0] = 3 nums[1] = 8 nums[2] = 2 nums[3] = 5 nums[4] = 7 nums[0..2], so sum = 13 nums[1..3], so sum = 15 nums[2..4], so sum = 14 nums[0..4] = 25 So ... Read More

Find Number of Special Positions in a Binary Matrix Using Python

Arnab Chakraborty
Updated on 17-May-2021 13:00:44

320 Views

Suppose we have a binary matrix of order m x n, we have to find the number of special positions in the matrix. A position (i, j) is a special position when mat[i, j] = 1 and all other elements in row i and column j are 0.So, if the input is like10000001000001101000then the output will be 3, here the special positions are (0, 0), (1, 2) and (3, 1).To solve this, we will follow these steps −special := 0for i in range 0 to row count of matrix, doif number of 1s in row matrix[i] is 1, thennumOfOne := ... Read More

Advertisements