Found 7347 Articles for C++

Kth prime number greater than N in C++

Hafeezul Kareem
Updated on 09-Apr-2021 13:10:07

219 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. Live Demo#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 odd number in an array in C++

Hafeezul Kareem
Updated on 09-Apr-2021 13:09:45

254 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. Live Demo#include using namespace std; int findKthOddNumber(int arr[], int n, int k) {    for (int i = 0; i

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

Hafeezul Kareem
Updated on 09-Apr-2021 13:01:01

63 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. Live Demo#include using namespace std; struct Node {    int data;    Node *left, *right; }; Node* getNewNode(int data) {    Node* node = (Node*)malloc(sizeof(Node));    node->data = ... Read More

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

Hafeezul Kareem
Updated on 09-Apr-2021 12:59:57

347 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

Keith Number in C++

Hafeezul Kareem
Updated on 09-Apr-2021 12:59:34

266 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

Kaprekar Number in C++

Hafeezul Kareem
Updated on 09-Apr-2021 12:59:13

2K+ 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

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

Hafeezul Kareem
Updated on 09-Apr-2021 12:58:23

650 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. Live Demo#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 ... Read More

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

Hafeezul Kareem
Updated on 09-Apr-2021 12:37:28

147 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. Live Demo#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

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

Hafeezul Kareem
Updated on 09-Apr-2021 12:37:03

439 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. Live Demo#include using namespace std; struct Heap ... Read More

K’th Boom Number in C++

Hafeezul Kareem
Updated on 09-Apr-2021 12:36:39

169 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

Advertisements