Programming Articles - Page 2355 of 3363

Merging two unsorted arrays in sorted order in C++.

Narendra Kumar
Updated on 31-Oct-2019 06:03:10

492 Views

Problem statementWrite a function that takes two unsorted arrays and merges them into a new array in sorted order.arr1[] = {10, 5, 7, 2} arr2[] = {4, 17, 9, 3} result[] = {2, 3, 4, 5, 7, 9, 10, 17}Algorithm1. Merge two unsorted array into new array 2. Sort newly create arrayExample#include #include #define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) using namespace std; void mergeAndSort(int *arr1, int n1, int *arr2, int n2, int *result){    merge(arr1, arr1 + n1, arr2, arr2 + n2, result);    sort(result, result + n1 + n2); } void displayArray(int *arr, int n){    for (int i = 0; i < n; ++i) {       cout

Mid-Square hashing in C++.

Narendra Kumar
Updated on 31-Oct-2019 05:56:52

3K+ Views

Problem statementThe mid-square method is a method of generating pseudorandom numbers. This method was invented by John von Neumann and was described at a conference in 1949In this technique, an initial seed value is taken and it is squared.Some digits from the middle are extracted and these extracted digits form a number which is taken as the new seed.Let us take 3456 as seed. Its square is 11943936Take the middle 4 digits as new seed i.e. 9439. Its square is 89094721Take middle 4 digits as new seed i.e. 0947Repeat this processAlgorithm1. Choose initial seed value 2. Take the square of ... Read More

Mersenne Prime Number in C++.

Narendra Kumar
Updated on 31-Oct-2019 05:54:02

712 Views

DescriptionIn mathematics, a Mersenne prime is a prime number that is one less than a power of two. That is, it is a prime number of the form Mn = 2n − 1 for some integer n.Write a C++ program to print all Mersenne Primes smaller than an input positive integer n.The exponents n which give Mersenne primes are 2, 3, 5, 7, ... and the resulting Mersenne primes are 3, 7, 31, 127Algorithm1. Generate all the primes less than or equal to the given number n 2. Iterate through all numbers of the form 2n-1 and check if they ... Read More

Differences between org.simple.json and org.json libraries in Java?

Manisha Chand
Updated on 20-May-2025 14:46:45

3K+ Views

In Java, org.simple.json and org.json are two libraries that help in reading, writing, and manipulating JSON. But still, they are different. In this article, we are going to learn about these differences. Difference between JSON.simple vs JSON Let's see the below differences and they are - Features JSON.simple JSON ... Read More

How to convert Java array or ArrayList to JsonArray using Gson in Java?

raja
Updated on 08-Jul-2020 06:20:07

7K+ Views

The Java Arrays are objects which store multiple variables of the same type, it holds primitive types and object references and an ArrayList can represent a resizable list of objects. We can add, remove, find, sort and replace elements using the list. A JsonArray can parse text from a string to produce a vector-like object. We can convert an array or ArrayList to JsonArray using the toJsonTree().getAsJsonArray() method of Gson class.Syntaxpublic JsonElement toJsonTree(java.lang.Object src)Exampleimport com.google.gson.*; import java.util.*; public class JavaArrayToJsonArrayTest {    public static void main(String args[]) {       String[][] strArray = {{"elem1-1", "elem1-2"}, {"elem2-1", "elem2-2"}};       ArrayList arrayList = ... Read More

How to resolve "Expected BEGIN_OBJECT but was BEGIN_ARRAY" using Gson in Java?

raja
Updated on 08-Jul-2020 05:49:52

19K+ Views

While deserializing, a Gson can expect a JSON object but it can find a JSON array. Since it can't convert from one to the other, it can throw an error as "JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY" at the runtime.Exampleimport com.google.gson.Gson; public class GsonErrorTest {    public static void main(String args[]) throws Exception {       String json = "{\"employee\":[{\"name\":\"Raja Ramesh\", \"technology\":\"java\"}]}";       Gson gson = new Gson();       Software software = gson.fromJson(json, Software.class);       System.out.println(software);    } } class Software {    Employee employee; } class Employee {    String name; ... Read More

Find the value of ln(N!) using Recursion using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 06:39:33

210 Views

Suppose we have a number N, our task is to find ln(N!) using recursion. ln() is basically log base e. To solve this we can use this formula −$$\ln\lgroup N!\rgroup=\ln\lgroup N*\lgroup N-1\rgroup *\lgroup N-2\rgroup *\dotsm*2*1\rgroup=\ln\lgroup N\rgroup+\ln\lgroup N+1\rgroup+\dotsm+\ln\lgroup 1\rgroup$$Example Live Demo#include #include using namespace std; double factLog(int n) {    if (n

Find the unit place digit of sum of N factorials using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 06:37:53

214 Views

Here we will see how to get the unit place digit of the sum of N factorials. So if N is 3, then after getting sum, we will get 1! + 2! + 3! = 9, this will be the result, for N = 4, it will be 1! + 2! + 3! + 4! = 33. so unit place is 3. If we see this clearly, then as the factorials of N > 5, the unit place is 0, so after 5, it will not contribute to change the unit place. For N = 4 and more, it will ... Read More

Find the frequency of a number in an array using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 06:35:38

348 Views

Suppose we have an array. There are n different elements. We have to check the frequency of one element in the array. Suppose A = [5, 12, 26, 5, 3, 4, 15, 5, 8, 4], if we try to find the frequency of 5, it will be 3.To solve this, we will scan the array from left, if the element is the same as the given number, increase the counter, otherwise go for the next element, until the array is exhausted.Example Live Demo#include using namespace std; int countElementInArr(int arr[], int n, int e) {    int count = 0;    for(int i = 0; i

Find the frequency of a digit in a number using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 06:33:53

2K+ Views

Here we will see how to get the frequency of a digit in a number. Suppose a number is like 12452321, the digit D = 2, then the frequency is 3.To solve this problem, we take the last digit from the number, then check whether this is equal to d or not, if so then increase the counter, then reduce the number by dividing the number by 10. This process will be continued until the number is exhausted.Example Live Demo#include using namespace std; int countDigitInNum(long long number, int d) {    int count = 0;    while(number){       if((number ... Read More

Advertisements