Asked inOla Cabs,SDE-2
Partition Equal Subset Sum

You are given an array 'ARR' of 'N' positive integers. Your task is to find if we can partition the given array into two subsets such that the sum of elements in both subsets is equal.

For example, let’s say the given array is [2, 3, 3, 3, 4, 5], then the array can be partitioned as [2, 3, 5], and [3, 3, 4] with equal sum 10.

Follow Up:

Can you solve this using not more than O(S) extra space, where S is the sum of all elements of the given array?
Input Format:
The first line of input contains an integer 'T' representing the number of test cases or queries to be processed.
Then the test case follows.

The first line of each test case contains an integer 'N', denoting the size of the array.

The second line of each test case contains 'N' single space-separated integers representing the array elements.
Output Format:
For each test case, print “true” or “false” denoting whether we can partition into two equal subset-sum or not, in a separate line. 
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraints:
1 <= 'T' <= 10
1 <= 'N' <= 100 
1 <= 'ARR'[i] <= 100

Time Limit: 1 sec
CodingNinjas
author
2y

Let isSubsetSum(arr, n, sum/2) be the function that returns true if
there is a subset of arr[0..n-1] with sum equal to sum/2

The isSubsetSum problem can be divided into two subproblems
a) isSubsetSum() ...read more

CodingNinjas
author
2y
Brute Force

Approach: The key point to notice here is that we have to partition an array into two equal subsets sum so two equal subsets must have the sum equal to 'TOTALSUM'/2, where 'TOTALSUM' repres...read more

CodingNinjas
author
2y
Using Memoization

Approach: The previous approach uses recursion which has many overlapping subproblems. i.e recursive function computes the same sub-problems again and again. So we can use memoization...read more

CodingNinjas
author
2y
Dynamic Programming

Approach:

  • The idea is to use a bottom-up dynamic programming approach instead of a memoization approach. Let us assume 'DP'['I']['J'] means whether the specific 'SUM' j can come from...read more
CodingNinjas
author
2y
Using 1-D array

Approach: We see in a previous approach that we only need current index 'I' , and previous index 'I' - 1, and the rest of the indexes are a waste of space, So, we can achieve the same u...read more

Add answer anonymously...
Ola Cabs SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter