C++ Articles

Page 7 of 597

Largest set with bitwise OR equal to n in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 134 Views

In this tutorial, we are going to write a program that finds the largest set with bitwise OR is equal to the given number n.Let's see the steps to solve the problem.Initialise the number n.Write a loop that iterates from 0 to n.If the i | n is equal to n, then add i to the result.Return the result.ExampleLet's see the code.#include using namespace std; void printBitWiseOrSet(int n) {    vector v;    for (int i = 0; i

Read More

Largest subarray having sum greater than k in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 438 Views

In this tutorial, we are going to write a program that finds the largest subarray have sum greater than k.Let's see the steps to solve the problem.Initialise the array.Iterate over the array and store sum at each index in a vector along with the index.Sort the above sums based on sum and index.Initialise an array to store the indexes.Write a loop that iterates till n.Update the values with min index of above indexes array and previous sums array index.Initialise sum to 0.Write a loop that iterates till n.Add current element to sum.If the sum is greater than k.The maximum subarray ...

Read More

Largest subarray with equal number of 0s and 1s in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 243 Views

Let's see the steps to complete the program.Initialise the array.Make all zeroes in the array to -1.Have a map an empty map to store the previous indexes.Initialise sum to 0, max length to 0 and ending index to -1.Write a loop that iterates till n.Add current element to sum.If the sum is equal to 0.Update the max length with i + 1.And ending index to i.If the sum is present in previous sums map and i - previousIndexes[sum] is greater than max length.Update the max length and ending index.Else add the sum to the previous indexes map.Print the starting index ...

Read More

Largest sum subarray with at-least k numbers in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 444 Views

Let's see the steps to complete the program.Initialise the array.Initialise max_sum array of size n.Find the max sum for every index and store it in max_sum array.Compute the sum of all the elements and store it in a variable sum.Write a loop that iterates from i = k to n.Add a[i] - a[i - k] to the sum.Update the result with max of result, sum.Update the result with max of result, sum + max_sum[i - k].ExampleLet's see the code.#include using namespace std; int getMaxSum(int a[], int n, int k) {    int maxSum[n];    maxSum[0] = a[0];    int currentMax ...

Read More

Find shortest safe route in a path with landmines in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 957 Views

In this problem, we are given a matrix mat[][]. It defines a path with landmines which are marked as 0. Our task is to Find shortest safe route in a path with landmines.While traversing through the safe path, we need to avoid walking adjacent cells of the landmine (left, right, above and bottom) for being unsafe.All valid moves while traversing the path are −- Left : mat[i][j] => mat[i-1][j] - Right : mat[i][j] => mat[i+1][j] - Top : mat[i][j] => mat[i][j - 1] - Bottom : mat[i][j] => mat[i][j + 1]Let’s take an example to understand the problem, Inputmat[][] = ...

Read More

Find Maximum side length of square in a Matrix in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 326 Views

In this problem, we are given a 2-D matrix mat[][] of size n, n being an odd number. Our task is to Find Maximum side length of a square in a Matrix.Problem Description − We need to find the length of the square matrix whose perimeter values are the same and it shares the same center as the matrix.Let’s take an example to understand the problem, Inputmat[][] = {    {2, 4, 6, 6, 5},    {1, 7, 7, 7, 3},    {5, 7, 0, 7, 1},    {3, 7, 7, 7, 1},    {2, 0, 1, 3, 2} }Output3Solution ...

Read More

Find Maximum value of abs(i – j) * min(arr[i], arr[j]) in an array arr[] in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 579 Views

In this problem, we are given an array arr[] insisting of N integer values.Our task is to Find Maximum value of abs(i – j) * min(arr[i], arr[j]) in an array arr[].Problem description − we need to find the maximum product value of the minimum value of two elements and the absolute differences between their indexes. i.e. for two values i and j, we need to maximise, abs(i - j) * min(arr[i] , arr[j]).Inputarr[] = {5, 7, 3, 6, 4}Output16ExplanationThe maximum value is 16, between index 0 and 4 => abs(0 - 4)*min(arr[0], arr[4]) => 4*min(5, 4) => 4*4 = 16Solution ...

Read More

Find mean of subarray means in a given array in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 215 Views

In this problem, we are given an array arr[] of size n and an integer m. Our task is to Find mean of subarray means in a given array.Code Description − Here, we need to find the mean of array as the mean of means of subarray of size m.Let’s take an example to understand the problem, Inputarr[] = {2, 5, 3, 6, 1}, m = 3Output3.78ExplanationAll subarrays of size m are {2, 5, 3}, {5, 3, 6}, {3, 6, 1} Means of means of subarray of size m, $$(\left(\frac{2+5+3}{3}\right)+\left(\frac{5+3+6}{3}\right)+\left(\frac{3+6+1}{3}\right))/3=\left(\frac{10}{3}\right)+\left(\frac{14}{3}\right)+\left(\frac{10}{3}\right)/3=34/3/3=3.78$$Solution ApproachA simple solution to the problem is by finding all ...

Read More

Find middle point segment from given segment lengths in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 228 Views

In this problem, we are given an array arr[] of size m representing the lengths of line segments.The line segments are from 0 to arr[0] , arr[0] to arr[1] and so on. Our task is to find the segment which is at the middle of all segments.Let’s take an example to understand the problem, Inputarr[] = {5, 7, 13}Output3ExplanationSegments are : (0, 5) , (5, 12), (12, 25)Solution ApproachTo solve the problem, we will find the middle point of the line by (arrSum/2). If this middle point is a starting or ending point of a line segment then print -1. ...

Read More

Find minimum in an array without using Relational Operators in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 251 Views

In this problem, we are given an array arr[] consisting of n positive elements. Our task is to find minimum in an array without using Relational Operators.Relational operators in programming are those operators which are used to check the relationship between two values. Like == (equals), greater than (>), less than (

Read More
Showing 61–70 of 5,961 articles
« Prev 1 5 6 7 8 9 597 Next »
Advertisements