Programming Articles - Page 2361 of 3366

Find a number x such that sum of x and its digits is equal to given n using C++.

Arnab Chakraborty
Updated on 29-Oct-2019 11:16:34

106 Views

Here we will see one problem, where we take a number n, we have to find another value say x, such that x + digit sum of x is same as the given number n. Suppose the value of n is 21. This program will return a number x = 15, as 15 + digit sum of 15, i.e. 15 + 1 + 5 = 21 = n.To solve this problem, we have to follow simple approach. We will iterate through 1 to n, in each iteration, we will see if the sum of the number and its digit sum ... Read More

Filling diagonal to make the sum of every row, column and diagonal equal of 3×3 matrix using c++

Arnab Chakraborty
Updated on 29-Oct-2019 11:19:29

275 Views

Suppose we have one 3x3 matrix, whose diagonal elements are empty at first. We have to fill the diagonal such that the sum of a row, column and diagonal will be the same. Suppose a matrix is like −After filling, it will be −Suppose the diagonal elements are x, y, z. The values will be −x = (M[2, 3] + M[3, 2])/ 2z = (M[1, 2] + M[2, 1])/ 2y = (x + z)/2Example Live Demo#include using namespace std; void displayMatrix(int matrix[3][3]) {    for (int i = 0; i < 3; i++) {       for (int j = 0; j < 3; j++)          cout

How to handle the errors generated while deserializing a JSON in Java?

raja
Updated on 07-Jul-2020 13:18:11

3K+ Views

The DeserializationProblemHandler class can be registered to get called when a potentially recoverable problem is encountered during the deserialization process. We can handle the errors generated while deserializing the JSON by implementing the handleUnknownProperty() method of DeserializationProblemHandler class.Syntaxpublic boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser p, JsonDeserializer deserializer, Object beanOrClass, String propertyName) throws IOExceptionExampleimport java.io.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.deser.*; public class DeserializationErrorTest {    public static void main(String[] args) throws JsonMappingException, JsonGenerationException, IOException {       String jsonString = "{\"id\":\"101\", \"name\":\"Ravi Chandra\", \"address\":\"Pune\", \"salary\":\"40000\" }";       ObjectMapper objectMapper = new ObjectMapper();       DeserializationProblemHandler deserializationProblemHandler = new UnMarshallingErrorHandler();     ... Read More

How to map the JSON data with Jackson Object Model in Java?

raja
Updated on 07-Jul-2020 13:04:14

3K+ Views

The ObjectMapper class provides functionality for converting between Java objects and matching JSON constructs. We can achieve mapping of JSON data represented by an Object Model to a particular Java object using a tree-like data structure that reads and stores the entire JSON content in memory. In the first step, read the JSON data into the JsonNode object then mapped it to another instance by calling the treeToValue() method of ObjectMapper class.Syntaxpublic T treeToValue(TreeNode n, Class valueType) throws JsonProcessingExceptionExampleimport java.io.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; public class JsonTreeModelDemo {    public static void main(String[] args) throws JsonProcessingException, IOException {       String jsonString ... Read More

How to merge two JSON strings in an order using JsonParserSequence in Java?

raja
Updated on 07-Jul-2020 13:06:13

881 Views

The JsonParserSequence is a helper class that can be used to create a parser containing two sub-parsers placed in a particular sequence. We can create a sequence using the static method createFlattened() of the JsonParserSequence class.Syntaxpublic static JsonParserSequence createFlattened(JsonParser first, JsonParser second)Exampleimport java.io.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.core.util.*; public class JsonParserSequenceTest {    public static void main(String[] args) throws JsonParseException, IOException {       String jsonString1 = "{\"id\":\"101\", \"name\":\"Ravi Chandra\", \"address\":\"Pune\"}";       String jsonString2 = "{\"id\":\"102\", \"name\":\"Raja Ramesh\", \"address\":\"Hyderabad\", \"contacts\":[{\"mobile\":\"9959984805\", \"home\":\"7702144400\"}]}";       JsonFactory jsonFactory = new JsonFactory();       JsonParser jsonParser1 = jsonFactory.createParser(jsonString1);       JsonParser jsonParser2 = jsonFactory.createParser(jsonString2);       ... Read More

Find amount to be added to achieve target ratio in a given mixture in C++

Arnab Chakraborty
Updated on 24-Oct-2019 14:12:49

76 Views

Suppose we have a container with size X. It has a mixture of water and other liquid, the mixture has W% of water in it. We have to find how many water must be added to increase the ratio of water to Y%? If X = 125, W = 20 and Y = 25, then output will be 8.33 liters.Suppose we have to add A amount of water with the previous mixture, so new amount will be X + A. So the amount of water in the mixture will follow this formula.Old Amount+A=((W% of X) + A)Also the amount of ... Read More

Find all pairs (a,b) and (c,d) in array which satisfy ab = cd in C++

Arnab Chakraborty
Updated on 24-Oct-2019 13:23:15

242 Views

Suppose we have an array A, from that array, we have to choose two pairs (a, b) and (c, d), such that ab = cd. Let the array A = [3, 4, 7, 1, 2, 9, 8]. The output pairs are (4, 2) and (1, 8). To solve this, we will follow these steps −For i := 0 to n-1, dofor j := i + 1 to n-1, doget product = arr[i] * arr[j]if product is not present in the hash table, then Hash[product] := (i, j)if product is present in the hash table, then print previous and current elements.Example Live ... Read More

Find all pairs (a, b) in an array such that a % b = k in C++

Arnab Chakraborty
Updated on 24-Oct-2019 13:15:12

194 Views

Suppose we have an array A, from that array, we have to get all pairs (a, b) such that the a%b = k. Suppose the array is A = [2, 3, 4, 5, 7], and k = 3, then pairs are (7, 4), (3, 4), (3, 5), (3, 7).To solve this, we will traverse the list and check whether the given condition is satisfying or not.Example Live Demo#include using namespace std; bool displayPairs(int arr[], int n, int k) {    bool pairAvilable = true;    for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {          if (arr[i] % arr[j] == k) {             cout

Find all factorial numbers less than or equal to n in C++

Arnab Chakraborty
Updated on 24-Oct-2019 13:08:48

1K+ Views

Here we will see how to print all factorial numbers less than or equal to n, a number N is said to be factorial number if it is a factorial of a positive number. So some factorial numbers are 1, 2, 6, 24, 120.To print factorial numbers, we do not need to find the factorial directly. Starting from i = 1, print factorial*i. Initially factorial is 1. Let us see the code for better understanding.Example Live Demo#include using namespace std; void getFactorialNumbers(int n) {    int fact = 1;    int i = 2;    while(fact

Find All Duplicate Subtrees in C++

Arnab Chakraborty
Updated on 24-Oct-2019 13:05:38

206 Views

Consider we have a binary tree. We have to find if there are some duplicate subtrees in the tree or not. Suppose we have a binary tree like below −There are two identical subtrees of size 2. In each subtree D, BD and BE both are also duplicate subtrees We can solve this problem by using tree serialization and hashing process. We will store the inorder traversal of subtrees in the hash table. We will insert opening and closing parenthesis for empty nodes.Example Live Demo#include #include #include #include using namespace std; const char MARKER = '$'; struct ... Read More

Advertisements