Ayush Gupta

Ayush Gupta

433 Articles Published

Articles by Ayush Gupta

Page 36 of 44

C++ program to check whether it is possible to finish all the tasks or not from given dependencies

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 222 Views

In this article, we will be discussing a program to check if it is possible to finish all the given tasks on the basis of the given prerequisites.For example, let us say we have been given three tasks and prerequisites are [[1, 0], [2, 1], [3, 2]].( [1, 0] means that to pick up ‘1’ task; the ‘0’ task must be completed first.)Then, in this example since the ‘0’ task doesn’t have any prerequisite it can be completed first. Then the ‘1’ task can be completed, since the ‘0’ task has been completed. Similarly, both ‘2’ and ‘3’ tasks can ...

Read More

C++ program for Finite Automata algorithm for Pattern Searching

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 1K+ Views

In this article, we will be discussing a program to execute the Finite Automata algorithm for pattern searching.We are provided with a text[0...n-1] and a pattern[0...m-1]. We have to find all the occurrences of the pattern[] in the text[].For this we would preprocess the text[] and build a 2-d array to represent it. After that we just have to traverse between the elements of the text[] and the different states of the automata.Example#include #include #define total_chars 256 int calc_nextstate(char *pat, int M, int state, int x) {    if (state < M && x == pat[state])       return ...

Read More

C++ program to find 'k' such that its modulus with each array element is same

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 398 Views

In this article, we will be discussing a program to find an integer ‘k’, such that its modulus with each element of a given array is the same.For example, let us suppose we have been given with an array, arr = {12, 22, 32}Then we have the output value of k = 1, 2, 5, 10.Take the case of two values in the array ‘x’ and ‘y’ (x>y). Then we have (y+difference)%k = y%k. Solving this we get, difference%k = 0So, we will find all the divisors to the difference of the maximum and minimum element in the array and ...

Read More

C++ program to find first collision point of two series

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 248 Views

In this article, we will be discussing a program to find the first collision point i.e the first point that both of the series have.In this, we would be given with five variables ‘a’, ‘b’, ‘c’, ‘d’ and ‘n’. We have to create two arithmetic progression series from these having n digits eachb, b+a, b+2a, ….b+(n-1)a d, d+c, d+2c, ….. d+(n-1)cAnd then find the first common point that both of the given series has.To solve this, we would create the numbers in the first series. And for each number we would check if it is greater than or equal to ...

Read More

C++ program to find minimum vertex cover size of a graph using binary search

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 856 Views

In this article, we will be discussing a program to find the minimum vertex cover size of a given graph using binary search.Minimum vertex cover is a set of vertices of the given graph such that every edge in the graph is incident of either of the vertices in that set.For example, take the graph2 ---- 4 ---- 6 |     | | | | | 3 ---- 5Here, the minimum vertex cover involves vertices 3 and 4. All the edges of the graphs are incident on either 3 or 4 ...

Read More

C++ program to find sum of digits of a number until sum becomes single digit

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 708 Views

In this article, we will be discussing a program to find the sum of digits of a number until the sum itself becomes a single digit and cannot be done summation of further.For example, take the case of a number 14520. Adding the digits of this number we get 1 + 4 + 5 + 2 + 0 = 12. Since this is not a single digit number, we would further add the digits of the number received. Adding them we get, 1 + 2 = 3.Now, 3 is the final answer because it is a single digit number itself ...

Read More

C++ program to find the best fit rectangle that covers a given point

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 337 Views

In this article, we will be discussing a program to find the best fit rectangle that covers a given point.In this problem, we are given we the coordinates of a point (x, y) and a ratio of length/breadth = l/b (say). We have to find the coordinates of a rectangle which contains the given point and whose dimensions follow the given ratio. In case of multiple rectangle existing, we have to choose the one having the shortest distance between its euclid’s center and the given point.To solve this, first we would minimize the ratio l/b. After that, we find the ...

Read More

C++ program to find the first digit in product of an array of numbers

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 187 Views

In this article, we will be discussing a program to find the first digit in the product of the elements of the given array.For example, let us say we have been given an array.arr = {12, 5, 16}Then the product is of these elements would be 12*5*16 = 960. Therefore, the result i.e the first digit of the product in this case would be 9.Example#include using namespace std; int calc_1digit(int arr[], int x) {    long long int prod = 1;    for(int i = 0;i < x; i++) {       prod = prod*arr[i];    }    while (prod >= 10)       prod = prod / 10;    return prod; } int main() {    int arr[]={12,43,32,54};    cout

Read More

C++ program to find the number of triangles amongst horizontal and vertical line segments

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 211 Views

In this article, we will be discussing a program to find the number of triangles that can be formed by joining the intersection points of the given horizontal and vertical line segments.For example, let us say we had been given the following line segments. In this we have 3 intersection points. So the number of triangles that can be formed using these points will be 3C2.   | ---|--------|--    |        |    |  --|---|    |        |We will be following the Sweep Line Algorithm. We will be storing all the values of the line ...

Read More

C++ program to find the Parity of a number efficiently

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 2K+ Views

In this article, we will be discussing a program to find the parity of a given number N.Parity is defined as the number of set bits (number of ‘1’s) in the binary representation of a number.If the number of ‘1’s in the binary representation are even, the parity is called even parity and if the number of ‘1’s in the binary representation is odd, the parity is called odd parity.If the given number is N, we can perform the following operations.y = N ^ (N >> 1)y = y ^ (y >> 2)y = y ^ (y >> 4)y = ...

Read More
Showing 351–360 of 433 articles
« Prev 1 34 35 36 37 38 44 Next »
Advertisements