Found 7347 Articles for C++

C++ Programe to find n-th term in series 1 2 2 3 3 3 4

Naveen Singh
Updated on 13-Mar-2021 12:14:13

275 Views

In this problem, we are given an integer N. The task is to find n-th term in series 1 2 2 3 3 3 4….Let’s take an example to understand the problem, InputN = 6Output3ExplanationThe series upto nth term is 1, 2, 2, 3, 3, 3, ...Solution ApproachA simple approach to solve the problem is by using a nested loop. The outer for loop is from 1 to n. And the inner loop is from 1 to i (iterator of outer loop). For each iteration in the inner loop, count the number of elements of the series and return the ... Read More

Find n-th node of inorder traversal in C++

Naveen Singh
Updated on 13-Mar-2021 12:12:17

269 Views

In this problem, we are given a binary tree and an integer N. The task is to find the n-th node in inorder traversal of a Binary Tree.A binary tree has a special condition that each node can have a maximum of two children.Traversal is a process to visit all the nodes of a tree and may print their values too.Let’s take an example to understand the problem, InputN = 6Output3Explanationinorder traversal of tree : 4, 2, 5, 1, 6, 3, 7Solution ApproachThe idea is to use the in-order traversal of the binary tree which is done by using recursive ... Read More

Find n-th node in Preorder traversal of a Binary Tree in C++

Naveen Singh
Updated on 13-Mar-2021 12:08:15

194 Views

In this problem, we are given a binary tree and an integer N. The task is to find the n-th node in Preorder traversal of a Binary Tree.A binary tree has a special condition that each node can have a maximum of two children.Traversal is a process to visit all the nodes of a tree and may print their values too.Let’s take an example to understand the problem, InputN = 6Output6ExplanationPre order traversal of tree : 1, 2, 4, 5, 3, 6, 7Solution ApproachThe idea is to use the pre-order traversal of the binary tree which is done by using ... Read More

Find n-th node in Postorder traversal of a Binary Tree in C++

Naveen Singh
Updated on 12-Mar-2021 08:33:38

93 Views

In this problem, we are given a binary tree and an integer N. The task is to find the n-th node in Postorder traversal of a Binary Tree.A binary tree has a special condition that each node can have a maximum of two children.Traversal is a process to visit all the nodes of a tree and may print their values too.Let’s take an example to understand the problem, InputN = 6Output3ExplanationPost order traversal of tree − 4, 5, 2, 6, 7, 3, 1Solution ApproachThe idea is to use the post order traversal of the binary tree which is done by ... Read More

Find n-th element in a series with only 2 digits (and 7) allowed in C++

Naveen Singh
Updated on 12-Mar-2021 08:28:25

163 Views

In this problem, we are given an integer N, denoting a series of numbers consisting of 4 and 7 only.The series is 4, 7, 44, 47, 74, 77, …The task is to find the n-th element in a series with only 2 digits (and 7) allowed.Let’s take an example to understand the problem, InputN = 4, Output47ExplanationThe series is: 4, 7, 44, 47, ….Solution ApproachA simple solution to the problem is creating the series till Nth number. It’s simple, if the current number’s last digit is 7. Then the last digit of previous and next numbers is 4.So, we will ... Read More

Find n positive integers that satisfy the given equations in C++

Naveen Singh
Updated on 12-Mar-2021 08:26:15

213 Views

In this problem, we are given three values A, B and N. Our task is to Find n positive integers that satisfy the given equations.Problem Description − We need to find the N positive values that satisfy both the equations, x12 + x22 + … xn2 ≥ A x1 + x2 + … xn ≤ BPrint n values if present, otherwise print -1.Let’s take an example to understand the problem, InputN = 4, A = 65, B = 16Output1 1 1 8ExplanationThe equations are −12 + 12 + 12 + 82 = 1 + 1 + 1 + 64 = ... Read More

Find N % (Remainder with 4) for a large value of N in C++

Naveen Singh
Updated on 12-Mar-2021 08:21:40

61 Views

In this problem, we are given a string num representing a large integer. Our task is to Find N % (Remainder with 4) for a large value of N.Problem Description − we will be finding the remainder of the number with 4.Let’s take an example to understand the problem, Inputnum = 453425245Output1Solution ApproachA simple solution to the problem is by using the fact that the remainder of the number with 4 can be found using the last two digits of the number. So, for any large number, we can find the remainder by dividing the number’s last two digits by ... Read More

Find Multiples of 2 or 3 or 5 less than or equal to N in C++

Naveen Singh
Updated on 12-Mar-2021 08:18:02

245 Views

In this problem, we are given a number N. Our task is to Find Multiples of 2 or 3 or 5 less than or equal to N.Problem Description − We will be counting all elements from 1 to N that are divisible by 2 or 3 or 5.Let’s take an example to understand the problem, InputN = 7Output5ExplanationAll the elements from 1 to 7 are : 1, 2, 3, 4, 5, 6, 7. Elements divisible by 2/3/5 are 2, 3, 4, 5, 6Solution ApproachA simple approach to solve the problem is by traversing all numbers from 1 to N and ... Read More

Find modular node in a linked list in C++

Naveen Singh
Updated on 12-Mar-2021 07:36:43

163 Views

In this problem, we are given a singly linked list LL and a number k. Our task is to Find modular node in a linked list.Problem Description − we need to find the last node of the linked list for which the index is divisible by k i.e. i % k == 0.Let’s take an example to understand the problem, Inputll = 3 -> 1 -> 9 -> 6 -> 8 -> 2, k = 4Output6ExplanationThe element 6 has index 4, which is divisible by 4.Solution ApproachA simple solution to the problem is by creating a counter to count the ... Read More

Find missing elements of a range in C++

Naveen Singh
Updated on 12-Mar-2021 07:33:23

350 Views

In this problem, we are given an array arr[] of size n and the start and end element denoting the range. Our task is to Find missing elements of a range.Problem Description − we will be finding the elements of the range that are not present in the range.Let’s take an example to understand the problem, Inputarr[] = {4, 6, 3, 7}, start = 3, end = 8Output5, 8ExplanationThe range is [3, 4, 5, 6, 7, 8]The array is {4, 6, 3, 7}Elements of range that are not present in array is 5, 8Solution ApproachYou can solve this problem in ... Read More

Advertisements