Programming Articles - Page 2362 of 3366

Find a Symmetric matrix of order N that contain integers from 0 to N-1 and main diagonal should contain only 0’s in C++

Arnab Chakraborty
Updated on 24-Oct-2019 13:02:41

142 Views

Here we will see how to generate one symmetric matrix of order N, and the elements of each row will contain numbers from 0 to N – 1. The diagonal elements will be 0 always.This task is easy, we will form a matrix of N x N, then for each row i and for each column j, if i and j are same, then mark it as 0, otherwise increase one counter from 1 to N – 1, place the values for each individual row.Example#include using namespace std; void makeSymmetricMatrix(int n) {    int matrix[n][n];    for(int i = 0; i

Find a subset with greatest geometric mean in C++

Arnab Chakraborty
Updated on 24-Oct-2019 12:58:09

164 Views

Here we have an array A with some elements. Our task is to find the subset where the geometric mean is maximum. Suppose A = [1, 5, 7, 2, 0], then the subset with greatest geometric mean will be [5, 7].To solve this, we will follow one trick, we will not find the mean, as we know that the largest two elements will form the greatest geometric mean, so the largest two elements will be returned as subset.Example#include using namespace std; void largestGeoMeanSubset(int arr[], int n) {    if (n < 2) {       cout max) ... Read More

Find a point such that sum of the Manhattan distances is minimize in C++

Arnab Chakraborty
Updated on 24-Oct-2019 12:55:39

689 Views

Suppose we have n different points in K dimension space, the value of n is in range (2, 105), and value of k in range (1 to 5). We have to determine the point such that the sum of Manhattan distance from resultant point to n points is minimized.The Manhattan distance between two points P1(x1, y1) and P2(x2, y2), is |x1 – x2| + |y1 – y2|. Suppose dimension is 3, and there are three points like (1, 1, 1), (2, 2, 2), (3, 3, 3), then the output will be (2, 2, 2).To solve this problem, we have to ... Read More

How to implement custom FieldNamingStrategy using Gson in Java?

raja
Updated on 07-Jul-2020 12:39:14

1K+ Views

The FieldNamingStrategy is a mechanism for providing custom field naming in Gson. This allows the client code to translate field names into a particular convention that is not supported as a normal Java field declaration rules. The translateName() method will prefix every field name with the string “pre_”.In the below example, we can implement the Custom FieldNamingStrategy.Exampleimport java.lang.reflect.Field; import com.google.gson.*; public class GsonFieldNamingStrategyTest {    public static void main(String[] args) {       Employee emp = new Employee();       emp.setEmpId(115);       emp.setFirstName("Adithya");       emp.setLastName("Jai");       CustomFieldNamingStrategy customFieldNamingStrategy = new CustomFieldNamingStrategy();     ... Read More

Find a pair with the given difference in C++

Arnab Chakraborty
Updated on 24-Oct-2019 12:53:24

413 Views

Consider we have an array A, there are n different elements. We have to find a pair (x, y) from the array A, such that the difference between x and y is same as given difference d. Suppose a list of elements are like A = [10, 15, 26, 30, 40, 70], and given difference is 30, then the pair will be (10, 40) and (30, 70)To solve this problem, we will assume that the array is sorted, then starting from left we will take two pointers to point elements, initially first one ‘i’ will point to the first element, ... Read More

Find a pair with maximum product in array of Integers in C++

Arnab Chakraborty
Updated on 24-Oct-2019 12:50:20

205 Views

Consider we have an array A, there are n different elements. We have to find a pair (x, y) from the array A, such that the product of x and y is maximum. The array may contain positive or negative elements. Suppose an array is like: A = [-1, -4, -3, 0, 2, -5], then the pair will be (-4, -5) as product is maximum.To solve this problem, we have to keep track four numbers, the positive_max, positive_second_max, negative_max, negative_second_max. At the end if the (positive_max * positive_second_max) is greater than (negative_max * negative_second_max), then return positive pairs, otherwise return ... Read More

Find a pair of elements swapping which makes sum of two arrays same in C++

Arnab Chakraborty
Updated on 24-Oct-2019 12:44:48

412 Views

Consider we have two arrays with different number of elements. We have to find a pair of elements (x, y), where x is present in the first array, and y is present at the second array. The pair will be chosen such that after swapping the elements between these two arrays, the sum of these two arrays will be same.Suppose first array A is holding [4, 1, 2, 2, 1, 1] and B is holding [3, 3, 6, 3], now the sum of A is 11 and sum of B is 15, we will take a pair like (1, 3), ... Read More

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

Arnab Chakraborty
Updated on 24-Oct-2019 12:40:45

351 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

Find a Fixed Point in an array with duplicates allowed in C++

Arnab Chakraborty
Updated on 24-Oct-2019 12:37:06

193 Views

Here we will see how to find fixed point in a given array. In array one element will be denoted as fixed point if the value is same as its index. This program will return the value if any, otherwise return -1. The array can hold negative numbers also. And the data elements are sorted. Here duplicate elements are allowed in the array.Here we will use binary search approach to solve this problem in O(log n) time. But we need some modification, if the normal binary search is used, then it may fail for duplicate elements. To check left, we ... Read More

Find a Fixed Point (Value equal to index) in a given array in C++

Arnab Chakraborty
Updated on 24-Oct-2019 12:34:24

258 Views

Here we will see how to find fixed point in a given array. In array one element will be denoted as fixed point if the value is same as its index. This program will return the value if any, otherwise return -1. The array can hold negative numbers also. And the data elements are sorted.Here we will use binary search approach to solve this problem in O(log n) time. At first we will check whether the middle element is fixed point or not, if yes, then return it, if not, then there will be two situations, if the index of ... Read More

Advertisements