Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles on Trending Technologies
Technical articles with clear explanations and examples
Count all sub-arrays having sum divisible by k
In this tutorial, we will be discussing a program to find the number of sub-arrays having sum divisible by k.For this we will be provided with an array and a value k. Our task is to find the number of sub arrays that are having their sum as equal to the given value k.Example#include using namespace std; //counting subarrays with k sum int count_subarray(int arr[], int n, int k){ int mod[k]; memset(mod, 0, sizeof(mod)); int cumSum = 0; for (int i = 0; i < n; i++) { cumSum += arr[i]; ...
Read MoreGreatest Sum Divisible by Three in C++
Suppose we have an array nums of integers, we need to find the maximum possible sum of elements of the given array such that it is divisible by three. So if the input is like [3, 6, 5, 1, 8], then the output will be 18, as the subarray is [3, 6, 1, 8], and the sum is 18, that is divisible by 3.To solve this, we will follow these steps −n := size of nums arraycreate one 2d array dp of size (n + 1) x 3set dp[0, 0] := 0, dp[0, 1] := -inf, dp[0, 2] := inffor ...
Read MoreWrite your own strcmp that ignores cases in C++
Here, we have to create a strcmp (string compare) function that compares two string but ignores cases of the characters of the string. The function will return -1 if string1 < string2, 0 if string1 = string2, 1 if string1 > string2.Let’s take an example to understand the problem, Inputstring1 = “Hello” , string2 = “hello”Output0To create our own strcmp function that ignores cases while comparing the strings. We will iterate through all characters of both the strings, if characters at ith index are the same i.e. string1[i] == string2[i], continue. If string1[i] > string2[i], return 1. If string1[i] < ...
Read MoreNumber of Burgers with No Waste of Ingredients in C++
Suppose we have two integers tomatoSlices and cheeseSlices. These are the ingredients of different burgers −Jumbo Burger: 4 tomato slices and 1 cheese slice.Small Burger: 2 Tomato slices and 1 cheese slice.We have to find [total_jumbo, total_small] so that the number of tomatoSlices that are left is equal to 0 and the number of cheeseSlices that are left is also 0. If it is not possible to make the remaining tomatoSlices and cheeseSlices equal to 0 return []. So if the input is tomatoSlices = 16 and chesseSlices = 7, then the output will be [1, 6]. So this indicates, ...
Read MoreCount all Quadruples from four arrays such that their XOR equals to ‘x’ in C++
In this tutorial, we will be discussing a program to find the number of quadruples from four arrays such that their XOR equals to x.For this we will be provided with four arrays and a value x. Our task is to count all the quadruples whose XOR is equal to the given value x.Example#include using namespace std; //counting quadruples with XOR equal to x int count_quad(int a[], int b[], int c[], int d[], int x, int n){ int count = 0; for (int i = 0 ; i < n ; i++) for (int j ...
Read MoreCount Square Submatrices with All Ones in C++
Suppose we a binary matrix, of size m x n. We have to count number of square submatrices, with all 1s. So if the matrix is like −011111110111So there will be 15 squares. 10 squares of single ones, 4 squares of four ones, and 1 square with nine ones.To solve this, we will follow these steps −set ans := 0, n := row count and m := column countfor i in range 0 to m – 1ans := ans + matrix[n – 1, i]for i in range 0 to n – 1ans := ans + matrix[i, m – 1]ans := ...
Read MoreCount all prefixes of the given binary array which are divisible by x in C++
In this tutorial, we will be discussing a program to find the number of prefixes of the binary array which are divisible by x.For this we will be provided with binary array and a value x. Our task is to find the number of elements whose prefixes are divisible by given value x.Example#include using namespace std; //counting the elements with prefixes //divisible by x int count_divx(int arr[], int n, int x){ int number = 0; int count = 0; for (int i = 0; i < n; i++) { number = number * ...
Read MoreGroup the People Given the Group Size They Belong To in C++
Suppose there are n people whose IDs are in range 0 to n - 1 and each person belongs exactly to one group. We have the array groupSizes of length n. This array is indicating that the group size each person belongs to, we have to find the groups there are and the people's IDs each group includes.Suppose the input is like − [3, 3, 3, 3, 3, 1, 3], then the output is [[5], [0, 1, 2], [3, 4, 6]], Other possible solutions can be [[2, 1, 6], [5], [0, 4, 3]] or [[5], [0, 6, 2], [4, 3, ...
Read MoreCount all possible walks from a source to a destination with exactly k edges in C++
In this tutorial, we will be discussing a program to find the number of walks from a source to a destination with exactly k edges.For this we will be provided with a graph and the values of source and destination. Our task is to find all the possible paths starting from the source to the destination having exactly k edges.Example#include using namespace std; #define V 4 //counting walks using recursion int countwalks(int graph[][V], int u, int v, int k){ if (k == 0 && u == v) return 1; if (k == 1 && graph[u][v]) return 1; if (k
Read MoreMinimum Remove to Make Valid Parentheses in C++
Suppose we have a string s of '(' , ')' and lowercase English characters. We have to remove the minimum number of parentheses ( '(' or ')', in any positions ) so that the resulting parenthese string is valid and return any valid string. A parentheses string is valid when all of these criteria are fulfilled −It is the empty string, contains lowercase characters only, orIt can be written as the form of AB (A concatenated with B), where A and B are valid strings, orIt can be written as the form of (A), where A is a valid string.So ...
Read More