Programming Articles - Page 1013 of 3363

Program to find index whose left and right elements sums are equal in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:28:14

714 Views

Suppose we have a list of items called nums, we have to find the smallest index i such that the sum of the numbers which are present at the left of i is equal to the sum of numbers present at right of i. If we cannot find any such solution, return -1.So, if the input is like nums = [8, 2, 3, 6, 5, 2, 5, 9, 1, 2], then the output will be 4, because sum of elements that are left of index 4 is [8, 2, 3, 6] = 19, and sum of elements that are present ... Read More

Program to find highest common factor of a list of elements in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:26:33

4K+ Views

Suppose we have a list of elements called nums, we have to find the largest positive value that divides each of the integers.So, if the input is like nums = [15, 81, 78], then the output will be 3, as 3 is the largest integer that divides all 15, 81, and 78.To solve this, we will follow these steps −if size of nums is same as 1, thenreturn nums[0]div := gcd of nums[0] and nums[1])if size of nums is same as 2, thenreturn divfor i in range 1 to size of nums - 2, dodiv := gcd of div and ... Read More

Program to check n can be represented as sum of k primes or not in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:25:38

711 Views

Suppose we have two inputs n and k. We have to check whether n can be represented as a sum of k number of prime values or not.So, if the input is like n = 30 k = 3, then the output will be True because 30 can be represented like 2 + 11 + 17.To solve this, we will follow these steps −if n < k*2, then return Falseif k > 2, then return Trueif k is same as 2, thenif n is even, then return Trueif (n-2) is prime, then return Truereturn Falseif n is prime, then return ... Read More

Program to find all substrings whose anagrams are present in a string in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:22:40

520 Views

Suppose we have a string s with lowercase letters. We have to find all in s where there must be another substring in s at a different location that is an anagram of that taken substrings. We have to find a list of substrings in in lexicographic order.So, if the input is like s = "abcba", then the output will be ['a', 'a', 'ab', 'abc', 'abcb', 'b', 'b', 'ba', 'bc', 'bcba', 'cb', 'cba'] for each of them we can find different anagrams present in the string itself.To solve this, we will follow these steps −res := a new listL := ... Read More

Program to find smallest index for which array element is also same as index in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:20:25

184 Views

Suppose we have a list of elements called nums where all items are unique, and they are sorted in ascending order, we have to find the minimum i such that nums[i] = i. If we cannot find any solution, then return -1. We have to solve this problem in O(log(n)) time.So, if the input is like nums = [-4, -1, 2, 3, 8], then the output will be 2, because both nums[2] = 2 and nums[3] = 3 but 2 is smaller.To solve this, we will follow these steps −ret := -1, lhs := 0, rhs := size of nums ... Read More

Program to find first fit room from a list of rooms in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:18:40

260 Views

Suppose we have a list of numbers called rooms and another target value t. We have to find the first value in rooms whose value is at least t. If there is no such room, return -1.So, if the input is like rooms = [20, 15, 35, 55, 30] t = 30, then the output will be 35. Because 30 is smaller than 35 and previous rooms are not sufficient for target 30.To solve this, we will follow these steps −for each room in rooms, doif room >= t, thenreturn roomreturn -1ExampleLet us see the following implementation to get better ... Read More

Program to check given number is a Fibonacci term in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:20:43

2K+ Views

Suppose we have a number say n. We have to check whether n is present in Fibonacci sequence or not. As we know in Fibonacci sequence f(i) = f(i-1) + f(i-2) for each i from 2 to n, and f(0) = 0, f(1) = 1.So, if the input is like n = 13, then the output will be True, as some terms in Fibonacci sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, so 34 is present.To solve this, we will follow these steps −phi := 0.5 + 0.5 * square root of(5.0)a := phi * nreturn ... Read More

Program to find local peak element indices from a list of numbers in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:17:17

463 Views

Suppose we have a list of numbers called nums whose length is at least 2. We have to find the index of every peak in the list. The list is sorted in ascending order. An index i is a peak when −nums[i] > nums[i + 1] when i = 0nums[i] > nums[i - 1] when i = n - 1nums[i - 1] < nums[i] > nums[i + 1] elseSo, if the input is like nums = [5, 6, 7, 6, 9], then the output will be [2, 4], as the element at index 2 is 7 which is larger than ... Read More

Program to check whether elements frequencies are even or not in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:14:01

131 Views

Suppose we have a list of elements called nums, we have to check whether all numbers appear even times or not. We have to solve it using constant space.So, if the input is like nums = [8, 9, 9, 8, 5, 5], then the output will be True, because all number have occurred twice.To solve this, we will follow these steps −if size of nums is odd, thenreturn Falsesort the list numsfor i in range 1 to size of nums, doif nums[i] is same as nums[i - 1], thennums[i] := 0, nums[i - 1] := 0return true when sum of ... Read More

Program to check same value and frequency element is there or not in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:12:26

184 Views

Suppose we have a list of numbers called nums, we have to check whether there is an element whose frequency is same as its value.So, if the input is like nums = [2, 5, 7, 5, 3, 5, 3, 5, 9, 9, 5], then the output will be True, because 5 appears 5 times.To solve this, we will follow these steps −nums_c := a list containing frequencies of each elements present in numsfor each value i and frequency j in nums_c, doif i is same as j, thenreturn Truereturn FalseExampleLet us see the following implementation to get better understandingfrom collections ... Read More

Advertisements