Programming Articles - Page 2404 of 3363

C++ program to find cabs nearby using Great Circle Distance formula

Ayush Gupta
Updated on 03-Oct-2019 11:40:29

650 Views

In this article, we will be discussing a program to find the cabs near about (less than 50km) using the Great Circle Distance formula.Let us suppose we have been given a JSON file which contains the name and coordinates of the people who need a cab and also the coordinates of the all the cabs available.To solve this, we would convert the GPS coordinates into double. From the double form, we would finally convert them in degrees to radians. Then we can ultimately apply the Great Circle Distance formula to find the cabs available in 50km from the user’s position.Note ... Read More

C++ program to find ‘k’ such that its modulus with each array element is same

Ayush Gupta
Updated on 03-Oct-2019 11:34:33

357 Views

In this article, we will be discussing a program to find an integer ‘k’, such that its modulus with each element of a given array is the same.For example, let us suppose we have been given with an array, arr = {12, 22, 32}Then we have the output value of k = 1, 2, 5, 10.Take the case of two values in the array ‘x’ and ‘y’ (x>y). Then we have (y+difference)%k = y%k. Solving this we get, difference%k = 0So, we will find all the divisors to the difference of the maximum and minimum element in the array and ... Read More

C++ program for Finite Automata algorithm for Pattern Searching

Ayush Gupta
Updated on 03-Oct-2019 11:30:42

1K+ Views

In this article, we will be discussing a program to execute the Finite Automata algorithm for pattern searching.We are provided with a text[0...n-1] and a pattern[0...m-1]. We have to find all the occurrences of the pattern[] in the text[].For this we would preprocess the text[] and build a 2-d array to represent it. After that we just have to traverse between the elements of the text[] and the different states of the automata.Example Live Demo#include #include #define total_chars 256 int calc_nextstate(char *pat, int M, int state, int x) {    if (state < M && x == pat[state])       ... Read More

C++ program to check whether it is possible to finish all the tasks or not from given dependencies

Ayush Gupta
Updated on 03-Oct-2019 11:25:23

191 Views

In this article, we will be discussing a program to check if it is possible to finish all the given tasks on the basis of the given prerequisites.For example, let us say we have been given three tasks and prerequisites are [[1, 0], [2, 1], [3, 2]].( [1, 0] means that to pick up ‘1’ task; the ‘0’ task must be completed first.)Then, in this example since the ‘0’ task doesn’t have any prerequisite it can be completed first. Then the ‘1’ task can be completed, since the ‘0’ task has been completed. Similarly, both ‘2’ and ‘3’ tasks can ... Read More

C++ program to find ways an integer can be expressed as sum of n-th power of unique natural numbers

Ayush Gupta
Updated on 03-Oct-2019 11:21:14

823 Views

In this article, we will be discussing a program to find ways an integer (say X) can be expressed as sum of n-th power of unique natural numbers.For example, let X = 100 and n = 2Then there would be 3 ways to express 100 as sum of squares of natural numbers.100 = 102 100 = 62 + 82 100 = 12 + 32 + 42 + 52 + 72This can be done easily by using recursion. We would start from 1 and go till the n-th root of the given number. In every run, we would subtract the n-th ... Read More

C++ program to find unique pairs such that each element is less than or equal to N

Ayush Gupta
Updated on 03-Oct-2019 11:17:31

187 Views

In this article, we will be discussing a program to find unique pairs of numbers having elements less than or equal to N and following some certain conditions −The square of difference between the two numbers must be equal to the LCM of those two numbers.The HCF of those two numbers can be represented as a product of any two consecutive numbers.The best approach to solve this problem would be to take two consecutive numbers (starting from 1) and finding the multiples of product of those numbers. Then among the multiples, to specify to one pair of numbers we need ... Read More

C++ program to find union and intersection of two unsorted arrays

Ayush Gupta
Updated on 03-Oct-2019 11:14:47

2K+ Views

In this article, we will be discussing a program to find the union and intersection of two given unsorted arrays.Let us denote the two arrays with ‘A’ and ‘B’. Then union of those arrays is denoted by A ∪ B which is basically an array of all the elements in both the given arrays; provided that each element repeats only once.To find this, we will create a separate array and copy down all the elements from the first array. Then we will traverse through the elements of the second array and check if it is already present in the union ... Read More

How to exclude a field in Gson during serialization in Java?

Aishwarya Naglot
Updated on 13-May-2025 17:38:16

754 Views

Serialization is the process that converts an object into a format that can be stored or transmitted easily. Excluding a Field in Gson During Serialization We might have some scenarios where we are asked or required to exclude a certain field from the object while serializing it. For example, we have a Student object with fields like name, age, and address. We want to serialize the object but exclude the address field for building basic information about the student in this case, we use In this article, we will learn how to exclude a field in Gson during serialization ... Read More

Differences between fromJson() and toJson() methods of Gson in Java?

Aishwarya Naglot
Updated on 13-May-2025 16:23:07

5K+ Views

Gson is a Java library which is developed by Google and used to convert Java objects into JSON and vice versa. It is mostly used in applications where data can be exchanged in JSON format. There are two useful methods in Gson that we are going to discuss in this article. These methods are fromJson() and toJson(). The fromJson() Method The fromJson() method is used to convert a JSON string into a Java object. It takes two parameters: the JSON string and the class type of the object you want to create. Here is the syntax of the fromJson() method: ... Read More

How can we serialize an array of objects using flexjson in Java?

Aishwarya Naglot
Updated on 13-May-2025 16:28:51

483 Views

Serialization is the process where we convert an object into a format that can be easily stored or transmitted and later reconstructed. The given task is to serialize an array of objects using Flexjson in Java. Serializing an Array of Objects Using Flexjson Flexjson is a lightweight Java library for serializing and deserializing java beans, maps, arrays, and collections in JSON format. We can also deserialize a JSON string to an existing object using the deserializeInto() method of the JSONDeserializer class, this method deserializes the given input into the existing object target. The values in the JSON input can overwrite ... Read More

Advertisements