- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- 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 an array called nums, whose size is 2*n. We have to perform n operations on this array. In the ith operation (1-indexed), we will do the following:

Select two elements, x and y.

Get a score of i*gcd(x, y).

Remove x and y from the array nums.

We have to find the maximum score we can get after performing n operations.

So, if the input is like nums = [6,2,1,5,4,3], then the output will be 14 because the optimal choices are (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14

To solve this, we will follow these steps −

n := size of nums

dp := an array of size (2^n) and fill with -1

Define a function dfs() . This will take mask, t

if mask is same as (2^n - 1), then

return 0

if dp[mask] is not same as -1, then

return dp[mask]

ma := 0

for i in range 0 to n, do

if 2^i AND mask is non-zero, then

go for next iteration

for j in range i + 1 to n - 1, do

if 2^j AND mask is non-zero, then

go for next iteration

next := dfs(mask OR 2^i OR 2^j, t+1) + gcd(nums[i], nums[j])*t

ma := maximum of next and ma

dp[mask] := ma

return dp[mask]

From the main method, return dfs(0, 1)

Let us see the following implementation to get better understanding

from math import gcd def solve(nums): n = len(nums) dp = [-1] * (1 << n) def dfs(mask, t): if mask == (1 << n) - 1: return 0 if dp[mask] != -1: return dp[mask] ma = 0 for i in range(n): if (1 << i) & mask: continue for j in range(i + 1, n): if (1 << j) & mask: continue next = dfs(mask | (1 << i) | (1 << j), t + 1) + gcd(nums[i], nums[j]) * t ma = max(next, ma) dp[mask] = ma return dp[mask] return dfs(0, 1) nums = [6,2,1,5,4,3] print(solve(nums))

[6,2,1,5,4,3]

14

- Related Questions & Answers
- Program to find maximum score from performing multiplication operations in Python
- Program to find lexicographically smallest string after applying operations in Python
- Program to find minimum possible maximum value after k operations in python
- Python program to find runner-up score
- Program to minimize hamming distance after swap operations in Python
- Find maximum operations to reduce N to 1 in Python
- Program to maximize the minimum value after increasing K sublists in Python
- Program to maximize the number of equivalent pairs after swapping in Python
- Program to find maximum score in stone game in Python
- Program to find maximum score from removing stones in Python
- Program to find number of items left after selling n items in python
- Program to find maximize palindrome length from subsequences in Python
- Python program to find word score from list of words
- Program to find out the position of a ball after n reversals in Python
- Program to find remainder after dividing n number of 1s by m in Python

Advertisements