Programming Articles - Page 2354 of 3366

Program to print nodes between two given level numbers of a binary tree using C++

Ayush Gupta
Updated on 01-Nov-2019 06:12:35

174 Views

In this tutorial, we will be discussing a program to print nodes between the two given level numbers of a binary tree.In this, we will be given a low level and a high level for a particular binary tree and we have to print all the elements between the given levels.To solve this we can use queue-based level traversal. While moving through inorder traversal we can have a marking node at the end of each level. Then we can go to each level and print its nodes if the marking node exists between the given levels.Example#include #include using ... Read More

Find all combinations that add upto given number using C++

Arnab Chakraborty
Updated on 01-Nov-2019 06:06:46

502 Views

Suppose we have a positive number n. We have to find all combinations of positive numbers, that adds up to that number. Here we want only combinations, not the permutations. For the value n = 4, there will be [1, 1, 1, 1], [1, 1, 2], [2, 2], [1, 3], [4]We will solve this using recursion. We have an array to store combinations, and we will fill that array using recursive approach. Each combination will be stored in increasing order of elements.Example#include using namespace std; void getCombination(int arr[], int index, int num, int decrement) {    if (decrement < 0)       return;    if (decrement == 0){       for (int i = 0; i < index; i++)          cout

How can we update an existing JSON data using javax.json API in Java?

raja
Updated on 08-Jul-2020 06:48:13

7K+ Views

The JsonBuilderFactory interface is a factory to create JsonObjectBuilder instance and JsonObjectBuilder is a builder for creating JsonObject models from scratch. This interface initializes an empty JSON object model and provides methods to add name/value pairs to the object model and to return the resulting object. We can create a JsonObjectBuilder instance that can be used to build JsonObject using the createObjectBuilder() method.SyntaxJsonObjectBuilder createObjectBuilder()In the below example, We can update an existing JSON data with newly added data.Exampleimport java.io.*; import javax.json.*; public class UpdateExistingJsonTest {    public static void main(String[] args) throws Exception {       String jsonString = "{\"id\":\"115\", \"name\":\"Raja\", \"address\":[{\"area\":\"Madhapur\", \"city\":\"Hyderabad\"}]}";       ... Read More

How to serialize and deserialize a JSON using the ExclusionStrategy interface in Java?

raja
Updated on 08-Jul-2020 06:48:57

732 Views

The ExclusionStrategy interface can be used to exclude any field during serialization and deserialization. We can provide a custom implementation of the ExclusionStrategy interface and need to register it with GsonBuilder using the setExclusionStrategies() method. It configures Gson to apply a set of exclusion strategies during serialization and deserialization.Syntaxpublic GsonBuilder setExclusionStrategies(ExclusionStrategy... strategies)Exampleimport com.google.gson.*; import com.google.gson.ExclusionStrategy; import com.google.gson.FieldAttributes; public class ExclusionStrategyTest {    public static void main(String args[]) throws Exception {       Gson gson = new GsonBuilder().setExclusionStrategies(new CustomExclusionStrategy()).create();          Person person = new Person();          person.setFirstName("Adithya");          person.setLastName("Sai");          person.setAddress("Hyderabad");     ... Read More

Minimum number of power terms with sum equal to n using C++.

Narendra Kumar
Updated on 31-Oct-2019 07:40:40

308 Views

Problem statementGiven two positive integer N and X. The task is to express N as a sum of powers of X (X0 + X1 +…..+ Xn) such that the number of powers of X should be minimum.Print the minimum number of power of N used to make the sum equal to N.If N = 15 and X = 3 then we need 3 powers of ‘3’ as follows −15 = (32 + 31 + 31)AlgorithmUse below formula to calculate final result −1. If x = 1, then answer will be n only (n = 1 + 1 +…. n times)s ... Read More

Minimum number of points to be removed to get remaining points on one side of axis using C++.

Narendra Kumar
Updated on 31-Oct-2019 07:37:14

592 Views

Problem statementWe are given N points in a Cartesian plane. Our task is to find the minimum number of points that should be removed in order to get the remaining points on one side of any axis.If given input is {(10, 5), (-2, -5), (13, 8), (-14, 7)} then if we remove (-2, -5) then all remaining points are above X-axis.Hence answer is 1.Algorithm1. Finds the number of points on all sides of the X-axis and Y-axis 2. Return minimum from both of themExample#include #include #define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) using namespace std; struct point{    int x, ... Read More

Minimum Number of Platforms Required for a Railway Station using C++.

Narendra Kumar
Updated on 31-Oct-2019 07:35:09

329 Views

Problem statementGiven arrival and departure times of all trains that reach a railway station, the task is to find the minimum number of platforms required for the railway station so that no train waits.We are given two arrays that represent arrival and departure times of trains that stop.For below input, we need at least 3 platforms −TrainArrival timeDeparture timeTrain-109:0009:15Train-209:3511:45Train-309:4011:05Train-411:0012:00Train-514:3018:15Train-618:0019:00Algorithm1. Sort arrival and departure time arrays in ascending order 2. Trace the number of trains at any time keeping track of trains that haves arrived, but not departedExample#include #include #define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) using namespace std; int getPlatformCount(int ... Read More

Minimum number of palindromes required to express N as a sum using C++.

Narendra Kumar
Updated on 31-Oct-2019 07:30:17

219 Views

Problem statementGiven a number N, we have to find the minimum number of palindromes required to express N as a sum of themIf N = 15 then 2 palindromes are required i.e. 8 and 7.Algorithm1. Generate all the palindromes up to N in a sorted fashion 2. Find the size of the smallest subset such that its sum is NExample#include #include #include #include using namespace std; vector table; int createPalindrome(int input, bool isOdd){    int n = input;    int palindrome = input;    if (isOdd)       n /= 10;    while (n > ... Read More

Minimum number of page turns to get to a desired page using C++.

Narendra Kumar
Updated on 31-Oct-2019 07:26:43

585 Views

Problem statementGiven a book of N pages, the task is to calculate the minimum number of page turns to get to a give desired page K.we can either start turning pages from the front side of the book (i.e from page 1) or from the backside of the book (i.e page number N).Each page has two sides, front and back, except the first page, which has only backside and the last page which may only have backside depending on the number of pages of the book.If N = 5 and K = 4 then we have to turn minimum 1 ... Read More

Minimum number of operations required to sum to binary string S using C++.

Narendra Kumar
Updated on 31-Oct-2019 07:24:59

239 Views

Problem statementGiven a binary string str. Find the minimum number of operations required to be performed to create the number represented by str. Only following operations can be performed −Add 2xSubtract 2xIf binary string is “1000” then we have to perform only 1 operation i.e. Add 23If binary string is “101” then we have to perform 2 operations i.e. Add 22 + 20Example#include #include #include using namespace std; int getMinOperations(string s){    reverse(s.begin(), s.end());    int n = s.length();    int result[n + 1][2];    if (s[0] == '0') {       result[0][0] = 0;   ... Read More

Advertisements