Maximum sum of a Subarray with prime integers in Java6 Jan 2025 | 5 min read Find the greatest sum of a continuous subarray such that the subarray only consists of prime numbers given an array arr[] of integers of size n. Stated a certain way, no non-prime integer is allowed to exist in the selected subarray. Example 1: Input: int a[] = { 8, 1, 2, 4, 3, 7, 5, 11, 13 } Output: The maximum sum obtained from the prime sub-array is 39. Explanation: For the given array { 8, 1, 2, 4, 3, 7, 5, 11, 13 }, the prime subarray is given by { 3, 7, 5, 11, 13 } and the sum is (3 + 7 + 5 + 11 + 13 = 39). Hence, the maximum sum obtained from the prime sub-array is 39. Example 2: Input: int a[] = { 1, 5, 8, 4, 7, 5, 1, 3, 7 } Output: The maximum sum obtained from the prime sub-array is 12. Explanation: For the given array { 1, 5, 8, 4, 7, 5, 1, 3, 7 }, the prime subarray is given by { 7, 5 } and the sum is (7 + 5 = 12). Hence, the maximum sum obtained from the prime sub-array is 12. Example 3: Input: int a[] = { 8, 9, 7, 4, 4, 1, 10, 5, 6, 7 } Output: The maximum sum obtained from the prime sub-array is 7. Explanation: For the given array { 8, 9, 7, 4, 4, 1, 10, 5, 6, 7 }, the prime subarray is given by { 7 } and the sum is 7. Hence, the maximum sum obtained from the prime sub-array is 7. Approach: Naïve ApproachThe idea is to maintain track of the maximum total and check all potential subarrays that contain only prime numbers. Algorithm:Step 1: Set maxSum's initial value to 0. Step 2: Go through each subarray in arr in a loop. Step 2.1: Verify if the current subarray solely consists of prime numbers. Step 2.1.1: In that case, calculate out its total. Step 2.1.2: Update maxSum if its sum exceeds it. Step 3: Return with maxSum. Implementation:FileName: MaxiSubArraySum.java Output: The maximum sum obtained from the prime sub-array is 39 Complexity Analysis: The Time Complexity of the above code is O(N2*√N)), since we are first determining whether each integer in the subarray is prime, and then we are verifying all possible subarrays. The Space Complexity is O(1), which remains constant. Approach: Efficient ApproachCreating a basic linear traversal is the strategy to follow. When a prime number appears, we continue to add to the existing sum, and we also update the maximum sum each time the current sum increases. We reset the current sum to 0 if we find a non-prime number. Algorithm:Step 1: Initialize the cur_Sum and max_Sum to 0. Step 2: For every element present in the array, do the iteration. Step 2.1: In the event that the element is a prime number Step 2.1.1: Add it to cur_Sum. Step 2.1.2: Modify max_Sum so that it equals the maximum of cur_Sum and max_Sum. Step 2.2: If the element not be a prime number. Step 2.2.1: Set cur_Sum back to zero. Step 3: Give max_Sum back. Step 4: Return the highest total obtained from the primary subarray. Implementation:FileName: EfficientMaxiSumSubArray.java Output: The maximum sum obtained from the prime sub-array 39 Complexity Analysis: The Time Complexity is O(n√N), where N is the value of the highest element in the input array, and n is the number of elements in the array. The space complexity is O(1). |
The command pattern encapsulates a request as an object, thereby letting us parameterize other objects with different requests, queue or log requests, and support undoable operations. The definition is a bit confusing at first but let's step through it. In analogy to our problem above remote control...
3 min read
There are three different types of Java getInteger() methods which can be differentiated depending on its parameter. These are: Java Integer getInteger(String nm) Method Java Integer getInteger(String nm, int val) Method Java Integer getInteger(String nm, Integer val) Method 1. Java Integer getInteger(String nm) Method: The getInteger(String nm) method is a...
5 min read
The factorial of a number N is the product of all positive descending integers (integers less than or equal to N). N! = N * (N - 1) ... * 3 * 2 * 1 In this section, we will create Java programs to find the factorial of...
3 min read
In the realm of database programming, handling large text data is a common requirement. Java, being one of the most widely used programming languages, provides various mechanisms to interact with databases. One such mechanism is the (Character Large Object), which is designed specifically to manage...
5 min read
In Java, JSON plays an important role in storing data. ArrayList is a special type of Array whose size is dynamic. It is also used to store or remove data anytime. ArrayList uses all the method of List and maintain the insertion order because it implements...
3 min read
? An exception is an unwanted and unexpected error thrown in the program. Most of the time, an exception occurs when there is an error in our code but it can be handled. It disrupts the normal flow of the code. For example, the code throws an exception...
4 min read
Java offers a potent feature known as classes for object-oriented programming. An object may be created using a class as a blueprint since it has both data and behaviour. There are concrete classes that may be instantiated directly in addition to abstract classes that define shared...
4 min read
Java is a flexible programming language that offers a number of data structures for organising data sets. Maps, such as HashMap and TreeMap, are essential in situations where mapping keys to values is required. There are circumstances, nevertheless, in which you must link a key to more...
4 min read
Matrices are an essential part of linear algebra and computer programming. They are used in various applications, including image processing, data manipulation, and numerical simulations. One common task when dealing with matrices is computing the sum of elements along the main diagonal. In this article, we...
5 min read
In the world of programming, there are numerous occasions when you'll need to work with images and handle them as bytes. Whether you are dealing with file uploads, network protocols, or any other scenario where we need to transmit or manipulate image data, knowing how to...
5 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