Articles on Trending Technologies

Technical articles with clear explanations and examples

Find smallest subarray that contains all elements in same order in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Dec-2019 426 Views

Suppose we have two arrays of size m and n, The task is to find minimum length subarray in the first array, that contains all the elements if the second array. Element in second array may be present in the large array in non-contiguous but order must be same. So if two arrays are like A = [2, 2, 4, 5, 8, 9], and B = [2, 5, 9], then the output will be 5. As the smallest subarray of A, will be [2, 4, 5, 8, 9]. Here all elements like [2, 5, 9] are in the same order. ...

Read More

Find the Nth term of the series where each term f[i] = f[i – 1] – f[i – 2] in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Dec-2019 176 Views

Suppose we have a series called f. Each term of f, follows this rule f[i] = f[i – 1] – f[i – 2], we have to find the Nth term of this sequence. f[0] = X and f[1] = Y. If X = 2 and Y = 3, and N = 3. The result will be -2.If we see this closely, there will be almost six terms before the sequence starts repeating itself. So we will find the first 6 terms of the series and then the Nth term will be the same as (N mod 6)th term.Example#include< iostream> using ...

Read More

Find the altitude and area of an isosceles triangle in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Dec-2019 402 Views

Consider we have the side of the isosceles triangle, our task is to find the area of it and the altitude. In this type of triangle, two sides are equal. Suppose the sides of the triangle are 2, 2 and 3, then altitude is 1.32 and the area is 1.98. Altitude(h)=$$\sqrt{a^{2}-\frac{b^{2}}{2}}$$ Area(A)=$\frac{1}{2}*b*h$ Example #include #include using namespace std; float getAltitude(float a, float b) {     return sqrt(pow(a, 2) - (pow(b, 2) / 4)); } float getArea(float b, float h) {     return (1 * b * h) / 2; } int main() {     float a = 2, b = 3;     cout

Read More

How to detect duplicate values in primitive Java array?

karthikeya Boyini
karthikeya Boyini
Updated on 19-Dec-2019 2K+ Views

To detect the duplicate values in an array you need to compare each element of the array to all the remaining elements, in case of a match you got your duplicate element.One solution to do so you need to use two loops (nested) where the inner loop starts with i+1 (where i is the variable of outer loop) to avoid repetitions in comparison.Exampleimport java.util.Arrays; import java.util.Scanner; public class DetectDuplcate {        public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter the size of the array that is to ...

Read More

How to create array of strings in Java?

Sai Subramanyam
Sai Subramanyam
Updated on 19-Dec-2019 2K+ Views

In Java, you can create an array just like an object using the new keyword. The syntax of creating an array in Java using new keyword −type[] reference = new type[10];Where, type is the data type of the elements of the array.reference is the reference that holds the array.And, if you want to populate the array by assigning values to all the elements one by one using the index −reference [0] = value1; reference [1] = value2;You can declare an array of Strings using the new keyword as &mius;String[] str = new String[5]; And then, you can populate the string ...

Read More

Find minimum difference between any two element in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Dec-2019 824 Views

Suppose we have an array of n elements called A. We have to find the minimum difference between any two elements in that array. Suppose the A = [30, 5, 20, 9], then the result will be 4. this is the minimum distance of elements 5 and 9.To solve this problem, we have to follow these steps −Sort the array in non-decreasing orderInitialize the difference as infiniteCompare all adjacent pairs in the sorted array and keep track of the minimum oneExample#include #include using namespace std; int getMinimumDifference(int a[], int n) {    sort(a, a+n);    int min_diff = INT_MAX;    for (int i=0; i

Read More

How to add elements to the midpoint of an array in Java?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 19-Dec-2019 306 Views

Apache commons provides a library named org.apache.commons.lang3 and, following is the maven dependency to add library to your project.           org.apache.commons       commons-lang3       3.0     This package provides a class named ArrayUtils. You can add an element at a particular position in an array using the add() method of this class.Exampleimport java.util.Arrays; import org.apache.commons.lang3.ArrayUtils; public class AddingElementToMidPoint {    public static void main(String args[]) {       int[] myArray = {23, 93, 30, 56, 92, 39};       int eleToAdd = 40;       int position= myArray.length/2;       int [] result = ArrayUtils.add(myArray, position, eleToAdd);       System.out.println(Arrays.toString(result));    } }Output[23, 93, 30, 40, 56, 92, 39]

Read More

Find three closest elements from given three sorted arrays in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Dec-2019 457 Views

Suppose we have three sorted arrays A, B and C, and three elements i, j and k from A, B and C respectively such that max(|A[i] – B[i]|, |B[j] – C[k]|, |C[k] – A[i]|) is minimized. So if A = [1, 4, 10], B = [2, 15, 20], and C = [10, 12], then output elements are 10, 15, 10, these three from A, B and C.Suppose the size of A, B and C are p, q and r respectively. Now follow these steps to solve this −i := 0, j := 0 and k := 0Now do the following ...

Read More

What is the difference between non-static methods and abstract methods in Java?

radhakrishna
radhakrishna
Updated on 19-Dec-2019 2K+ Views

Following are the notable differences between non-static methods and abstract methods.Non-static (normal) methodsAbstract methodsThese methods contain a body.Abstract methods don’t have body these are ended with a semicolonYou can use normal method directly.You cannot use abstract methods directly, to use them you need to inherit them and provide body to these methods and use them.Example:public void display() {    System.out.println("Hi"); }Example:public void display();

Read More

Find pair with maximum difference in any column of a Matrix in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 165 Views

Suppose we have one matrix or order NxN. We have to find a pair of elements which forms maximum difference from any column of the matrix. So if the matrix is like −123535967So output will be 8. As the pair is (1, 9) from column 0.The idea is simple, we have to simply find the difference between max and min elements of each column. Then return max difference.Example#include #define N 5 using namespace std; int maxVal(int x, int y){    return (x > y) ? x : y; } int minVal(int x, int y){    return (x > y) ? ...

Read More
Showing 55971–55980 of 61,297 articles
Advertisements