Coding Practice Articles

Page 2 of 3

Longest consecutive subsequence in C++

Jiya Garg
Jiya Garg
Updated on 02-Sep-2024 228 Views

Given an array of integers, determine the length of the longest subsequence where the elements are consecutive integers, regardless of their order within the subsequence. Input  arr = {100, 4, 200, 1, 3, 2} Output  Length of the longest consecutive sequence is: 4 Different approaches for longest consecutive subsequence The following are the approaches to get longest consecutive subsequence By sorting the Array By using Set Approach 1: By Sorting the ArrayBelow are the steps to get the longest consecutive subsequence in C++ using Array Sort ...

Read More

Java Program to Recursively Remove All Adjacent Duplicates

Shriansh Kumar
Shriansh Kumar
Updated on 16-Aug-2024 851 Views

The problem statement states that we have given a String str of length N (where N is an integer) containing alphanumeric characters. We need to recursively remove all adjacent duplicate characters so that the resultant string does not contain any adjacent duplicate characters. We can use a recursive or iterative approach to solve the problem. Here, we first remove the adjacent duplicate elements from the left part of the string. After that, we recursively remove the adjacent duplicates from the right part of the string. Example Scenario 1: Input: str1 = "tuttor"; Output: res = tuor The adjacent duplicate ...

Read More

Java Program to add year to current date using Calendar.add method

Shriansh Kumar
Shriansh Kumar
Updated on 16-Aug-2024 2K+ Views

The Calendar class of java.util package provides a method with the name add(). This method accepts current date and amount of time as parameter values. If the given amount of time is positive, it will add it to the current date, and in the case of negative time, it will subtract it. In this article, we will see a few Java programs to add year to the current date using the Calendar.add() method. Example Scenario: Input 1: current_date = Thu Nov 22 18:19:06 UTC 2018 Input 2: year_to_add = 12 Output: new_date = Thu Nov 22 18:19:06 UTC 2030 ...

Read More

Java Program to Compute the Running Total of a List

Shriansh Kumar
Shriansh Kumar
Updated on 30-Jul-2024 1K+ Views

A running total (also known as an accumulated total) of a list represents the sum of a sequence of elements. As new elements are added to the sequence, the running total continuously updates. To perform this operation in Java, we can use for loop and while loop in our program. The program will have a time complexity of O(n), where n is the number of elements in the list because it loops through the list once and performs a constant-time operation for each element in the list. List is an interface of Java Collection Framework that stores collections of objects. ...

Read More

Java Program to Create a Matrix and Fill it With Prime Numbers

Shriansh Kumar
Shriansh Kumar
Updated on 30-Jul-2024 842 Views

As per the problem statement, we have to create an empty matrix and fill that matrix with the prime numbers starting from the smallest prime number i.e. 2. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. For instance, let's take the number 7. When we divide 7 by any number other than 1 and 7, we get a remainder. For example, dividing 7 by 2 gives a remainder of 1, and dividing 7 by 3 gives a remainder of 1 as well. Therefore, 7 has no divisors other ...

Read More

Count Alternating Subarray

Revathi Satya Kondra
Revathi Satya Kondra
Updated on 23-Jul-2024 325 Views

The Count Alternating Subarrays are used to count the number of subarrays where no two adjacent elements are similar. we can also call these subarrays as alternating subarrays. Problem Statement Before understanding what is "Count Alternating Subarrays" let's see what is a sub-array, and alternating sub-arrays. A subarray is part of an array formed by removing some or no prefixes of the array and removing some or no suffix elements of the given array. While dividing an array into multiple sub-arrays there ...

Read More

Program to find the Maximum Prime Difference

Revathi Satya Kondra
Revathi Satya Kondra
Updated on 23-Jul-2024 231 Views

The Maximum Prime Difference is a problem used to determine the largest difference between indices of two prime numbers in a given array. Problem Statement Here, we have given an array of integers as nums. our task is to find the maximum prime difference between the indices of any two prime numbers in an array. In the given array if we have only one prime number then it returns 0 and if no prime number returns -1. Example Scenario 1 Input: arr = [11, 4, 7, 6, 13] Output: 4 The prime numbers are 11 (index 0), ...

Read More

Minimum Operations to Make Median of Array Equal to K

Revathi Satya Kondra
Revathi Satya Kondra
Updated on 23-Jul-2024 330 Views

The problem “Minimum Operations to Make the Median of the Array Equal to K” is used to adjust the elements of an integer array so that its median becomes equal to a given value k. In one operation, you can increase or decrease any element by 1. Problem Statement The goal is to find the minimum number of such operations to make the median of the array equal to K. The median of an array is the middle element when the array is sorted in non-decreasing order. The larger one is considered the median if there are two middle ...

Read More

Longest Strictly Increasing or Strictly Decreasing Subarray

Revathi Satya Kondra
Revathi Satya Kondra
Updated on 23-Jul-2024 792 Views

The Longest Strictly Increasing or Strictly Decreasing Subarray problem is used to find the maximum length of the contiguous subarray within a given array where the elements are either strictly increasing or strictly decreasing. Problem Statement Given an array of integers nums, return the length n of the longest subarray of n which is either strictly increasing or strictly decreasing. Example Scenario 1 Input: nums = [1, 3, 2, 4, 3, 5, 4, 6] Output: n = 2 The longest strictly increasing subarrays are [1, 3], [2, 4], [3, 5], and [4, 6]. The longest strictly decreasing subarrays ...

Read More

Latest Time You Can Obtain After Replacing Characters

Revathi Satya Kondra
Revathi Satya Kondra
Updated on 23-Jul-2024 165 Views

The Latest Time You Can Obtain After Replacing Characters sub-task is applied to an input string, in which the string is represented as a 12-hour format time when the maximum number of characters are replaced by '?'. In a 12-hour format time, "HH:MM” where HH is an element from the set {00, 01, …, 10, 11} and MM is also an element from the set {00, 01, …, 59}. The earliest possible time is 00:00, and the latest time is 11:59. Problem Statement In this problem statement, the goal is to replace all "?" characters in the string s ...

Read More
Showing 11–20 of 21 articles
Advertisements