163 subscribers
3.42K photos
24 videos
39 files
371 links
Link: @java_posts

Contact: java.response.email@gmail.com
Download Telegram
Using clone() Method
The clone() method is the method of Object class. It creates a copy of an object and returns the same copy. The JVM creates a new object when the clone() method is invoked. It copies all the content of the previously created object into new one object. Note that it does not call any constructor. We must implement the Cloneable interface while using the clone() method. The method throws CloneNotSupportedException exception if the object's class does not support the Cloneable interface. The subclasses that override the clone() method can throw an exception if an instance cannot be cloned
#Source_code
#57
Given an integer array, find the maximum length subarray having a given sum.
#Data_structure
Java
Given an integer array, find the maximum length subarray having a given sum. #Data_structure
code.png
302.8 KB
The problem differs from the problem of finding the maximum length subsequence with given sum. Unlike subsequences, subarrays are required to occupy consecutive positions within the original array.
#Source_code
#Data_structure
Java
Given an integer array, find the maximum length subarray having a given sum. #Data_structure
code.png
373.4 KB
We can use a map to solve this problem in linear time. The idea is to create an empty map to store the first subarray’s ending index, having a given sum. We traverse the given array and maintain the sum of elements seen so far.

* If the target is seen for the first time, insert the sum with its index into the map.
* If target-S is seen before, there is a subarray with the given sum that ends at the current index, and we update the maximum length subarray having sum S if the current subarray has more length.
#Source_code
#Data_strucutre
Assigning a Variable to the int Variable.
To print the ASCII value of a character, we need not use any method or class. Java internally converts the character value to an ASCII value.
#Source_code
#58
To reverse a number, follow the steps given below:
* First, we find the remainder of the given number by using the modulo (%) operator.
* Multiply the variable reverse by 10 and add the remainder into it.
* Divide the number by 10.
#Source_code
#60
In the following program, we have replaced the while loop by a for loop. It also removes the last digit of the number, after each iteration. When the condition, number!=0 becomes false, the loop exits and we get the reversed number.
#Source_code
#61
The following program reverses both numbers, positive and negative. When we enter a number, it first checks the number is positive or negative. If the number is negative, it converts the number into positive by multiplying -1. After that, it performs the same steps (as we have performed in the above programs) to reverse a number. At last, again it checks the number is negative or positive. To make the number negative, it again multiplies the reverse number by -1.
#Source_code
#62