Server Side Programming Articles - Page 2173 of 2650

Find any pair with given GCD and LCM in C++

Arnab Chakraborty
Updated on 25-Sep-2019 12:56:42

308 Views

In this section we will see how to get number of pairs using the given GCD and LCM values. Suppose the GCD and LCM values are 2 and 12. Now the possible pairs of numbers are (2, 12), (4, 6), (6, 4) and (12, 2). So our program will find the count of pairs. That is 4.Let us see the algorithm to understand what will be the technique to solve this problem.AlgorithmcountPairs(gcd, lcm): Begin    if lcm is nit divisible by gcd, then       return 0    temp := lcm/gcd    c := primeFactorCount(temp)    res := shift ... Read More

Minimum insertions to make a Co-prime array in C++

Arnab Chakraborty
Updated on 25-Sep-2019 12:47:05

200 Views

In this section we will see another interesting problem. Suppose we have an array of N elements. We have to find minimum number of intersection points to make this array as co-prime array. In the co-prime array gcd of every two consecutive elements is 1. We have to print the array also.Suppose we have elements like {5, 10, 20}. This is not co-prime array. Now by inserting 1 between 5, 10 and 10, 20, it will be co-prime array. So the array will be like {5, 1, 10, 1, 20}AlgorithmmakeCoPrime(arr, n): begin    count := 0    for i in ... Read More

Minimum Initial Energy Required To Cross Street in C++

Arnab Chakraborty
Updated on 25-Sep-2019 12:40:05

453 Views

Suppose we have an array where positive and negative numbers are stored. The array is representing the checkpoint from one end to another end of the streets. The positive and negative values are representing the energy at the checkpoints. The positive values can increase energy, and negative number decreases energy. We have to find the initial energy level to cross the street, such that energy level never becomes 0 or less than 0.Suppose we have an array A = {4, -6, 2, 3}. Let the initial energy is 0. So after reaching at first check point, the energy is 4. ... Read More

Python Program for Binary Search

Farhan Muhamed
Updated on 22-Aug-2025 15:53:46

6K+ Views

The binary search algorithm is the fastest algorithm to search for an element in a sorted array. It works by dividing the array into two halves and checking if the target element is in the left or right half, thus reducing the search space by half with each iteration. Implementing Binary Search Algorithm Given a sorted array of integers and a target element, and our task is to implement a python program to search for the target element in the array using the binary search algorithm. Here are some example scenarios: Scenario 1 Input: arr[] = {1, ... Read More

Maximum Length Chain of Pairs in C++

Arnab Chakraborty
Updated on 25-Sep-2019 12:32:39

217 Views

There is a chain of pairs is given. In each pair, there are two integers and the first integer is always smaller, and second one is greater, the same rule can also be applied for the chain construction. A pair (x, y) can be added after a pair (p, q), only if q < x.To solve this problem, at first we have to sort given pairs in increasing order of first element. After that we will compare the second element of a pair, with the first element of next pair.Input − A chain of number pairs. {(5, 24), (15, 25), ... Read More

Python Program to Print Matrix in Z form

Pavitra
Updated on 25-Sep-2019 12:26:13

389 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given a square matrix of order n*n, we need to display elements of the matrix in Z form.Z form is traversing the matrix in the following steps −Traverse the first rowNow, traverse the second principal diagonalFinally, traverse the last row.We will take an input matrix here implicitly taken to demonstrate the flow of code.demostrateExample Live Demoarr = [[1, 2, 6, 9],    [1, 2, 3, 1],    [7, 1, 3, 5],    [1, 8, 7, 5]] n = len(arr[0]) i = 0 ... Read More

Python Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!

Pavitra
Updated on 25-Sep-2019 12:21:44

3K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given an integer input n, we need to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!Here we are implementing for loop, therefore, we get O(n) as the time complexity.Here to reach the efficiency we calculate factorial within the same loop.Here we frame a sumofseries function as described below −Example Live Demodef sumOfSeries(num):    res = 0    fact = 1    for i in range(1, num+1):       fact *= i       res ... Read More

Matrix Chain Multiplication (A O(N^3) Solution) in C++

Arnab Chakraborty
Updated on 02-Dec-2024 11:54:42

3K+ Views

If a chain of matrices is given, we have to find a minimum number of correct sequences of matrices to multiply. We know that the matrix multiplication is associative, so for four matrices ABCD, we can multiply A(BCD), (AB)(CD), (ABC)D, and A(BC)D, in these sequences. Like these sequences, our task is to find which ordering is efficient to multiply.ExampleIn the given input there is an array say arr, which contains arr[] = {1, 2, 3, 4}. It means the matrices are of the order (1 x 2), (2 x 3), (3 x 4). Input − The ... Read More

Python Program to find the area of a circle

Pavitra
Updated on 25-Sep-2019 12:18:21

767 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given the radius of a circle, we need to find a circle.The area of a circle can simply be evaluated using the following formula.Area = Pi*r*rLet’s see the implementation below −Example Live Demodef findArea(r):    PI = 3.142    return PI * (r*r); # Driver method print("Area is %.6f" % findArea(5));OutputArea is 78.550000All variables and functions are declared in the global scope as shown in the figure below.ConclusionIn this article, we learned about the approach to find whether it is possible to make ... Read More

Tribonacci Word in C++

Arnab Chakraborty
Updated on 25-Sep-2019 12:17:14

228 Views

The Tribonacci Word is a sequence of digits. This is similar to the Fibonacci Words. Tribonacci Word is constructed by repeated concatenation of three previous stringsT(n) = T(n - 1) + T(n - 2) + T(n - 3)The first few strings to start, are {1, 12, 1213} So the next one will be 1213 + 12 + 1 = 1213121Algorithmtribonacci_word(n): Begin    first := 1, second := 12, third := 1213    print first, second, third    for i in range 3 to n, do       temp := third       third := third + second + ... Read More

Advertisements