Programming Articles

Page 1291 of 2547

Maximum length of subarray such that sum of the subarray is even in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 997 Views

We are given with an array Arr[] of integers. The goal is to find longest length subarray of Arr[] , sum of whose elements is even. That is, the sum of elements of a subarray is even and that subarray is longest in length.Input − Arr[] = { 2, 3, 5, 2, 6, 7 }.Output −Maximum length of subarray − 4Explanation −The maximum length subarray is { 5, 2, 6, 7 }. Sum is 20 which is even.Input − Arr[] = { 5, 7, 7, 3, 4 }.Output − Maximum length of subarray − 4Explanation − The maximum length subarray ...

Read More

Count number of right triangles possible with a given perimeter in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 409 Views

We are given a perimeter P of a triangle. Perimeter is the sum of all sides of the triangle. The goal is to find the number of right triangles that can be made which have the same perimeter.If the sides of the triangle are a, b and c. Then a + b + c = P and a2 + b2 = c2 ( pythagoras theorem for any combination of a, b, and c )We will check this by taking a from 1 to p/2 and b from a+1 to p/3. Then c = p-a-b (a+b+c=p)For all right triangles, apply Pythagoras ...

Read More

How to create two vertical lines on a plot with shaded area in-between using R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 439 Views

Sometimes we want to place a vertical rectangle on a plot that has different color as compared to the rest of the plot area. This vertical rectangle is created based on the conditional values of x axis and represent the pivot area or unimportant area depending on the characteristics of the data. These values of x variable are placed as vertical lines on the plot and the area between these lines is shaded. It can be done by using geom_rect function.ExampleConsider the below data frame −> x y df dfOutput  x y 1 2 9 2 3 7 3 1 ...

Read More

Maximum sum path in a matrix from top to bottom in C++ Program

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

In this tutorial, we will be discussing a program to find maximum sum path in a matrix from top to bottom.For this we will be provided with a matrix of N*N size. Our task is to find the maximum sum route from top row to bottom row while moving to diagonally higher cell.Example#include using namespace std; #define SIZE 10 //finding maximum sum path int maxSum(int mat[SIZE][SIZE], int n) {    if (n == 1)       return mat[0][0];    int dp[n][n];    int maxSum = INT_MIN, max;    for (int j = 0; j < n; j++)   ...

Read More

Count number of solutions of x^2 = 1 (mod p) in given range in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 208 Views

We are given with integers x and p. The goal is to find the number of solutions of the equation −x2=1 ( mod p ) such that x lies in range [1, N].We will do this by traversing from 1 to N and take each number as x check if (x*x)%p==1. If yes then increment the count.Let’s understand with examples.Input − n=5, p=2Output − Number of Solutions − 3Explanation − Between the range 1 to 5.12=1%2=1, count=1 22=4%2=0, count=1 32=9%2=1, count=2 42=16%2=0, count=2 52=25%2=1, count=3 Total number of solutions=3.Input − n=3, p=4Output − Number of Solutions − 2Explanation − Between ...

Read More

How to check whether the elements of a vector are arranged in an increasing order or decreasing order?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 1K+ Views

A vector can contain values that are increasing or decreasing in nature or they can be also random which means a higher value may come after a lower one which is followed by a higher value. An example of increasing arrangement of elements of vector is 1, 2, 3 and the opposite of that would be decreasing arrangement. We can check whether a vector is arranged in increasing order or decreasing order by checking whether the difference between all values of the vector is greater than or equal to zero or not and it can be done by using diff ...

Read More

Maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array in C++

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

In this tutorial, we will be discussing a program to find maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array.For this we will be provided with an array containing N intergers and a value K. Our task is to find the maximum sum of the subsequence including elements not nearer than K.Example#include using namespace std; //returning maximum sum int maxSum(int* arr, int k, int n) {    if (n == 0)       return 0;    if (n == 1)       return arr[0];    if ...

Read More

Count number of step required to reduce N to 1 by following certain rule in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 447 Views

We are given a number N. The goal is to count the number of steps required to reduce the number to 1 by following rules −If the number is power of 2, reduce it to its half.Else reduce it to the N-(nearest power of 2 which is less than N).For step 1, we will check if N is power of 2, by checking if ceil(log2(N)), floor(log2(N)) return the same result. If yes then N=N/3, increment count of operation.If the result of step 1 is false then we will perform step 2 and subtract the nearest power of 2 less than ...

Read More

How to extract p-values for intercept and independent variables of a general linear model in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 519 Views

General linear model does not assume that the variables under consideration are normally distributed, therefore, we can use other probability distributions to create a general linear model. We should actually say that if the data does not follow normal distribution then we can try different distributions using general linear model and check whether the model is appropriate or not. The p-values plays an important role in selecting the best model and we might want to extract them from the model object. This can be done by using coef function.ExampleConsider the below data frame −> set.seed(123) > var1 var2 var3 Response ...

Read More

Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1<=a<=b<=c<= n in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 628 Views

We are given an integer n. The goal is to find triplets ( set of 3 numbers ) that satisfy the conditions −a2+b2=c21

Read More
Showing 12901–12910 of 25,466 articles
Advertisements