Knowee
Questions
Features
Study Tools

Given an array of integers A, print true if we can partition the array into three non-empty subarrays with equal sums.Input FormatThe first line of the input contains an integer N. Second line of input contains an array of size N.Output FormatPrint true if we can partition the array, otherwise false.Constraints3 ≤ N ≤ 104-104 ≤ Ai ≤ 104ExampleInput103 3 6 5 -2 2 5 1 -9 4OutputtrueExplanation(3 + 3) = (6) = (5 - 2 + 2 + 5 + 1 - 9 + 4) = 6.

Question

Given an array of integers A, print true if we can partition the array into three non-empty subarrays with equal sums.Input FormatThe first line of the input contains an integer N. Second line of input contains an array of size N.Output FormatPrint true if we can partition the array, otherwise false.Constraints3 ≤ N ≤ 104-104 ≤ Ai ≤ 104ExampleInput103 3 6 5 -2 2 5 1 -9 4OutputtrueExplanation(3 + 3) = (6) = (5 - 2 + 2 + 5 + 1 - 9 + 4) = 6.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Python solution for the problem:

def can_partition(nums):
    total_sum = sum(nums)
    if total_sum % 3 != 0:
        return False

    count = 0
    sub_sum = 0
    for num in nums:
        sub_sum += num
        if sub_sum == total_sum / 3:
            sub_sum = 0
            count += 1

    return count >= 3

# Test the function
nums = [3, 3, 6, 5, -2, 2, 5, 1, -9, 4]
print(can_partition(nums))  # Output: True

Here is the step-by-step explanation:

  1. Calculate the total sum of the array. If the total sum is not divisible by 3, return False because it's impossible to partition the array into three equal sums.

  2. Initialize a counter and a variable to keep track of the current subarray sum.

  3. Iterate over the array. For each number, add it to the current subarray sum.

  4. If the current subarray sum equals to the total sum divided by 3, reset the subarray sum to 0 and increment the counter.

  5. After the loop, if the counter is greater than or equal to 3, return True. This means we can partition the array into at least three subarrays with equal sums. Otherwise, return False.

This problem has been solved

Similar Questions

Given an integer array nums, return true if you can partition the array into two subsets such that the sum of the elements in both subsets is equal or false otherwise. Example 1:Input: nums = [1,5,11,5]Output: trueExplanation: The array can be partitioned as [1, 5, 5] and [11].Example 2:Input: nums = [1,2,3,5]Output: falseExplanation: The array cannot be partitioned into equal sum subsets. Constraints:1 <= nums.length <= 2001 <= nums[i] <= 100

Implement a Java program to Find the subarray of size 3 with the greatest sum.Input FormatThe input consists of a single line where the user is prompted to enter 30 integers, separated by spaces.ConstraintsThe array contains exactly 30 integers.Output FormatThe output consists of two lines: The first line contains the subarray with the greatest sum separated by spaces. The second line contains the sum of the subarray.Sample Input 010 20 30 40 50 60 70 80 90 100 11 22 33 44 55 66 77 88 99 9 8 7 6 5 4 3 2 1 99 88Sample Output 0Subarray with the Greatest Sum: 80 90 100Sum of the Subarray: 270Contest ends in an hourSubmissions: 0Max Score: 10Difficulty: MediumRate This Challenge: More Java 151import java.io.*;2import java.util.*;3​4public class Solution {5​6    public static void main(String[] args) {7        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */8   }9}

Given an array of integers of size N, print the sum of sum of all subarrays.Input FormatFirst line of input contains T - number of test cases. Its followed by 2T lines, the first line contains N - size of the array and second line contains the elements of the array.Output FormatFor each test case, print the sum of sum of all subarrays, separated by newline.Constraints10 points1 <= T <= 1001 <= N <= 10220 points1 <= T <= 1001 <= N <= 10370 points1 <= T <= 10001 <= N <= 104General Constraints-106 <= arr[i] <= 106ExampleInput333 4 521 231 -3 4Output4063ExplanationTest Case 1:[3] + [3,4] + [3,4,5] + [4] + [4,5] + [5] = 40Test Case 2:[1] + [1,2] + [2] = 6Test Case 3:[1] + [1,-3] + [1,-3,4] + [-3] + [-3,4] + [4] = 3

Sum of 2 NumbersGiven an array, check if there exist 2 elements of the array such that their sum is equal to the sum of the remaining elements of the array.Input FormatThe first line of input contains T - the number of test cases. It is followed by 2T lines, the first line contains N - the size of the array. The second line contains N integers - elements of the array.Output FormatFor each test case, print "Yes" if such elements exist, and "No" otherwise, separated by a new line.Constraints30 points1 <= T <= 1001 <= N <= 1000-106 <= A[i] <= 10670 points1 <= T <= 5001 <= N <= 10000-106 <= A[i] <= 106ExampleInput25-3 5 8 2 -465 -10 8 4 2 -3OutputYesNoExplanationExample 1:Possible values: 8 + (-4) = (-3) + 5 + 2.Example 2:No 2 elements exist whose sum is equal to the sum of the remaining array elements.

You are given an integer array and an integer K. You have to tell if there exists a pair of integers in the given array such that ar[i]-ar[j]=K and i≠j.Input FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines, the first line contains N and K - the size of the array and the number K. The second line contains the elements of the array.Output FormatFor each test case, print "true" if the arrays contains such elements, "false" otherwise, separated by new line.Constraints40 points2 <= N <= 100060 points2 <= N <= 100000General Constraints1 <= T <= 100-105 <= ar[i], K <= 105ExampleInput25 601 20 40 100 8010 1112 45 52 65 21 645 234 14 575 112Outputtruefalse

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.