Server Side Programming Articles - Page 1893 of 2646

Binary Tree Right Side View in C++

Arnab Chakraborty
Updated on 04-May-2020 08:56:19

219 Views

Suppose we have a binary tree, if we see the tree from right side, then we can see some elements of it. we have to display those elements. So if the tree is like −To solve this, we will follow these steps −We will create one helping method for dfs. This will take tree_node, an array to hold answers, and level. The level is initially 0. The dfs will work like below −if node is null, then returnif level = length of the answer array, then insert value of node into the ans arraydfs(right of the node, ans, level + ... Read More

Binary Search Tree Iterator in C++

Arnab Chakraborty
Updated on 04-May-2020 08:55:04

2K+ Views

Suppose we want to make one iterator for binary tree. There will be two methods. The next() method to return the next element, and hasNext() method to return Boolean value, that will indicate that the next element is present or not. So if the tree is like −And the sequence of function calls are [next(), next(), hasNext(), next(), hasNext(), next(), hasNext(), next(), hasNext()]. The output will be [3, 7, true, 9, true, 15, true, 20, false]To solve this, we will follow these steps −There are two methods next and hasNext, The next() method will be like −curr := stack top ... Read More

Find Peak Element in Python

SaiKrishna Tavva
Updated on 12-Dec-2024 17:50:26

5K+ Views

A peak element in an array is defined as an element that is greater than its neighbors. We can use some common approaches such as binary search to efficiently locate a peak element and linear search algorithm which involves iterating through an array. Binary Search Approach The Binary Search is an efficient technique for locating a peak element in an array. To find a peak, follow these steps: Choose the middle element of the array. ... Read More

Count numbers from 1 to n that have 4 as a digit in C++

Ayush Gupta
Updated on 05-Feb-2020 07:57:50

451 Views

In this tutorial, we will be discussing a program to find the numbers from 1 to n that have 4 as a digit.For this we will be provided with a number n. Our task is to count all the numbers which have 4 as one of their digits and print it out.Example Live Demo#include using namespace std; bool has4(int x); //returning sum of digits in the given numbers int get_4(int n){    int result = 0;    //calculating the sum of each digit    for (int x=1; x

Count number of even and odd elements in an array in C++

Ayush Gupta
Updated on 05-Feb-2020 07:53:52

758 Views

In this tutorial, we will be discussing a program to find the number of even and odd elements in an array.For this we will be provided with an array. Our task is to calculate the number of even and odd elements in the given array.Example Live Demo#include using namespace std; void CountingEvenOdd(int arr[], int arr_size){    int even_count = 0;    int odd_count = 0;    //looping through the elements    for(int i = 0 ; i < arr_size ; i++) {       //checking if the number is odd       if (arr[i]%2 != 0)          odd_count ++ ;       else          even_count ++ ;    }    cout

Count all increasing subsequences in C++

Ayush Gupta
Updated on 05-Feb-2020 07:49:01

585 Views

In this tutorial, we will be discussing a program to find the number of increasing sequences.For this we will be provided with an array containing digits 0 to 9. Our task is to count all the sequences present in the array such that the next element is greater than the previous element.Example Live Demo#include using namespace std; //counting the possible subsequences int count_sequence(int arr[], int n){    int count[10] = {0};    //scanning each digit    for (int i=0; i=0; j--)          count[arr[i]] += count[j];       count[arr[i]]++;    }    //getting all the possible subsequences    int result = 0;    for (int i=0; i

Count all elements in the array which appears at least K times after their first occurrence in C++

Ayush Gupta
Updated on 05-Feb-2020 07:45:38

296 Views

In this tutorial, we will be discussing a program to find the number of elements in the array which appears at least K times after their first occurrence.For this we will be provided with an integer array and a value k. Our task is to count all the elements occurring k times among the elements after the element in consideration.Example Live Demo#include #include using namespace std; //returning the count of elements int calc_count(int n, int arr[], int k){    int cnt, ans = 0;    //avoiding duplicates    map hash;    for (int i = 0; i < n; ... Read More

Count all distinct pairs with difference equal to k in C++

Ayush Gupta
Updated on 05-Feb-2020 07:41:45

232 Views

In this tutorial, we will be discussing a program to find the distinct pairs with difference equal to k.For this we will be provided with an integer array and the value k. Our task is to count all the distinct pairs that have the difference as k.Example Live Demo#include using namespace std; int count_diffK(int arr[], int n, int k) {    int count = 0;    //picking elements one by one    for (int i = 0; i < n; i++) {       for (int j = i+1; j < n; j++)          if (arr[i] - ... Read More

Count all 0s which are blocked by 1s in binary matrix in C++

Ayush Gupta
Updated on 05-Feb-2020 07:38:38

296 Views

In this tutorial, we will be discussing a program to find the count of 0s which are blocked by 1s in a binary matrix.For this we will be provided with a binary matrix. Our task is to find and count all the 0s in the matrix that are blocked by 1s.Example Live Demo#include using namespace std; #define Row 4 #define Col 5 int r[4] = { 0, 0, 1, -1 }; int c[4] = { 1, -1, 0, 0 }; bool isSafe(int x, int y, int M[][Col]) {    if (x >= 0 && x = 0 &&       y

Count 1’s in a sorted binary array in C++

Ayush Gupta
Updated on 05-Feb-2020 07:32:28

167 Views

In this tutorial, we will be discussing a program to find the 1’s in a sorted binary array.For this we will be provided with an array containing only 1 and 0. Our task is to count the number of 1’s present in the array.Example Live Demo#include using namespace std; //returning the count of 1 int countOnes(bool arr[], int low, int high){    if (high >= low){       int mid = low + (high - low)/2;       if ( (mid == high || arr[mid+1] == 0) && (arr[mid] == 1))          return mid+1;   ... Read More

Advertisements