C++ Articles

Page 20 of 597

k-th missing element in an unsorted array in C++

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

In this tutorial, we are going to write a program that finds out the k-th missing element in the given unsorted array.Find the k-th number that is missing from min to max in the given unsorted array. Let's see the steps to solve the problem.Initialise the unsorted array.Insert all the elements into a set.Find the max and min elements from the array.Write a loop that iterates from min to max and maintain a variable for the count.If the current element is present in the set, then increment the count.If the count is equal to k, then return i.ExampleLet's see the ...

Read More

k-th missing element in sorted array in C++

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

In this tutorial, we are going to write a program that finds out the k-th missing element in the given sorted array.Find the k-th number that is missing from min to max in the given unsorted array. Let's see the steps to solve the problem.Initialise the sorted array.Initialise two variables difference and count with k.Iterate over the array.If the current element is not equal to the next element.Find the difference between the two numbers.If the difference is greater than or equal to k, then return current element plus count.Else subtract difference from the count.Return -1.ExampleLet's see the code.#include using ...

Read More

K-th smallest element after removing some integers from natural numbers in C++

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

In this tutorial, we are going to write a program that finds out the smallest element after removing some integers from the natural numbers.We have given an array of elements and k value. Remove all the elements from natural numbers that are present in the given array. And then find the k-th smallest number from the remaining natural numbers.Let's see the steps to solve the problem.Initialise the array and k.Initialise an array and initialise all the elements with 0 except the elements present in the given array.Write a loop that iterates till the size of the given array.Decrement the value ...

Read More

K'th Boom Number in C++

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

In this tutorial, we are going to write a program that finds the k-th boom number.The number that contains only 2 and 3 are called boom number.Let's see the steps to solve the above problem.Initialise the value of k.Initialise a queue of strings.Push the empty string to the queue.Initialise a counter variable to 0.Write a loop that iterates till counter is less than or equal to the given k.Get the front of the queue.Pop the element from the queue.Store the front of the queue in a variable.Push the number after appending 2 to the front.Increment the counter and check whether ...

Read More

K'th Least Element in a Min-Heap in C++

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

In this tutorial, we are going to write a program that finds the k-th least element from the min-heap.We will use priority queue to solve the problem. Let's see the steps to complete the program.Initialise the min-heap with correct values.Create a priority queue and insert the root node of the min-heap.Write a loop that iterates k - 1 times.Pop the least element from the queue.Add the left and right nodes of the above node into the priority queue.The greatest element in priority queue is the k-th greatest element now.Return it.ExampleLet's see the code.#include using namespace std; struct Heap { ...

Read More

K'th Smallest/Largest Element in Unsorted Array in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 283 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.ExampleLet's see the code.#include using namespace std; int findKthSmallestNumber(int arr[], int n, int k) {    sort(arr, arr + n);    return arr[k - 1]; } int main() {    int arr[] = { 45, 32, 22, 23, 12 }, n = 5, k = 3;    cout

Read More

K'th Smallest/Largest Element using STL in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 1K+ 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.Initialise a empty ordered set.Iterate over the array and insert each element to the array.Iterate over the set from 0 to k - 1.Return the value.ExampleLet's see the code.#include using namespace std; int findKthSmallestNumber(int arr[], int n, int k) {    set set;    for (int i = 0; i < n; i++) {       set.insert(arr[i]);    }    auto it = set.begin();    for (int i ...

Read More

Kaprekar Number in C++

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

In this tutorial, we are going to write a program that finds whether the given number is kaprekar number or not.Take a number. Find the square of that number. Divide the number into two parts. If the sum of the two parts is equal to the original number, then the number is called kaprekar number.Let's see the steps to solve the problem.Initialise the n.Find the square of the n.Find the number of digits in the square of the n and store it in a variable.Divide the square of n with 10, 100, 1000, etc.., until the digits count.Check whether sum ...

Read More

Keith Number in C++

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

In this tutorial, we are going to write a program that checks whether the given number is Keith Number or not.The number n is called Keith number if it appears in the sequence generated using its digits. The sequence has first n terms as digits of the number n and other terms are recursively evaluated as sum of previous n terms.Let's see the steps to solve the problem.Initialise the number n.Initialise an empty vector elements to store the sequence.Count the digits and add every digit to the vecor.Reverse the digits vector.Initialise a variable with 0 called next element.Write a loop ...

Read More

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

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 739 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
Showing 191–200 of 5,962 articles
« Prev 1 18 19 20 21 22 597 Next »
Advertisements