Found 26504 Articles for Server Side Programming

Program to find nth ugly number in C++

Arnab Chakraborty
Updated on 26-Nov-2020 08:06:06

331 Views

Suppose we have a number n; we have to find the nth ugly number. As we know that the ugly numbers are those numbers, whose prime factors are only 2, 3 and 5. So if we want to find 10th ugly number, the output will be 12, as the first few ugly numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 and so on.To solve this, we will follow these steps:Define an array v of size(n + 1)if n is same as 1, then:return 1two := 2, three := 3, five := 5twoIdx := 2, threeIdx := 2, fiveIdx := 2for initialize i := 2, when i

Program to find median of two sorted lists in C++

Arnab Chakraborty
Updated on 26-Nov-2020 07:56:38

211 Views

Suppose we have two sorted lists. We have to find the median of these two lists. So if the arrays are like [1,5,8] and [2,3,6,9], then the answer will be 5.To solve this, we will follow these steps:Define a function solve(), this will take an array nums1, an array nums2,if size of nums1 > size of nums2, then:return solve(nums2, nums1)x := size of nums1, y := size of nums2low := 0, high := xtotalLength := x + ywhile low

Program to check whether given list of blocks are symmetric over x = y line or not in python

Arnab Chakraborty
Updated on 26-Nov-2020 07:49:12

389 Views

Suppose we have a list of numbers called nums. And it is representing the height of square blocks, we have to check whether the shape is symmetric over the y = x line or not.So, if the input is like nums = [7, 5, 3, 2, 2, 1, 1], then the output will be TrueTo solve this, we will follow these steps:i := 0j := size of nums - 1while i

Program to count number of consecutive lists whose sum is n in C++

Arnab Chakraborty
Updated on 26-Nov-2020 07:46:21

358 Views

Suppose we have a number n, we have to find the number of lists of positive consecutive values that sum up to n.So, if the input is like n = 15, then the output will be 4, as The possible lists are: [1, 2, 3, 4, 5], [4, 5, 6], [7, 8], and [15].To solve this, we will follow these steps:begin := 1, end := 1, x := (n + 1)sum := 0while end = n, do:if sum is same as n, then:(increase count by 1)sum := sum - begin(increase begin by 1)(increase end by 1)return count + 1Let us ... Read More

Program to find number of unique four indices where they can generate sum less than target from four lists in python

Arnab Chakraborty
Updated on 26-Nov-2020 07:43:54

179 Views

Suppose we have four list of numbers A, B, C and D and also have another number target. We have to find the number of different unique indices i, j, k, l such that A[i] + B[j] + C[k] + D[l] ≤ target.So, if the input is like A = [3, 2] B = [5, 3] C = [1] D = [2, 3] target = 9, then the output will be 3, as We can pick the following combinations: [3, 3, 1, 2] [3, 3, 1, 2] [2, 3, 1, 3]To solve this, we will follow these steps:temp_list := a ... Read More

Program to print matrix elements in spiral order in python

Arnab Chakraborty
Updated on 26-Nov-2020 07:39:27

2K+ Views

Suppose we have a 2D matrix mat. We have to print the matrix elements in a spiral way. At first starting from the first row (mat[0, 0]), print the whole content and then follow the last column to print, then the last row, and so on, thus it prints the elements in a spiral fashion.So, if the input is like71092916239142759911then the output will be [7, 10, 9, 1, 3, 4, 5, 11, 9, 9, 2, 9, 6, 2, 9, 2, 1, 7]To solve this, we will follow these steps:d := 0top := 0, down := row count of matrix – 1, left := 0, right := column count of matrix - 1c := 0res := a new listdirection := 0while top

Program to sort a given linked list into ascending order in python

Arnab Chakraborty
Updated on 26-Nov-2020 07:34:29

5K+ Views

Suppose we have a linked list. We have to sort the list into ascending order.So, if the input is like [5, 8, 4, 1, 5, 6, 3], then the output will be [1, 3, 4, 5, 5, 6, 8, ]To solve this, we will follow these steps:values := a new listhead := nodewhile node is not null, doinsert value of node at the end of valuesnode := next of nodesort the list valuesvalues := make a double ended queue by taking elements of valuesnode := headwhile node is not null, dovalue of node := left element of queue and delete ... Read More

Program to find number of items left after selling n items in python

Arnab Chakraborty
Updated on 26-Nov-2020 07:31:35

3K+ Views

Suppose we have a list of numbers called items and another value n. A salesman has items in a bag with random IDs. The salesman can delete as many as n items from the bag. We have to find the minimum number of different IDs in the bag after n removals.So, if the input is like items = [2, 2, 6, 6] n = 2, then the output will be 1 as we he can sell two items with ID 2 or ID 6, then only items with single target will be there.To solve this, we will follow these steps:c ... Read More

Program to find second deepest node in a binary tree in python

Arnab Chakraborty
Updated on 26-Nov-2020 07:29:14

318 Views

Suppose we have a binary tree; we have to find the depth of the second deepest leaf. If there are multiple deepest leaves, the second deepest leaf node will be the next highest one. As we know the root has a depth of 0.So, if the input is likethen the output will be 1, as the second deepest node is 3.To solve this, we will follow these steps:if root is null, thenreturn nullnodes := a new listinsert root at the end of nodescount := 0, prev := 0, now := 0while nodes is not empty, donew := a new listflag ... Read More

Program to check whether leaves sequences are same of two leaves or not in python

Arnab Chakraborty
Updated on 26-Nov-2020 07:21:57

115 Views

Suppose we have two binary trees; we have to check whether the sequence of leaves left-to-right in both trees are the same.So, if the input is likethen the output will be True as the sequence is [2, 6] for both trees.To solve this, we will follow these steps:c := a new listDefine a function inorder() . This will take root, and cif c is null, thenc := a new listif root is not null, theninorder(left of root, c)if left of root is null and right of root is null, theninsert value of root at the end of cinorder(right of root, ... Read More

Advertisements