Programming Articles

Page 1117 of 2547

Klee's Algorithm (Length Of Union Of Segments of a line) in C++

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

In this tutorial, we are going to write a program that finds the length of union of segments of a line.We are given starting and ending points of the line segment and we need to find the length of union of segments of the line.The algorithm that we are going to use is called klee's algorithm.Let's see the steps to solve the problem.Initialise the array with coordinates of all the segments.Initialise a vector called points with double the size of segments array.Iterate over the segments array.Fill the values of points array at index i * 2 with the first point ...

Read More

Kth node in Diagonal Traversal of Binary Tree in C++

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

In this tutorial, we are going to write a program that finds the k-th node in the diagonal traversal of a binary tree.Let's see the steps to solve the problem.Initialise the binary tree with some sample data.Initialise the number k.Traverse the binary tree diagonally using the data structure queue.Decrement the value of k on each node.Return the node when k becomes 0.Return -1 if there is no such node.ExampleLet's see the code.#include using namespace std; struct Node {    int data;    Node *left, *right; }; Node* getNewNode(int data) {    Node* node = (Node*)malloc(sizeof(Node));    node->data = data; ...

Read More

Kth odd number in an array in C++

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

In this tutorial, we are going to write a program that finds the k-th odd number from the given array.Let's see the steps to solve the problem.Initialise the array and k.Iterate over the array.If the current element is odd, then decrement the value of k.If k is 0, then return the current element.Return -1.ExampleLet's see the code.#include using namespace std; int findKthOddNumber(int arr[], int n, int k) {    for (int i = 0; i

Read More

Kth prime number greater than N in C++

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

In this tutorial, we are going to write a program that finds the k-th prime number which is greater than the given number n.Initialise the number n.Find all the prime numbers till 1e6 and store it in a boolean array.Write a loop that iterates from n + 1 to 1e6.If the current number is prime, then decrement k.If k is equal to zero, then return i.Return -1.ExampleLet's see the code.#include using namespace std; const int MAX_SIZE = 1e6; bool prime[MAX_SIZE + 1]; void findAllPrimes() {    memset(prime, true, sizeof(prime));    for (int p = 2; p * p

Read More

Kth smallest element after every insertion in C++

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

In this tutorial, we are going to find the k-th smallest element after every insertion.We are going to use the min-heap to solve the problem. Let's see the steps to complete the program.Initialise the array with random data.Initialise the priority queue.Till k - 1 there won't be any k-th smallest element. So, print any symbol u like.Write a loop that iterates from k + 1 to n.Print the root of the min-heap.If the element is greater than the root of the min-heap, then pop the root and insert the element.ExampleLet's see the code.#include using namespace std; void findKthSmallestElement(int elements[], ...

Read More

kth smallest/largest in a small range unsorted array in C++

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

In this tutorial, we are going to write a program that finds the k-th smallest number in the unsorted array.Let's see the steps to solve the problem.Initialise the array and k.Sort the array using sort method.Return the value from the array with the index k - 1.Let's see the code.Example#include using namespace std; int findKthSmallestNumber(int arr[], int n, int k) {    sort(arr, arr + n);    return arr[k - 1]; } int main() {    int arr[] = { 3, 5, 23, 4, 15, 16, 87, 99 }, k = 5;    cout

Read More

Lagrange's four square theorem in C++

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

In this tutorial, we are going to learn about largrange's four square theorem.The lagranges's four square theorem states that every natural number can be written as sum of squares of 4 numbers.The following code finds the 4 numbers which satisfies the above condition for the given number n.ExampleLet's see the code.#include using namespace std; void printSquareCombinations(int n) {    for (int i = 0; i * i

Read More

Lagrange's Interpolation in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 2K+ Views

In this tutorial, we are going to write a program that finds the result for lagranges's interpolation formula.You don't have write any logic for the program. Just convert the formula into code. Let's see the code.Example#include using namespace std; struct Data {    int x, y; }; double interpolate(Data function[], int xi, int n) {    double result = 0;    for (int i = 0; i < n; i++) {       double term = function[i].y;       for (int j = 0; j < n; j++) {          if (j != i) { ...

Read More

Larger of a^b or b^a in C++

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

In this tutorial, we are going to write a program that finds out the larger among the ab and baIt's a straightforward problem. Let's see the steps to solve it.Initialise the values of a and b.Take the log of both the values.Compute the values of $b\:\log\:a$ and $a\:\log\:b$Compare the both values.If $a\:\log\:b$ is greater than $b\:\log\:a$, then print bais greater.If $b\:\log\:a$ is greater than $a\:\log\:b$, then print abis greater.Else print both are equal.ExampleLet's see the code.#include using namespace std; int main() {    int a = 4, b = 7;    long double x = (long double) a * ...

Read More

Largest Even and Odd N-digit numbers in C++

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

In this tutorial, we are going to write a program that finds the largest even and odd number of n digits number.Let's see the steps to solve the problem.Initialise the number n.The largest odd number is pow(10, n) - 1.The largest even number is odd - 1.ExampleLet's see the code.#include using namespace std; void findEvenAndOddNumbers(int n) {    int odd = pow(10, n) - 1;    int even = odd - 1;    cout

Read More
Showing 11161–11170 of 25,466 articles
Advertisements