What is a LinkedList? A Linked list is a sequence of data structures where each node contains two parts as follows - Data: The value or information stored in the node. Next: A reference (or pointer) to the next node in the sequence. Detecting a Loop in a LinkedList A loop or cycle means that the last node of a linked list is connected back to one of the nodes earlier in the list, creating a cycle. Or, the next pointer of a node points to itself. The following are the ways ... Read More
A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited. In this example, we will see how to iterate over a Set in Java using the following methods: Using forEach() Method Using Stream API Using forEach() Method The forEach() method from the Set is used for iterating over each element of the set. We can use this method to perform an action for each element in ... Read More
ArrayList class in Java is a part of the Java Collections Framework and is used for storing a dynamically sized collection of elements. It is similar to an array, but it can grow and shrink in size according to the requirements of the program.Removing Duplicate Elements from an ArrayList In an ArrayList, we can store duplicate elements. In this article, we will see how to remove duplicate elements from an ArrayList in Java. The following are the methods we will use to remove duplicate elements from an ArrayList: Using removeIf() Method ... Read More
In this article, we will learn how to insert an element at a specific index in a Java list. For example, we have a fruit list by certain order and we want to insert a new fruit at a specific index in the list. Following are the ways to insert an element at a specific index in a Java list: Using add() Method Using ListIterator Using Stream API Using add() Method The add() method of the the list interface helps us to insert an element at ... Read More
In this article, we will understand how to find prime numbers between two intervals. Prime numbers are special numbers that have only two factors, 1 and itself, and cannot be divided by any other number. 11 is a prime number. Its factors are 1 and 11 itself. Some examples of prime numbers are 2, 3, 5, 7, 11, 13, and so on. 2 is the only even prime number. All other prime numbers are odd numbers. Problem Statement Write a program in Java to display all prime numbers between two given intervals. Below is a demonstration of the same. ... Read More
The N-Queens problem is a puzzle where we need to place N queens on an N x N chessboard such that no two queens attack each other. A queen will attack another queen if they are in the same row, column, or diagonal. In this problem, you are given a value of N, and you need find possible arrangements for N queens on N x N chessboard. For example, Consider that we have a chessboard of size 4 x 4. In this case, we can place maximum 4 queens on the chessboard such that no two queens attack ... Read More
Given coordinates of three points in 2D plane, and the task is to check whether the points are collinear or not. For Example, // Input: Coordinates of three points points = {{2, 3}, {4, 6}, {6, 9}}; // Output The points are collinear. What are Collinear Points? Points are said to be collinear if they lie on the same line and they are not collinear if they are on the different lines. Given below is the figure of collinear and non-collinear points. Checking Collinearity with Area of Triangle To form a triangle, we ... Read More
In this article, we will understand the four divisors problem. We have an array of integers, and our task is to find the sum of divisors of the array elements that have exactly four divisors. If there is no such integer in the array, then return 0. Here is a detailed example given below: Example The following example implements four divisors sum problem: Input: arr = [5, 10, 15, 8] Output: 57 The explanation of the above example is as follows: Divisors of 5 = (1, 5) => count != ... Read More
In this article, our task is to break a palindromic string. We have to replace exactly one character with any lowercase English letter such that the string becomes a lexicographically smallest possible string that isn't a palindrome. Now after doing so, we have to find the final string. If there is no way to do so, then return the empty string. Example Here is an example of breaking the given palindrome string by replacing one letter and the output is a non-palindromic lexicographically smallest string: Input: abccba Output: aaccba In the above ... Read More
In Python, Arrays are commonly used to store and manage collections of data. In this article, we are going to learn how to reverse an array in groups of a given size. For example, let's consider the array and a number k, which represents the size of the array. The goal is to break the array into smaller parts, each containing k elements, and then reverse the elements inside each part. If the elements left at the end are less than k, we will reverse them. Suppose the array is [1, 2, 3, 4, 5, 6] and k=3, then ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP