Overloading is one of the mechanisms to achieve polymorphism where, a class contains two methods with same name and different parameters. It allows one method to perform different tasks based on the parameters passed to it. Whenever you call this method, the method body will be bound with the method call based on the parameters we pass to it. Constructor Overloading in Java Constructors are special methods that are called when an object is created. Constructor overloading in Java allows a class to have multiple constructors with different parameters. This is useful when you want to create objects in ... Read More
A Constructor in Java is similar to a method, and it is invoked at the time of creating an object of the class, it is generally used to initialize the instance variables of a class. The constructors have same name as their class. The main purpose of a constructor is to initialize the instance variables of a class. Return Type of Constructor In Java, constructors do not have a return type, not even void. They are used to initialize the object when it is created. A constructor doesn’t have any return type. ... Read More
The Collection is a framework that provides architecture to store and manipulate the group of objects. Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion. In this article we will look at how to get the size of a collection in Java. The size of a collection is the number of elements it contains. Following are the ways to get the size of a collection in Java: Using size() Method Using loops Using Streams API Getting Size of Collection Using size() Method The size() method is ... Read More
The List extends Collection and declares the behavior of a collection that stores a sequence of elements. The Collection is a framework that provides architecture to store and manipulate the group of objects. Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion. In this article, we will learn to rotate elements of a list in Java. Rotating a list means shifting the elements of the list either to the left or to the right. Following are the ways to rotate elements of a list in Java: ... Read More
In this article, we will explain how to find the minimum and maximum values in a binary search tree (BST), with implementation in JavaScript. A binary search tree is a data structure that stores data in a sorted order such that for every node, the left subtree contains values less than the node's value, and the right subtree contains values greater than the node's value. So the leftmost node will have the minimum value, and the rightmost node will have the maximum value. Find Minimum and Maximum in a BST Given root node of a binary search tree, ... Read More
The binary search algorithm is the fastest algorithm to search for an element in a sorted array. It works by dividing the array into two halves and checking if the target element is in the left or right half, thus reducing the search space by half with each iteration. Binary Search Using Recursion The following are the steps to implement binary search using recursion: Define a binSearch() function that accepts the array, the left index, the right index, and the target that you want to search. Check if the ... Read More
We are given an array of N integers and the task is to find the length of the longest subarray whose elements sum up to zero. A subarray is a continuous sequence of elements within the array. If no such subarray exists, return 0. Let's look at a few example scenarios to understand the problem better- Scenario 1- Input: N = 7 arr = {1, 2, -3, 3, -1, 2, -2} Output: 5 Explanation: The subarray {2, -3, 3, -1, 2} has a sum of 0 and its length is 5, which is the longest such ... Read More
We are given an array of unsorted integers of size N. The task is to find the distinct maximum and second maximum elements which are present in the array. The array may contain duplicate elements also, so we have to find only distinct elements. If there is no second maximum, we will return -1 for the second maximum. Let's look at some example scenarios to understand the problem clearly - Scenario 1- Input: N = 5, A[] = {2, 2, 1, 3, 4} Output: 4 and 3 Explanation: From the given array, we can see that '4' is ... Read More
We are given two strings, a and b and the task is to find whether we can obtain the string b by rotating string a exactly two places in either an anticlockwise (left) or clockwise (right) direction. Let's take an example scenario to understand the problem clearly — Scenario 1- Input: a = "google", b = "legoog" Output: True Explanation: Rotating "google" two places anticlockwise gives "legoog", which matches string b. Thus, we return True. Scenario 2- Input 2: a = "tuorialst", b = "tutorials" Output: False Explanation: String "tuorialst" cannot be ... Read More
We are given an array of unsorted integers, and the task is to find the missing positive integer. The given array may contain negative numbers, zeros, and duplicate values, all in any random order. Let's look at some example scenarios to understand the problem clearly- Scenario 1- Input: arr = [3, -2, 5, 1, -7, 4, -1, 8] Output: 2 Explanation: The number 2 is not present in the array, so the missing positive is 2 Scenario 2- Input: arr = [0] Output: 1 Explanation: In the given array, '1' is the only positive ... Read More