- Related Questions & Answers
- Check if an array can be divided into pairs whose sum is divisible by k in Python
- Check if elements of array can be made equal by multiplying given prime numbers in Python
- Check if a sorted array can be divided in pairs whose sum is k in Python
- Is pizza healthy? If not, can it be made healthy?
- Check if a two-character string can be made using given words in Python
- Check if a number can be written as sum of three consecutive integers in C++
- Check if it possible to partition in k subarrays with equal sum in Python
- Check if array can be sorted with one swap in Python
- Checking if a string can be made palindrome in JavaScript
- Find if neat arrangement of cups and shelves can be made in Python
- Program to check if array pairs are divisible by k or not using Python
- Maximum elements that can be made equal with k updates in C++
- Program to check we can find three unique elements ose sum is same as k or not Python
- Check whether the number can be made perfect square after adding 1 in Python
- Check if an array of 1s and 2s can be divided into 2 parts with equal sum in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a list of numbers called nums and also a positive value K. We can perform any of these three operations on nums −

- Make one number negative,
- Add index (start from index 1) of the number to the number itself
- Subtract index of the number from the number itself.

Finally, we have to check whether the given array can be transformed as the sum of the array becomes k, by performing these operations only once on each element.

So, if the input is like nums = [1,2,3,7] k = 8, then the output will be True as we can subtract index of 2 and 3 from 2 and 3 to make array like [1, 0, 0, 7], so now the sum is 8 = k.

To solve this, we will follow these steps −

- size := 100
- Define a function is_ok() . This will take i, total, k, nums, table
- n := size of nums
- if total <= 0, then
- return False

- if i >= n, then
- if total is same as k, then
- return True

- if total is same as k, then
- return False
- if table[i, total] is not -1, then
- return table[i, total]

- table[i, total] := 1 when (is_ok(i+1, total - 2 * nums[i], k, nums, table) is non-zero or is_ok(i+1, total, k, nums, table) is non-zero), otherwise 0
- table[i, total] := 1 when (is_ok(i+1, total -(i+1) , k, nums, table) or table[i, total]), otherwise 0
- table[i, total] := 1 when (is_ok(i+1, total + i + 1, k, nums, table) or table[i, total]), otherwise 0
- return table[i, total]
- From the main method do the following −
- total := sum of all elements in nums
- table := an array of length same as size and fill with -1
- for i in range 0 to size, do
- table[i] := an array of length same as size and fill with -1

- return is_ok(0, total, k, nums, table)

Let us see the following implementation to get better understanding −

size = 100 def is_ok(i, total, k, nums, table): n = len(nums) if total <= 0: return False if i >= n: if total == k: return True return False if table[i][total] != -1: return table[i][total] table[i][total] = is_ok(i+1, total - 2 * nums[i], k, nums, table) or is_ok(i+1, total, k, nums, table) table[i][total] = is_ok(i+1, total - (i+1), k, nums, table) or table[i][total] table[i][total] = is_ok(i+1, total + i + 1, k, nums, table) or table[i][total] return table[i][total] def solve(nums, k): total = sum(nums) table = [-1]*size for i in range(size): table[i] = [-1]*size return is_ok(0, total, k, nums, table) nums = [1,2,3,7] k = 8 print(solve(nums, k))

[1,2,3,7], 8

True

Advertisements