A string is a sequence of characters used to store information as text. In C++, strings are represented as array of characters using the std::string class. In this article, we will learn what is an array of strings in C++, how to declare and initialize it, and how to access its elements. C++ Array of Strings Array of strings refer to an array where each element is a string. Just like a normal array, we can define an array of strings by specifying the data type as std::string and the size of the array inside square brackets. To initialize ... Read More
In this problem, you are given an integer array arr of size n and an integer S. Your task is to find an element k in the array such that if all the elements greater than k in the array are replaced with k, then the sum of all the elements of the resultant array becomes equal to S. If such an element exists, print it; otherwise, print -1. Scenario 1: Input: int S = 9; int arr[] = { 1, 3, 2, 5, 8 }; Output: ... Read More
The array sum refers to the sum of all elements in an array. The STL library of C++ provides built-in functions to easily calculate the sum of elements in an array. In this article, we will explain all those STL functions with examples. Consider the following input/output scenario to understand the concept of array sum: Input: int arr[] = {1, 2, 3, 4, 5}; Output: 15 Explanation: The sum of the elements in the array is 1 + 2 + 3 + 4 + 5 = 15. Following are the different STL functions ... Read More
The product of an array refers to the multiplication result of all the elements in the array. The STL library of C++ provides several built-in functions to quickly calculate product of an array. In this article, we will explain all those STL functions with examples. Consider the following input/output scenario to understand the concept of array product: // Input array int arr[] = {1, 2, 3, 4, 5}; // Output 120 Explanation: The product of the elements in the array is 1 * 2 * 3 * 4 * 5 = 120. Following ... Read More
The 3Sum Smaller is one of the problem variations in which we need to find the number of triplets in an array such that their sum is less than a given target. We are given an array of n integers called nums and a target value, and we have to find the number of index triplets (i, j, k) where i, j, k are indices of an array all in range 0 to n – 1, such that they satisfy the condition: nums[i] + nums[j] + nums[k]
HashSet in Java implements the Set interface, which belongs to the Java Collections Framework and is a part of the java.util package. It stores unique elements, which means we cannot store any duplicate (appearing more than once) values in a HashSet. The given task is to add all the elements of another collection to a HashSet object in Java. For example, if we have a collection ArrayList with elements [1, 3, 4, 5, 5]. Our task is to add all these elements to a HashSet, maybe to remove duplicates from the list, or for any other reason. Adding Elements from ... Read More
In general, a linked list is a linear data structure that is a collection of "nodes". Each node contains data and a pointer pointing to the next node. A doubly linked list contains an extra pointer pointing to the previous node, and using this, we can traverse forwards as well as backwards. LinkedList in Java In Java, a linked list is represented by the class named LinkedList. It is a part of the Java Collection Framework, and it belongs to the java.util package. This class implements two interfaces, namely, List and Deque. In this article, we will learn how to ... Read More
In Java, Vector is a part of the Java Collections Framework that implements a growable array of objects, which changes its size. It is thread-safe, so it can be useful in multi-threaded environments. Add elements at the middle of a Vector in Java We can add elements in the middle of a Vector in Java in the following ways: Using add() method Using insertElementAt() method Using add() Method The add() method of the java.util.Vector class accepts an integer value representing an index and an element as parameters and adds the ... Read More
The java.util.HashSet class is a part of the Java collection framework. It stores unique elements, which means we cannot store the same element more than once. A HashSet can contain only one null value. If we try to add duplicate elements to a HashSet object, the elements will not be added. Adding elements to HashSet in JavaThe following are the methods used to add elements to an existing HashSet object: Using add() method Using Collections.addAll() method Using add() Method The add() method of the HashSet class is used for adding ... Read More
The given task is to add months to a date (object) using the add() method of the Calendar class. For example, if we need to add 5 months to the date: 26-06-2025, the new date would be 26-11-2025. Calendar.add() method in Java The calender.add() method in Java is used to add a specified amount of time to any field of the Calendar. Using this method, we can add months, years, days, etc. This method accepts two parameters: the first one is the field we want to add to, and the second one is the amount we want to ... Read More