Server Side Programming Articles - Page 1894 of 2650

House Robber II in C++

Arnab Chakraborty
Updated on 04-May-2020 08:59:30

785 Views

Consider, you are a professional robber. And you are planning to rob houses along a street. Each house has a certain amount of money stored. All houses are arranged in a circle. That means the first house is the neighbor of the last house. We have to keep in mind that the adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night. So if we have a list of integers representing the amount of money of each house, determine the maximum amount of money you can ... Read More

Implement Trie (Prefix Tree) in Python

Arnab Chakraborty
Updated on 04-May-2020 08:58:35

5K+ Views

Suppose we have to make the trie structure, with three basic operations like insert(), search(), startsWith() methods. We can assume that all inputs are in lowercase letters. For example, if we call the functions as follows, we will see the outputsTrie trie = new Trie()trie.insert(“apple”)trie.search(“apple”)     //This will return truetrie.search(“app”)        //This will return falsetrie.startsWith(“app”)   //This will return truetrie.insert(“app”)trie.search(“app”)        //This will return trueTo solve this, we will follow these steps −Initially make one dictionary called child.The insert method will be like −current := childfor each letter l in word −if l is not present in ... Read More

Number of Islands in Python

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

2K+ Views

Suppose we have a grid, there are few 0s and few 1s. We have to count the number of islands. An island is place that is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. We can assume that all four edges of the grid are all surrounded by water.Suppose the grid is like −11000110000010000011There are three islands.To solve this, we will follow these steps −There will be two methods, one will be used to count number of islands called numIslands() and makeWater(). The makeWater() will be like −if number of rows in the grid is ... Read More

Binary Tree Right Side View in C++

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

203 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

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

437 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

723 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

560 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

282 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

Advertisements