Found 26504 Articles for Server Side Programming

Program to count minimum number of operations required to make numbers non coprime in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:57:53

324 Views

Suppose we have two numbers A and B. Now in each operation, we can select any one of the number and increment it by 1 or decrement it by 1. We have to find the minimum number of operations we need such that the greatest common divisor between A and B is not 1.So, if the input is like A = 8, B = 9, then the output will be 1, as we can select 9 then increase it to 10, so 8 and 10 are not coprime.To solve this, we will follow these steps:if gcd of a and b ... Read More

Program to find next state of next cell matrix state in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:54:17

184 Views

Suppose we have a 2D binary matrix where a 1 means a live cell and a 0 means a dead cell. A cell's neighbors are its immediate horizontal, vertical and diagonal cells. We have to find the next state of the matrix using these rulesAny living cell with two or three living neighbors lives.Any dead cell with three living neighbors becomes a live cell.All other cells die.So, if the input is like1100010001011101then the output will be1100010001001100To solve this, we will follow these steps:n := row size of matrix, m := column size of matrixres := a matrix of size n ... Read More

Program to remove all nodes with only one child from a binary tree in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:51:15

702 Views

Suppose we have a binary tree root; we have to remove all nodes with only one child.So, if the input is likethen the output will beTo solve this, we will follow these steps:Define a method called solve(), this will take tree rootif root is null, thenreturn rootif left of root is null and right of root is null, thenreturn rootif left of root is null, thenreturn solve(right of root)if right of root is null, thenreturn solve(left of root)left of root := solve(left of root)right of root := solve(right of root)return rootExampleclass TreeNode:    def __init__(self, data, left = None, right ... Read More

Program to find length of longest possible stick in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:47:08

281 Views

Suppose we have a list of integers sticks. Here each element in the list represents a stick with two ends, these values are between 1 and 6. These are representing each end. We can connect two sticks together if any of their ends are same. The resulting stick's ends will be the leftover ends and its length is increased. We have to find the length of the longest stick possible.So, if the input is like sticks = [ [2, 3], [2, 4], [3, 5], [6, 6] ], then the output will be 3, as we can connect [2, 3] and ... Read More

Program to find a matrix for each condominium's height is increased to the maximum possible height in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:44:10

268 Views

Suppose we have a 2D matrix, where matrix [r, c] represents the height of a condominium in a city. The west-east skyline is visible by taking the maximum of each row in the matrix. And the north-south skyline can be visible by taking the maximum of each column. We have to find a new matrix where each condominium's height is increased to the maximum possible height while keeping the same west-east and north-south skyline.So, if the input is like23456789104447778910as the west-east skyline is [4, 7, 10] and north-south skyline is [8, 9, 10]. We can increase everything in the first ... Read More

Program to find sum of concatenated pairs of all each element in a list in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:41:57

575 Views

Suppose we have a list of numbers called nums. We have to find the sum of every concatenation of every pair of numbers in nums. Here the pair (i, j) and pair (j, i) are considered different.So, if the input is like nums = [5, 3], then the output will be 176, as We have the following concatenations: (nums[0] + nums[0]) = (5 concat 5) = 55, (nums[0] + nums[1]) = (5 concat 3) = 53, (nums[1] + nums[0]) = (3 concat 5) = 35, (nums[0] + nums[0]) = (3 concat 3) = 33, then the sum is 55 + ... Read More

Program to find length of concatenated string of unique characters in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:36:22

262 Views

Suppose we have a list of strings words. We have to make a string that is constructed by concatenating a subsequence of words such that each letter is unique. We have to finally find the length of the longest such concatenation.So, if the input is like words = ["xyz", "xyw", "wab", "cde"], then the output will be 9, as we cannot pick any word since they contain duplicate characters.To solve this, we will follow these stepsans := 0Define a function recur() . This will take i:= 0, cur:= blank stringif i is same as size of words , then   ... Read More

Program to find minimum number of groups in communication towers in C++?

Arnab Chakraborty
Updated on 10-Nov-2020 08:33:18

211 Views

Suppose we have a 2D binary matrix where 1 represents a communication tower, and 0 represents an empty cell. The towers can communicate in the following ways: 1. If tower A, and tower B are either on the same row or column, they can communicate with each other. 2. If tower A can talk with tower B and B can talk with C, then A can talk to C (transitive property). We have to find the total number of tower groups there are (here a group is a list of towers that can talk with each other).So, if the input ... Read More

Program to find angle between hour and minute hands of a clock in C++?

Arnab Chakraborty
Updated on 10-Nov-2020 08:26:28

2K+ Views

Suppose we have two values hours and minutes. We have to find a smaller angle formed between the hour and the minute hand.So, if the input is like hour = 12 minutes = 45, then the output will be 112.5To solve this, we will follow these steps:if h = 12, then set h := 0if m = 60, then set m := 0hAngle := 0.5 * (60h) + mmAngle := 6mret := |hAngle - mAngle|return minimum of ret and (360 – ret)Let us see the following implementation to get better understanding:Example Live Demo#include using namespace std; class Solution { public: ... Read More

Program to find maximum number of courses we can take based on interval time in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:24:08

266 Views

Suppose we have a list of intervals in the form [start, end], this is representing the start and end time of a course. We have to find the maximum number of courses we can take, assuming we can only take one course at a time and the start of a course needs to be later than the end of the last course.So, if the input is like times = [[3, 6], [6, 9], [7, 8], [9, 11]], then the output will be 3, as we can take courses [[3, 6], [7, 8], [9, 11]]To solve this, we will follow these ... Read More

Advertisements