Number of GP sequence Problem in Java10 Sept 2024 | 6 min read The number of GP sequence problems in Java involves determining the count of valid geometric progression sequences within a given set of numbers. Geometric progressions, defined by a common ratio, are essential in various fields. In this tutorial, we will find the count of GP sequence that has the length 3. Example 1: Input: inputArray[] = {1, 2, 6, 2, 3, 6, 9, 18, 3, 9}, commonRatio = 3 Output: The count of GP subsequences with length 3 and common ratio 3 is: 6 Explanation: The program calculates that there are 6 geometric progression (GP) subsequences of length 3 with a common ratio of 3 in the specified array. Example 2: Input: inputArray[] = {1, 3, 9, 27, 81}, commonRatio = 3 Output: The count of GP subsequences with length 3 and common ratio 3 is: 3 Explanation: The GP subsequences are {1, 3, 9}, {3, 9, 27}, and {9, 27, 81}. Approach: Naive ApproachIn the naive approach, the algorithm iterates through every triplet of elements in the array, checking if they form a geometric progression subsequence. The triple-nested iteration results in cubic time complexity, making it less efficient for larger input sizes due to its exhaustive nature. Algorithm:Step 1: Initialize Two HashMaps: Create left and right HashMaps to count the occurrences of each element to the left and right of the current element in the array. Step 2: Populate Right HashMap: Iterate through the array and populate the right HashMap with the count of each element. Step 3: Count GP Subsequences by Iterate through each array element. Step 4: For the current element array[i]: Step 4.1: Calculate countLeft using the left HashMap if array[i] is divisible by the ratio. Step 4.2: Reduce array[i]'s count in the right HashMap. Step 4.3: Determine countRight for array[i] * ratio in the right HashMap. Step 4.4: Multiply countLeft and countRight to update the total subsequence count (result). Step 4.5: Increment array[i]'s count in the left HashMap. Step 6: Return Total Count: Return the total count of subsequences (result) after iterating through the array. Implementation:Filename: MyGPProgram.java Output: The count of GP subsequences with length 3 and common ratio 3 is: 6 Time Complexity: The time complexity is O(n) as it utilizes two loops that iterate through the input array, and the operations inside each loop are constant time, resulting in a linear overall time complexity. Auxiliary Space: The auxiliary space complexity is O(n) because the code employs two HashMaps to maintain counts of elements, and the space required by these HashMaps scales linearly with the size of the input array. Approach: Counting Geometric Progression (G.P.) SubsequencesThe code employs a counting approach for Geometric Progression (G.P.) subsequences, utilizing hash maps to track element occurrences and efficiently calculate the count based on the given ratio. Additionally, the code leverages binomial coefficients to compute combinations, enhancing the accuracy of G.P. subsequence counts. Algorithm:Step 1: Calculate the binomial coefficient (n choose k) using dynamic programming in binomialCoeff. Step 2: In countGPSubsequences, use left and right HashMaps to track the counts of elements to the left and right of the current element. Step 3: If the ratio is 1, calculate GP subsequences using the binomial coefficient for each element's count. Step 4: if not, iterate through the array:
Step 5: Define the array, size, and the common ratio. Call countGPSubsequences and print the result. Implementation:Filename: GPSequence.java Output: Count of GP subsequences with commonRatio 3: 6 Time Complexity: The outer loop in countGPSubsequences iterates through all array elements once, resulting in a time complexity of O(n), where n is the size of the input array. Auxiliary Space: The space complexity is influenced by the two hash maps (left and right). In the worst case, where all elements are unique, the space complexity would be O(n) since each element may have an entry in both hash maps. Next TopicPattern Matching for Switch |
Carol Number A Carol number is a special type of number derived from a simple mathematical formula. It is defined as: C_n = (2^n - 1)^2 - 2 Where, n is a positive integer. 2^n - 1 is a Mersenne...
6 min read
In Java, when we create an object of the class, the constructor of the class is always called, by default. We can count the number of objects directly if we keep tracking how many times the constructor is called. In this section, we will learn how...
2 min read
Java is the developer's first choice to write code. It is a very popular and successful programming language to build applications. The count of the Java developer is increased day by day. It is mostly used to develop web and mobile applications. In order to become...
5 min read
In the world of programming, basic arithmetic operations like addition are often taken for granted. We rely on operators like '+' to perform addition effortlessly. However, have you ever wondered how to add two numbers without using any operators in Java? In this section, we will...
8 min read
Abstraction Vs Encapsulation Java is an object-oriented programming language and it follows OOPs concepts. The OOPs concepts include classes, objects, polymorphism, inheritance. There are two other features of OOPs i.e. abstraction and encapsulation. They both seem very similar but totally different in concept and implementation. The major...
3 min read
The Java IntSummaryStatistics class's getMin() function is used to determine the minimum number of records in this IntSummaryStatistics. Syntax: public int getMin() Parameter: There are no values that can be passed as parameters to this method. Return Value: The minimal number of records in this IntSummaryStatistics are returned by...
2 min read
Expert programmers and new learners both encounter exciting obstacles during their code writing experiences. The widely popular programming language Java drives execution in a vast number of applications that span between enterprise solutions and mobile applications. Students and professionals who handle Java code usually struggle with the...
4 min read
? Problem Formulation: A list of letters and digits is provided. How to split up the string using the border between a letter and a number, and inversely, into substrings of either letters or numbers. Split string without a predefined function Divide the string str into three segments: a...
3 min read
? Every software application requires a username and password in order to authenticate the valid user. A username can be anything like an email-id or just a combination of characters. But while creating a password, one must be very careful. Because anyone with valid credentials can enter...
10 min read
with Examples In Java, an exception is an event that occurs during the execution of a program and disrupts the normal flow of the program's instructions. Bugs or errors that we don't want and restrict our program's normal execution of code are referred to as...
10 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India