Found 7197 Articles for C++

Magic Squares In Grid in C++

Arnab Chakraborty
Updated on 04-Jul-2020 10:12:01

395 Views

Suppose we have a grid we have to find the number of magic square sub-grid into that grid. A magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, column, and both diagonals all have the same sum.So, if the input is like438495192762then the output will be 1, as the magic square is438951276To solve this, we will follow these steps −Define one set with values: [816357492, 834159672, 618753294, 672159834, 492357816, 438951276, 294753618, 276951438]Define an array offset of size: 9 x 2 := {{-2, -2}, {-2, -1}, {-2, 0}, {-1, -2}, ... Read More

Degree of an Array in C++

Arnab Chakraborty
Updated on 04-Jul-2020 09:43:10

628 Views

Suppose we have an array of non-negative integers called nums, the degree of this array is actually the maximum frequency of any one of its elements. We have to find the smallest possible length of a contiguous subarray of nums, that has the same degree as nums.So, if the input is like [1, 2, 2, 3, 1], then the output will be 2, this is because the input array has a degree of 2 because both elements 1 and 2 appear twice. The subarrays that have the same degree − [1, 2, 2, 3, 1], [1, 2, 2, 3], [2, ... Read More

Count Binary Substrings in C++

Arnab Chakraborty
Updated on 04-Jul-2020 09:41:25

794 Views

Suppose we have a string s, we have to find the count of contiguous substrings that have the same number of 0's and 1's, and all the 0's and all the 1's in these substrings are grouped consecutively. If substrings occur multiple times are counted the number of times they occur.So, if the input is like "11001100", then the output will be 6, as the substrings are "1100", "10", "0011", "01", "1100", "10".To solve this, we will follow these steps −Define an array cnt of size 2 and fill this with 0res := 0for initialize i := 0, when i ... Read More

Binary Number with Alternating Bits in C++

Arnab Chakraborty
Updated on 04-Jul-2020 09:39:16

461 Views

Suppose we have a positive integer, we have to check whether it has alternating bits − so, two adjacent bits will always have different values.So, if the input is like 10, then the output will be True, as binary representation of 10 is 1010.To solve this, we will follow these steps −p := n AND 1if n < 2, then −return truen := n/2while n is non-zero, do −c := n AND 1if c XOR p is same as 0, then −return falsep := cn := n/2return trueLet us see the following implementation to get better understanding −Example Live Demo#include ... Read More

Longest Continuous Increasing Subsequence in C++

Arnab Chakraborty
Updated on 04-Jul-2020 09:34:52

845 Views

Suppose we have an array of integers; we have to find the length of longest continuous increasing subarray.So, if the input is like [2,4,6,5,8], then the output will be 3. As the longest continuous increasing subsequence is [2,4,6], and its length is 3.To solve this, we will follow these steps −if size of nums

Second Minimum Node In a Binary Tree in C++

Arnab Chakraborty
Updated on 04-Jul-2020 09:33:20

602 Views

Suppose there is a non-empty special binary tree with some non-negative value, here each node in this tree has exactly two or zero children. If the node has two children, then this node's value is the smaller value among its two children. In other words, we can say that [root.val = minimum of root.left.val, root.right.val]. If we have such binary tree, we have to find the second minimum value in the set made of all the nodes' value in the whole tree. If there is no such element, then return -1 instead.So, if the input is likethen the output will ... Read More

Maximum size rectangle binary sub-matrix with all 1s in C++

Ayush Gupta
Updated on 03-Jul-2020 08:06:57

196 Views

In this tutorial, we will be discussing a program to find maximum size rectangle binary sub-matrix with all 1s.For this we will be provided with 2D matrix containing zeroes and ones. Our task is to find the largest 2D matrix subset containing only ones.Example Live Demo#include using namespace std; #define R 4 #define C 4 //finding the maximum area int maxHist(int row[]) {    stack result;    int top_val;    int max_area = 0;    int area = 0;    int i = 0;    while (i < C) {       if (result.empty() || row[result.top()]

Maximum size of sub-array that satisfies the given condition in C++

Ayush Gupta
Updated on 03-Jul-2020 08:02:35

189 Views

In this tutorial, we will be discussing a program to find maximum size of sub-array that satisfies the given condition.For this we will be provided with an array of integers. Our task is to find the maximum length subset of that array satisfying either of arr[k] > arr[k + 1] when k is odd and arr[k] < arr[k + 1] when k is even, arr[k] > arr[k + 1] when k is even and arr[k] < arr[k + 1] when k is odd.Example Live Demo#include using namespace std; //comparing values of a and b int cmp(int a, int b) {   ... Read More

Maximum set bit sum in array without considering adjacent elements in C++

Ayush Gupta
Updated on 15-Sep-2020 14:11:22

189 Views

In this problem, we are given an array arr[] of integers. Our task is to create a program to calculate the Maximum set bit sum in array without considering adjacent elements in C++.Problem description − Here, we have an array arr[]. We have to find the number of set bits for each number. Then, we will find the maximum set bit sum in adjacent elements of the array. i.e. maximum sum for a[i] + a[i+2] ….Let’s take an example to understand the problem, Inputarr[] = {1, 4, 6, 7}Output4ExplanationArray with the element’s in binary formarr[] = {01, 100, 101, 111} ... Read More

Maximum score after flipping a Binary Matrix atmost K times in C++

Ayush Gupta
Updated on 15-Sep-2020 14:12:56

319 Views

In this problem, we are given a 2-D array arr[] consisting of boolean values (i.e 0’s and 1’s) and an integer K. Our task is to create a program to find the Maximum score after flipping a Binary Matrix atmost K times in C++.Problem Description − Here, for the 2-D array, and the k moves, we need to find the number that is created by the elements of the array. In each move, we will take a row or column and flip all the elements of the row or column. The choice will be done to keep in mind that ... Read More

Advertisements