Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles by sudhir sharma
Page 70 of 98
Preorder predecessor of a Node in Binary Tree in C++
In this problem, we are given a binary tree and a node value. Our task is to print the preorder predecessor of the node.Binary Tree is a special type of tree in which each root node can have maximum 2 child nodes.Preorder Traversal is a way to traverse nodes of the tree. In this we will first traverse root node then left child and then the right child.Preorder predecessor node is the node that comes before the node in the preorder traversal of the node.Let’s take an example to understand the problemInput: 1 Output: 9To solve this problem, a navie approach will ...
Read MorePreorder Successor of a Node in Binary Tree in C++
In this problem, we are given a binary tree and a node value. Our task is to print the preorder successor of the node.Binary Tree is a special type of tree in which each root node can have maximum 2 child nodes.Preorder Traversal is a way to traverse nodes of the tree. In this we will first traverse root node then left child and then the right child.Preorder successor node is the node that comes next to the node in the preorder traversal of the node.Let’s take an example to understand the problemInput: 9 Output 0 Explanation: the preorder traversal ...
Read MorePreorder Traversal of N-ary Tree Without Recursion in C++
In this problem, we are given an N-ary Tree. Our task is to print the preorder traversal of the tree.First, let’s learn some basic terminologies, N-ary Tree is a tree in which all nodes can have at max N child nodes. Example 2-ary (binary) tree has max 2 child nodes.Preorder Traversal is a way to traverse nodes of the tree. In this we will first traverse root node then left child and then the right child.Let’s take an example to understand our problemPreorder traversal : 12151499411719To solve this problem, we have to use the stack data structure. We will first ...
Read MorePrevious greater element in C++
In this problem, we are given an array. Our task is to return the greatest element that precedes the current element in the array otherwise print -1.Let’s take an example to understand the problemInput: {6, 2, 7, 1, 5, 3} Output: -1, 6, -1, 7, 7, 7To solve this problem, an easy and obvious solution will be using nested loops that will check greater element in the preceding part of the array.Program to show the implementation of our solutionExample Live Demo#include using namespace std; void preceddingGreatestElement(int arr[], int n){ cout = 0; j--) { if (arr[i]
Read MorePrevious number same as 1’s complement in C++
In this problem, we are given an integer n. Our task is to check weather the preceding number is equal to 1’s complement of the number.Let’s take a few examples to understand our problemInput: 12 Output: No Explanation: (12)10 = (1100)2 Preceding number 11 = (1011)2 1’s complement of 12 = (0011)2 Input: 4 Output: Yes Explanation: 4 = (100)2 Preceding number 3 = (011)2 1’s complement of 12 = (011)2To solve this problem, we can use a simple approach which is by comparing the previous number and the 1’s complement of the number.This approach is simple but consumes space ...
Read MorePrevious smaller integer having one less number of set bits in C++
In this problem, we are given an integer n. Our task is to print the largest number less than n which can be formed by changing one set bit of the binary representation of the number.Let’s take an example to understand the problemInput: n = 3 Output: 2 Explanation: (3)10 = (011)2 Flipping one set bit gives 001 and 010. 010 is greater i.e. 2.To solve this problem, we will have to flip the rightmost set bit and make it zero which will create the number the greatest possible number less than n that is found by flipping one bit ...
Read MorePrimality Test in C++
In this problem, we are given a number N and our task is to check whether it is a prime number or not.Primality test s the algorithm that is used to check whether the given number is prime or not.Prime number is a number which can be divided by itself only. Example : 2, 3, 5, 7.Let’s take an example to understand our problem, Input: 11 Output: YesThere are multiple methods to check for primality test of a number.One simple method to check for primality is by checking the division of the number by all numbers less than N. If ...
Read MorePrimality test for the sum of digits at odd places of a number in C++
In this problem, we are given a number N. our task is to check whether the sum of digits at odd place of the number gives a prime number or not.Primality Test is the algorithm that is used to check whether the given number is prime or not.Let’s take an example to understand the problem, Input: 3425 Output: No Explanation: sum digits at odd place = 5 + 4 = 9, which is not a prime number.To solve this problem an easy approach would be adding all digits that are at odd places in the number and then checking whether ...
Read MorePrimary key Vs Unique key
Primary KeyPrimary Key is a column that is used to uniquely identify each tuple of the table.It is used to add integrity constraints to the table. Only one primary key is allowed to be used in a table. Duplicate and NULL (empty) values are not valid in the case of the primary key. Primary keys can be used as foreign keys for other tables too.Let’s take an example, We have a table name employee which stores data of employees of a company. The below table shows the contents of the table.Emp_idNamePh_No.PositionSalaryEmp_id here is primary key of the table. As the ...
Read MorePrime Factor in C++ Program
Prime Factor is a prime number which is the factor of the given number.Factor of a number are the numbers that are multiplied to get the given number.Prime Factorisation is the process of recursively dividing the number with its prime factors to find all the prime factors of the number.Example : N = 120 Prime factors = 2 5 3 Factorization : 2 * 2 * 2 * 3 * 5Some points to remember about prime factors of a numberSet of prime factors of a number is unique.Factorization is important in many mathematical calculations like divisibility, finding common denominators, etc.It’s ...
Read More