Maximize Number of Continuous Automorphic Numbers in C++

Sunidhi Bansal
Updated on 17-Aug-2020 09:05:49

314 Views

Given the task is to maximize the number of continuous Automorphic elements in a given array with N number of elements.An automorphic number is a number whose square ends with the same digits as the number itself. For example 5 is an automorphic number as 5*5 = 25 and 25 ends with 5.Let’s now understand what we have to do using an example −Input − arr[]={5, 3, 625, 6, 8, 1}Output − 2Explanation − Automorphic numbers present in the above array are 5, 625, 6 and 1 but the maximum continuous automorphic numbers are {625, 6} which makes the output ... Read More

Unreachable Statement Using Final Variable in Java

AmitDiwan
Updated on 17-Aug-2020 09:04:48

210 Views

Unreachable statements are those that don’t get executed when the code is being executed. This could be so because −There is a return statement before the code.There is an infinite loop in the code.The execution of the code is terminated forcibly before it executes.Here, we will see how the unreachable statement can be used with the ‘final’ keyword −Example Live Democlass Demo_example{    final int a = 56, b = 99;    void func_sample(){       while (a < b){          System.out.println("The first value is less than the second.");       }       System.out.println("This ... Read More

Maximize Array Size by Deleting K Sub-Arrays to Make It Prime in C++

Sunidhi Bansal
Updated on 17-Aug-2020 09:03:00

144 Views

Given the task is to delete exactly K sub-arrays from a given array Arr[] with N positive elements such that all the remaining elements in the array are prime and the size of the remaining array is maximum.Input Arr[]={4, 3, 3, 4, 3, 4, 3} , K=2Output 3Explanation − K=2, this means only 2 sub-arrays must be deleted.The sub-arrays deleted are Arr[0] and Arr[3…5] which leaves the array Arr[]={3, 3, 3} with all the prime elements and the maximum size possible.Input Arr[]={7, 6, 2, 11, 8, 3, 12}, K=2Output 3Explanation − The sub-arrays deleted are Arr[1] and Arr[4…6] and the remaining prime array is ... Read More

Types of References in Java

AmitDiwan
Updated on 17-Aug-2020 09:02:34

863 Views

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

Type Erasure in Java

AmitDiwan
Updated on 17-Aug-2020 08:59:55

179 Views

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

Maximize the Sum of Products of Degrees Between Vertices in C++

Sunidhi Bansal
Updated on 17-Aug-2020 08:59:08

308 Views

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

Shuffle or Randomize a List in Java

AmitDiwan
Updated on 17-Aug-2020 08:56:23

306 Views

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

Maximum Distinct Lines Passing Through a Single Point in C

Sunidhi Bansal
Updated on 17-Aug-2020 08:55:56

244 Views

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

Maximum Given Sized Rectangles That Can Be Cut Out of a Sheet of Paper in C

Sunidhi Bansal
Updated on 17-Aug-2020 08:53:52

445 Views

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

Maximum Length of Subarray with Even Sum in C++

Sunidhi Bansal
Updated on 17-Aug-2020 08:52:38

931 Views

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

Advertisements