Articles on Trending Technologies

Technical articles with clear explanations and examples

Bitwise AND of sub-array closest to K in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 1K+ Views

In this problem, we are given an array arr[] of size n and an integer k. Our task is to find the subarray within from index i to j and compute bitwise AND of all its elements. After this print minimum value of abs(K- (bitwise AND of subarray)).Let’s take an example to understand the problem, Input − arr[] = {5, 1}, k = 2Output −To solve the problem, there can be a few methods.One simple solution will be using the direct method. By finding bitwise AND for all sub-arrays then finding the |K-X|.Step 1 − Find the bitwise AND for ...

Read More

Maximum students to pass after giving bonus to everybody and not exceeding 100 marks in C++

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

In this tutorial, we will be discussing a program to find maximum students to pass after giving bonus to everybody and not exceeding 100 marks.For this we will be provided with an array containing marks of N students. Our task is to get more student pass the exam (50 marks required) by giving each student the same amount of bonus marks without any student exceeding 100 marks.Example#include #include using namespace std; int check(int n, int marks[]) {    int* x = std::max_element(marks, marks+5);    int bonus = 100-(int)(*x);    int c = 0;    for(int i=0;    i= 50) c ...

Read More

Find multiplication of sums of data of leaves at same levelss in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 226 Views

ConceptWith respect of a given Binary Tree, return following value for it.With respect of every level, calculate sum of all leaves if there are leaves at this level. Else ignore it.Calculate multiplication of all sums and return it.InputRoot of following tree       3      / \     8   6          \           10Output80First level doesn’t have leaves. Second levelhas one leaf 8 and third level also has one leaf 10. So result is 8*10 = 80InputRoot of following tree              3     ...

Read More

Maximum subarray size, such that all subarrays of that size have sum less than k in C++

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

In this tutorial, we will be discussing a program to find maximum subarray size, such that all subarrays of that size have sum less than k.For this we will be provided with an array of size N and an integer k. Our task is to find the length of subarray such that all the sub-arrays of that length in the given array sum to be less than or equal to k.Example#include using namespace std; //finding maximum length subarray int bsearch(int prefixsum[], int n, int k) {    int ans = -1;    //performing binary search    int left = 1, ...

Read More

Word Squares in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 403 Views

Suppose we have a set of words (all are unique), we have to find all word squares and we can build from them. Here a sequence of words forms a valid word square if the kth row and column read the exact same string, where 0 ≤ k < maximum of numRows and numColumns. So as an example, the word sequence ["ball", "area", "lead", "lady"] will construct a word square because each word reads the same both horizontally and vertically.ballarealeadladySo, if the input is like ["area", "lead", "wall", "lady", "ball"], then the output will be [[ "wall", "area", "lead", "lady"], ...

Read More

Java Program to find the vertex, focus and directrix of a parabola

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 261 Views

Following is the Java program to find the vertex, focus and directrix of a parabola −Examplepublic class Demo{    public static void find_values(float val_1, float val_2, float val_3){       System.out.println("The value of vertex is (" + (-val_2 / (2 * val_1)) + ", "+ (((4 * val_1 * val_3) - (val_2 * val_2)) / (4 * val_1)) + ")");       System.out.println("The value of focus is (" + (-val_2 / (2 * val_1)) + ", " + (((4 * val_1 * val_3) - (val_2 * val_2) + 1) / (4 * val_1)) + ")");       ...

Read More

Find n-th lexicographically permutation of a strings in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 366 Views

ConceptWith respect of a given string of length m containing lowercase alphabets only, our task to determine the n-th permutation of string lexicographically.Inputstr[] = "pqr", n = 3OutputResult = "qpr"ExplanationAll possible permutation in sorted order − pqr, prq, qpr, qrp, rpq, rqpInputstr[] = "xyx", n = 2OutputResult = "xyx"ExplanationAll possible permutation in sorted order − xxy, xyx, yxxMethodHere we use some Mathematical concept for solving this problem.The concept is based on following facts.Here, the total number of permutation of a string generated by N characters (all distinct) is N!Now, the total number of permutation of a string generated by N ...

Read More

Maximum subarray sum in an array created after repeated concatenation in C++

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

In this tutorial, we will be discussing a program to find maximum subarray sum in an array created after repeated concatenation.For this we will be provided with an array and an integer K. Our task is to find the subarray with the maximum elements when the given array is repeated K times.Example#include using namespace std; //returning sum of maximum subarray int maxSubArraySumRepeated(int a[], int n, int k) {    int max_so_far = INT_MIN, max_ending_here = 0;    for (int i = 0; i < n*k; i++) {       max_ending_here = max_ending_here + a[i%n];       if (max_so_far ...

Read More

Encode N-ary Tree to Binary Tree in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 663 Views

Suppose we have a N-ary tree. We have to encode that tree into one binary. We also have to make deserializer to deserialize the binary tree to N-ary tree.So, if the input is likethen the output will beTo solve this, we will follow these steps −Define a function encode(), this will take root, if root is valid, then −return nullnode = new tree node with value of rootif size of children of root is not 0, then −left of node := encode(children[0] of root)curr = left of nodefor initialize i := 1, when i < size of children of root, ...

Read More

Find nth term of a given recurrence relation in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 330 Views

ConceptAssume bn be a sequence of numbers, which is denoted by the recurrence relation b1=1 and bn+1/bn=2n. Our task is to determine the value of log2(bn) for a given n.Input6Output15Explanationlog2(bn) = (n * (n - 1)) / 2 = (6*(6-1))/2 = 15Input200Output19900Methodbn+1/bn = 2nbn/bn-1 = 2n-1...b2/b1 = 21, We multiply all of above in order to attain(bn+1/bn).(bn/n-1)……(b2/b1) = 2n + (n-1)+……….+1So, bn+1/b1 = 2n(n+1)/2Because we know, 1 + 2 + 3 + ………. + (n-1) + n = n(n+1)/2So, bn+1 = 2n(n+1)/2 . b1; Assume the initial value b1 = 1So, bn+1 = 2sup>n(n+1)/2Now substituting (n+1) for n, we get, ...

Read More
Showing 27071–27080 of 61,297 articles
Advertisements