There are four different kinds of references based on the way in which the data is garbage collected.Strong referencesWeak referencesSoft referencesPhantom referencesStrong referenceIt is the default type of reference object. An object that has active strong reference can’t be garbage collected. It is possible only if the variable that is strongly referenced points to null. Let us see an example −Exampleclass Demo { //Some functionality } public class Demo_example{ public static void main(String[] args){ Demo my_inst = new Demo(); my_inst = null; } }Weak referenceThey are not default class of reference ... Read More
To support generic programming, as well as perform a stricter type check, Java implements type erasure.All type parameters in generic types are replaced with the bound (if unbounded) or object type. This way, the bytecode will only contain classes, methods, and interfaces.Type casts to preserve the type.Bridge methods are generated so as to preserve the polymorphism concept in extended generic types.Example Live Demoimport java.io.PrintStream; import java.util.*; public class Demo{ public Demo(){ } public static void main(String args[]){ List my_list = new ArrayList(); my_list.add("Hi there"); String my_str; ... Read More
Given the task is to construct a tree with a given integer N such that, the sum of degree(x) * degree(y) for all ordered pairs (x, y) is maximum and x is not equal to y.Input −N=5Output −50Explanation 1 \ 2 \ 3 \ 4 \ 5 Degree of 1st node = 1 Degree of 2nd node = 2 Degree of 3rd node = 2 Degree of 4th node ... Read More
To shuffle a list in Java, the code is as follows −Example Live Demoimport java.util.*; public class Demo{ public static void main(String[] args){ ArrayList my_list = new ArrayList(); my_list.add("Hello"); my_list.add(", "); my_list.add("this"); my_list.add("is"); my_list.add("a"); my_list.add("sample"); System.out.println("The original list is : " + my_list); Collections.shuffle(my_list); System.out.println(" The shuffled list is : " + my_list); } }OutputThe original list is : [Hello, ,, this, is, a, sample] The shuffled list is ... Read More
We are given the number N and coordinates of two points (x1, y1) and (x2, y2) for each line. The goal is to find the maximum number of lines from given lines that can pass through a single point such that no two lines cover each other, and no rotation is performed.We will represent lines as pair of (m, c) where y=mx+c and m is slope m=y2-y1/x2-x1Lines with same m are parallel given c1!=c2. We will count distinct slopes(m). For vertical lines if x1=x2, slope = INT_MAX else m.Let us understand with an example.Input Line 1 (x1, y1)=(4, 10) (x2, y2)=(2, ... Read More
We are given the dimensions of the sheet of paper, it’s Length L, and Breadth B. Also, we are given the dimensions of a small rectangle, it’s length l, and breadth b. The goal is to find the maximum number of smaller rectangles that can be cut out of a sheet of paper.We will do following steps −Firstly, we will take horizontal alignment, lengths L and l of sheet and rectangle respectively. Start aligning the L by l and B by b and count the rectangles.Then also do the same in vertical alignments. Count again. Return the maximum value of ... Read More
We are given with an array Arr[] of integers. The goal is to find longest length subarray of Arr[] , sum of whose elements is even. That is, the sum of elements of a subarray is even and that subarray is longest in length.Input − Arr[] = { 2, 3, 5, 2, 6, 7 }.Output −Maximum length of subarray − 4Explanation −The maximum length subarray is { 5, 2, 6, 7 }. Sum is 20 which is even.Input − Arr[] = { 5, 7, 7, 3, 4 }.Output − Maximum length of subarray − 4Explanation − The maximum length subarray ... Read More
The MAX_VALUE is used to find the maximum possible value for an integer in Java. Let us see an example −Example Live Demopublic class Demo{ public static void main(String[] args){ System.out.println("The type is"); System.out.println(Integer.TYPE); System.out.println("The size is"); System.out.println(Integer.SIZE); System.out.println("The max value of integer is"); System.out.println(Integer.MAX_VALUE); } }OutputThe type is int The size is 32 The max value of integer is 2147483647A class named Demo uses the Integer class and gives the various characteristics of the Integer class such as type, size ... Read More
We are given an array of candies[] of length stored in ‘size’. Each element candies[i] has a number for candies of type i.The goal is to buy as many candies as possible for any amount of money. The conditions are as given −If you purchase X[i] of type i (0
We are given a right isosceles triangle. Isosceles triangle is the one which has two sides of equal length. Right triangle is the one which has height(ag in fig.) and base (dg in fig.) perpendicular to each other. The goal is to find the maximum number of squares that can fit into this right isosceles triangle of side 2 sq units. The sides base or height (both equal) are taken as input. No. of squares is output.Refer to the below figure to understand the problemThe given triangle of height ag and base gd has 3 squares of side 2 each. ... Read More