Programming Articles - Page 764 of 3363

Find the number of solutions to the given equation in C++

sudhir sharma
Updated on 11-Feb-2022 11:20:12

480 Views

In this problem, we are given three integer values A, B, C. Our task is to find the number of solutions to the given equation.EquationX = B*Sm(X)^A + Cwhere Sm(X) is the sum of digits of X.We need to count all the values of X such that it satisfies the above equation where X can be any number between 1 to 109.Let’s take an example to understand the problem, InputA = 3, B = 6, C = 4Output3Solution ApproachA solution to solve the problem is by counting the number of values of X. For this, the sum of digits plays ... Read More

Find the number of Islands Using Disjoint Set in C++

sudhir sharma
Updated on 11-Feb-2022 10:58:15

623 Views

In this problem, we are given a 2D binary matrix. Our task is to find the number of islands Using DFS.Island is a ground of 1 or more connected 1’s in the matrix.Let’s take an example to understand the problem, Inputbin[][] = {{ 1 0 0 0}    {0 1 0 1}    {0 0 0 0}    {0 0 1 0}}Output3 ExplanationIslands are :    bin00 - bin11    bin13    bin32Solution ApproachTo find the island from a binary matrix using a disjoint set data structure. To find island count, we will traverse the matrix and do union of ... Read More

Find the number of islands Using DFS in C++

sudhir sharma
Updated on 11-Feb-2022 10:27:51

361 Views

In this problem, we are given a 2D binary matrix. Our task is to Find the number of islands Using DFS.Island is a ground of 1 or more connected 1’s in the matrix.Let’s take an example to understand the problem, Input : bin[][] = {{ 1 0 0 0}    {0 1 0 1}    {0 0 0 0}    {0 0 1 0}} Output : 3 ExplanationIslands are −bin00 - bin11bin13bin32 Solution ApproachTo solve the problem using DFS, we will use the DFS technique for exploring all the neighbours (maximum possible 8 of a number in the matrix) and ... Read More

Program to find Nth Even Fibonacci Number in C++

sudhir sharma
Updated on 11-Feb-2022 07:17:27

635 Views

In this problem, we are given an integer value N. Our task is to find Nth Even Fibonacci Number.Fibonacci Series generates subsequent number by adding two previous numbers. The Fibonacci series starts from two numbers − F0 & F1. The initial values of F0 & F1 can be taken 0, 1 or 1, 1 respectively.Let’s take an example to understand the problem, Input : N = 4 Output : 144Solution ApproachA simple solution to the problem is using the fact that every third number in the fibonacci sequence is even and the sequence of even numbers also follows the recursive ... Read More

numpy.matrix Method in Python

Syed Abeed
Updated on 11-Feb-2022 06:41:54

224 Views

The numpy.matrix method is used to interpret a given input as a matrix. It returns a matrix from an array-like object. Its syntax is as follows −numpy.matrix(data, dtype=None, copy=bool)where, data - It is the input data.dtype - It represents the data type of the output matrix.copy - If the input data is already an ndarray, then this flag copy determines whether the data is to be copied (default behavior), or whether a view is to be constructed.Example 1Let us consider the following example −# import numpy library import numpy as np # matrix function y = np.matrix([[4, 5], [7, ... Read More

numpy.vander Method in Python

Syed Abeed
Updated on 11-Feb-2022 06:34:18

322 Views

The numpy.vander() method is used to generate a Vandermonde (Vander) matrix. A Vander matrix contains a geometric progression in each row, for example, $$\mathrm{A =\begin{bmatrix}1 & 2 & 4 \1 & 3 & 9 \1 & 5 &25\end{bmatrix} or\: B = \begin{bmatrix}1 & 4 & 16 \1 & 6 &36 \end{bmatrix}}$$SyntaxIts syntax is as follows −numpy.vander(x, N=None, increasing=False)ParametersIt accepts the following parameters −x - This is the input array.N - It is the number of columns in the output. By default, it is None.Increasing - If increasing=True, then the power increases from left to right. If increasing=False, then powers are ... Read More

numpy.triu Method in Python

Syed Abeed
Updated on 11-Feb-2022 06:38:25

512 Views

The numpy.triu() method can be used to get the upper triangle of an array. Its syntax is as follows −Syntaxnumpy.triu(m, k=0)where, m - number of rows in the array.k - It is the diagonal. Use k=0 for the main diagonal. k < 0 is below the main diagonal and k > 0 is above it.It returns a copy of the array after replacing all the elements above the kth diagonal with zero.Example 1Let us consider the following example −# import numpy library import numpy as np # create an input matrix x = np.matrix([[6, 7], [8, 9], [10, 11]]) ... Read More

numpy.tril Method in Python

Syed Abeed
Updated on 11-Feb-2022 06:29:46

310 Views

We can use the numpy.tril() method to get the lower triangle of an array. Its syntax is as followsSyntaxnumpy.tril(m, k=0)where, m - number of rows in the array.k - It is the diagonal. Use k=0 for the main diagonal. k < 0 is below the main diagonal and k > 0 is above it.It returns a copy of the array after replacing all the elements above the k thdiagonal with zero.Example 1Let us consider the following example −# import numpy library import numpy as np # create an input matrix x = np.matrix([[20, 21, 22], [44 ,45, 46], [78, ... Read More

numpy.tri Method in Python

Syed Abeed
Updated on 11-Feb-2022 05:59:23

338 Views

The numpy.tri method can be used to get an array of 1's at and below a given diagonal and 0's elsewhere.Syntaxnumpy.tri(N, M=None, k=0, dtype=)Parametersnumpy.tri accepts the following parameters −N - It defines the number of the rows in an array.M - It defines the number of columns in an array. By default, it is None.k - Use k = 0, for the main diagonal, while k < 0 is below it and k > 0 is above it.dtype - It is data type of the returned array. By default, it is float.Example 1Let us consider the following example −# import ... Read More

Join a sequence of arrays with stack() over specific axis in Numpy

AmitDiwan
Updated on 10-Feb-2022 10:11:08

459 Views

To join a sequence of arrays, use the numpy.stack() method in Python Numpy. The axis parameter specifies the index of the new axis in the dimensions of the result. If axis=0 it will be the first dimension and if axis=-1 it will be the last dimension.The function returns the stacked array has one more dimension than the input arrays. The axis parameter specifies the index of the new axis in the dimensions of the result. For example, if axis=0 it will be the first dimension and if axis=-1 it will be the last dimension.The out parameter, if provided, the destination ... Read More

Advertisements