Programming Articles

Page 1319 of 2547

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

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 254 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 350 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 172 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 660 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 326 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

Maximum sub-matrix area having count of 1's one more than count of 0's in C++

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

In this tutorial, we will be discussing a program to find maximum sub-matrix area having count of 1’s one more than count of 0’s.For this we will be provided with a matrix containing 0’s and 1’s. Our task is to get the sub-matrix of maximum area containing more 1’s than 0’sExample#include using namespace std; #define SIZE 10 //finding length of longest sub-matrix int lenOfLongSubarr(int arr[], int n, int& start, int& finish) {    unordered_map um;    int sum = 0, maxLen = 0;    for (int i = 0; i < n; i++) {       sum += ...

Read More

Find number of edges that can be broken in a tree such that Bitwise OR of resulting two trees are equal in C++

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

ConceptWith respect of a given tree with m nodes and a number associated with every node, we canbreak any tree edge which will result in the formation of 2 new trees. Here, we have to count the number of edges in this way so that the Bitwise OR of the nodes present in the two trees constructed after breaking that edge are equal. It should be noted that the value ofevery node is ≤ 10^6.Inputvalues[]={1, 3, 1, 3}      1    / | \   2 3 4Output2Here, the edge between 1 and 2 can be broken, the Bitwise ...

Read More

BK Tree Introduction in C++

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

BK tree or Burkhard tree is a form of a data structure usually used to perform spell checks based on Levenshtein distance. It is also used for string matching Autocorrect feature can be used making this data structure. Let's say we have some words in a dictionary and we need to check some other words for spelling errors. We need to have a collection of words that is close to the given word whose spelling is to be checked. For example, if we have the word “uck” The correct word can be (truck, duck, duck, suck). Therefore spelling mistakes can ...

Read More

Maximum subsequence sum such that no three are consecutive

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

In this tutorial, we will be discussing a program to find maximum subsequence sum such that no three are consecutive.For this we will be provided with a series of positive integers. Our task is to find the maximum sum without taking in their consecutive positive integers in the sum value.Example#include using namespace std; //returning maximum subsequence without involving //three consecutive numbers int maxSumWO3Consec(int arr[], int n) {    int sum[n]; if (n >= 1) sum[0] = arr[0];    if (n >= 2) sum[1] = arr[0] + arr[1];    if (n > 2) sum[2] = max(sum[1], max(arr[1] + arr[2], arr[0] ...

Read More

Find original numbers from gcd() every pair in C++

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

ConceptWith respect of a given array array[] containing GCD of every possible pair of elements of another array, our task is to determine the original numbers which are used to compute the GCD array.Inputarray[] = {6, 1, 1, 13}Output13 6 gcd(13, 13) = 13 gcd(13, 6) = 1 gcd(6, 13) = 1 gcd(6, 6) = 6Inputarr[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 6, 6, 6, 8, 11, 13, 3, 3}Output13 11 8 6 6MethodAt first, sort the array in decreasing order.Largest element will always be one of the ...

Read More
Showing 13181–13190 of 25,466 articles
Advertisements