Found 33676 Articles for Programming

Check if the elements of stack are pairwise sorted in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:37:11

324 Views

Suppose we have a stack of numbers; we have to check whether values in the stack are pairwise consecutive or not. These pairs can be increasing or decreasing. If the stack has an odd number of values, the top element is left out of a pair. And we should retain the original stack content after checking. To solve this problem, we can use three operations on stack called push, pop and check whether stack is empty or not.So, if the input is like stk = [5, 6, -4, -5, 12, 11, 6, 7, 22], then the output will be True as ... Read More

Check if the characters of a given string are in alphabetical order in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:34:35

1K+ Views

Suppose we have a string s. We have to check whether characters in s are in alphabetical order or not.So, if the input is like s = "mnnooop", then the output will be True.To solve this, we will follow these steps −char_arr := a new list from the characters present in ssort the list char_arrreturn char_arr is same as list of all characters in s then true otherwise falseLet us see the following implementation to get better understanding −Example CodeLive Demodef solve(s):    char_arr = list(s)    char_arr.sort()            return char_arr == list(s)   s ... Read More

Check if the characters in a string form a Palindrome in O(1) extra space in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:32:50

513 Views

Suppose we have a string s. This string can contain lowercase characters, other special characters and numbers. We have to check whether only the letters present in the string are palindromic or not. Here one constraint is that there we are not allowed to use extra space to solve this problem.So, if the input is like s = "ra$5ce58car", then the output will be True, as the letters are forming "racecar" which is palindrome.To solve this, we will follow these steps −Define a function first_letter_index() . This will take str, left, rightindex := -1for i in range left to right ... Read More

Check if the binary representation of a number has equal number of 0s and 1s in blocks in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:31:14

257 Views

Suppose we have a number num, we have to check whether the binary representation of num has the same number of consecutive blocks of 0s and 1s. We have to keep in mind that 0 and a number with all 1s are not considered to have number of blocks of 0s and 1s.So, if the input is like num = 455, then the output will be True, as the binary representation of this number is 111000111.To solve this, we will follow these steps −bin_form := binary form of numone_count := a new setcount := 1for i in range 0 to ... Read More

Check if the array is beautiful in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:29:51

564 Views

Suppose we have an array nums of unique elements. We have to check whether these conditions satisfy or not:Elements will be in range 1 to n.The array must not be sorted in ascending order.So, if the input is like nums = [2, 6, 1, 5, 3, 4], then the output will be True.To solve this, we will follow these steps −n := size of numstotal := nums[0]is_sorted := Truefor i in range 1 to n - 1, doif nums[i] is same as nums[i - 1], thenreturn Falseif nums[i] < nums[i - 1], thenis_sorted := Falsetotal := total + nums[i]if is_sorted ... Read More

Check if the array has an element which is equal to sum of all the remaining elements in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:28:04

362 Views

Suppose we have an array called nums we have to check whether the array contains an element whose value is same as sum of all other elements.So, if the input is like nums = [3, 2, 10, 4, 1], then the output will be True, 10 = (3+2+4+1).To solve this, we will follow these steps −freq := an empty maptotal := 0for i in range 0 to size of nums - 1, dofreq[nums[i]] := freq[nums[i]] + 1total := total + nums[i]if total is even, thenif freq[quotient of (total / 2)] is non-zero, thenreturn Truereturn FalseLet us see the following implementation ... Read More

Check if the array has an element which is equal to product of remaining elements in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:27:06

154 Views

Suppose we have an array called nums we have to check whether the array contains an element whose value is same as product of all other elements.So, if the input is like nums = [3, 2, 24, 4, 1], then the output will be True, 24 = (3*2*4*1).To solve this, we will follow these steps −mul := 1for i in range 0 to size of nums - 1, domul := mul * nums[i]for i in range 0 to size of nums - 1, doif nums[i] is same as (mul / nums[i]), thenreturn Truereturn FalseLet us see the following implementation to ... Read More

Check if the array can be sorted using swaps between given indices only in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:25:56

182 Views

Suppose we have an array called nums with unique values from range [0, n – 1]. This array is unsorted. We also have another array of pairs where each pair contains indices where the elements of the array can be swapped. We can swap multiple number of times. We have to check whether we can arrange the array in sorted order using these swaps or not.So, if the input is like nums = [6, 1, 7, 3, 0, 5, 4, 2] pairs = [(0, 4), (6, 0), (2, 7)], then the output will be True, as we can swap index ... Read More

Check if sums of i-th row and i-th column are same in matrix in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:22:35

228 Views

Suppose we have a 2D matrix. We have to check whether the sum of i-th row is same as the sum of i-th column or not.So, if the input is like23451064214671567then the output will be True, as the sum of first row and column is (2 + 3 + 4 + 5) = 14 and (2 + 10 + 1 + 1) = 14.To solve this, we will follow these steps −row := row count of matcol := column count of mattotal_row := 0, total_col := 0for i in range 0 to row - 1, dototal_row := 0, total_col := ... Read More

Check if sum of divisors of two numbers are same in Python

Arnab Chakraborty
Updated on 15-Jan-2021 06:21:13

297 Views

Suppose we have two numbers p and q. We have to check whether the sum of all divisors of these tow numbers are same or not.So, if the input is like p = 559, q = 703, then the output will be True the divisors of 559 is 1, 13, 43 and 703 is 1, 19, 37. The sum of the divisors are 57.To solve this, we will follow these steps −Define a function divSum() . This will take ntotal := 1i := 2while i * i

Advertisements