Server Side Programming Articles - Page 1427 of 2650

Count pairs of natural numbers with GCD equal to given number in C++

Sunidhi Bansal
Updated on 02-Dec-2020 11:25:00

1K+ Views

We gave three input variables as ‘start’, ‘end’ and ‘number’. The goal is to find pairs of numbers between start and end that have GCD value equal to ‘number’. For example GCD(A, B)=number and both A, B are in range [start, end].Let us understand with examples.Input − start=5 end=20 number=8Output − Count of pairs of natural numbers with GCD equal to given number are − 3Explanation − Pairs between 5 to 20 such that GCD is 8 are − (8, 8), (8, 16), (16, 8)Input − start=5 end=20 number=7Output − Count of pairs of natural numbers with GCD equal to ... Read More

Count pairs with average present in the same array in C++

Sunidhi Bansal
Updated on 02-Dec-2020 11:20:18

316 Views

We are given an array of integers such that each element of the array is in the range [- 1000, 1000]. The goal is to find pairs of elements of the array such that their average is also present in that array. If array is arr[]= [1, 2, 3, 4]. Then pairs would be (1, 3) and (2, 4) as the average of 1, 3 is 2 and the average of 2, 4 is 3 and both 2 and 3 are present in the array. Count would be 2.Let us understand with examples.Input − arr[]= { -1, 2, 5, -3, ... Read More

Count pairs of numbers from 1 to N with Product divisible by their Sum in C++

Sunidhi Bansal
Updated on 02-Dec-2020 11:18:23

281 Views

We are given a number N. The goal is to find the pairs of numbers from 1 to N such that the product of pairs is equal to the sum of pairs.Let us understand with examples.Input − N=11Output − Count of pairs of no. from 1 to N with Product divisible by their Sum are − 1Explanation − Numbers 3 and 6 have product 18 and their sum 9 fully divides 18.Input − N=30Output − Count of pairs of no. from 1 to N with Product divisible by their Sum are − 12Explanation − Pairs are − (3, 6), (4, ... Read More

Count the number of carry operations required to add two numbers in C++

Sunidhi Bansal
Updated on 02-Dec-2020 11:16:24

2K+ Views

We are given two numbers num_1 and num_2. The goal is to count the number of carry operations required if the numbers are added. If numbers are 123 and 157 then carry operations will be 1. (7+3=10, 1+2+5=8, 1+1=2 ).Let us understand with examplesInput − num_1=432 num_2=638Output − Count of number of carry operations required to add two numbers are − 2Explanation − From right to left adding digits and counting carry −(2+9=10, carry 1 ) count=1, (1+3+3=7, carry 0 ) count=1, (4+6=10, carry 1 ) count=2Input − num_1=9999 num_2=111Output − Count of number of carry operations required to add ... Read More

Program to find minimum cost to reach final index with at most k steps in python

Arnab Chakraborty
Updated on 02-Dec-2020 05:50:04

261 Views

Suppose we have a list of numbers nums and another value k. Here the items at nums[i] represents the costs of landing at index i. If we start from index 0 and end at the last index of nums. In each step we can jump from some position X to any position up to k steps away. We have to minimize the sum of costs to reach last index, so what will be the minimum sum?So, if the input is like nums = [2, 3, 4, 5, 6] k = 2, then the output will be 12, as we can ... Read More

Program to find maximum profit after buying and selling stocks at most two times in python

Arnab Chakraborty
Updated on 02-Dec-2020 05:48:17

294 Views

Suppose we have a list of numbers called prices and that is representing the stock prices of a company in chronological order, we have to find the maximum profit we can make from buying and selling that stock at most two times. We have to buy first then sell.So, if the input is like prices = [2, 6, 3, 4, 2, 9], then the output will be 11, as we can buy at price 2, then sell at 6, again buy at 2, and sell at 9.To solve this, we will follow these steps −first_buy := -inf, first_sell := -infsecond_buy ... Read More

Program to check a string can be broken into given list of words or not in python

Arnab Chakraborty
Updated on 02-Dec-2020 05:47:04

609 Views

Suppose we have a word list and another string s with no spaces. We have to check whether the string can be broken down using the list of words or not.So, if the input is like words = ["love", "python", "we", "programming", "language"] s = "welovepythonprogramming", then the output will be TrueTo solve this, we will follow these steps −words := a new set of all unique wordsDefine a function rec() . This will take iif i is same as size of s , thenreturn Trueacc := blank stringfor j in range i to size of s, doacc := acc ... Read More

Program to find maximum number of boxes we can fit inside another boxes in python

Arnab Chakraborty
Updated on 02-Dec-2020 05:45:08

1K+ Views

Suppose we have a list of boxes where each row represents the height and width of given boxes. We can put a box in another box if first box is smaller than the second one (when both of its width and height are smaller than the other box), we have to find the maximum number of boxes we can fit into a box.So, if the input is likeWidthHeight1212101066510then the output will be 3, as we can fit the box [6, 6] inside [10, 10] which we can be put into [12, 12] box.To solve this, we will follow these steps ... Read More

Program to find length of longest consecutive path of a binary tree in python

Arnab Chakraborty
Updated on 02-Dec-2020 05:43:20

255 Views

Suppose we have a binary tree; we have to find the longest path in the binary tree.So, if the input is likethen the output will be 5 as longest consecutive sequence is [2, 3, 4, 5, 6].To solve this, we will follow these steps −if root is null, thenreturn 0maxPath := 0Define a function helper() . This will take nodeinc := 1, dec := 1if left of node is not null, then[left_inc, left_dec] := helper(left of node)otherwise, [left_inc, left_dec] := [0, 0]if right of node is not null, then[right_inc, right_dec] := helper(right of node)otherwise, [right_inc, right_dec] := [0, 0]if left ... Read More

Program to make almost BST to exact BST in python

Arnab Chakraborty
Updated on 02-Dec-2020 05:40:59

193 Views

Suppose we have a binary tree and that is almost a binary search tree. Only two nodes' value are swapped. We have to correct it and return the binary search tree.So, if the input is likethen the output will beTo solve this, we will follow these steps −prev_node := null, min_node := null, max_node := nullfound_one := Falsefor each node in the inorder traversal of root, doif prev_node is not null, thenif value of node < value of prev_node, thenif min_node is null or value of node < value of min_node, thenmin_node := nodeif max_node is null or value of ... Read More

Advertisements