
- 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
Program to find maximum possible value of an expression using given set of numbers in Python
Suppose we have two arrays called nums1 and nums2, they have same number of elements N. Now consider a set S with N elements from 1 through N. We have to find the value of (nums1[i1] + nums1[i2] + ... nums1[ik])^2 + (nums2[i1] + nums2[i2] + ... nums2[ik])^2 where {i1, i2, ... ik} is non empty subset of the set S.
So, if the input is like nums1 = [-1, 6] nums2 = [5, 4], then the output will be 106 because
- (-1)^2 + (5)^2 = 26
- (6)^2 + (4)^2 = 50
- (-1 + 6)^2 + (5 + 4)^2 = 106
To solve this, we will follow these steps −
- vs := a list of pairs (nums1[i], nums2[i]) for each i in range 0 to size of nums1 - 1
- vs := sort vs by tan-inverse of v[1]/v[0] for each v in vs
- best := 0
- for i in range 0 to size of vs - 1, do
- u := vs[i]
- l := u[0]*u[0]+u[1]*u[1]
- for each v in the concatenated list of vs and vs again from index i+1 to (i+ size of vs - 1), do
- t1 := (u[0]+v[0], u[1]+v[1])
- t2 := t1[0]*t1[0]+t1[1]*t1[1]
- if t2 >= l, then
- u := t1
- l := t2
- if l > best, then
- best := l
- u := vs[i]
- l := u[0]*u[0]+u[1]*u[1]
- for each v in reverse the concatenated list of vs and vs again from index i+1 to i+ size of vs -1], do
- t1 := (u[0]+v[0], u[1]+v[1])
- t2 := t1[0]*t1[0]+t1[1]*t1[1]
- if t2 >= l, then
- u := t1
- l := t2
- if l > best, then
- return best
Example
Let us see the following implementation to get better understanding −
from math import atan2 def solve(nums1, nums2): vs = zip(nums1,nums2) vs = sorted(vs, key=lambda v: atan2(v[1],v[0])) best = 0 for i in range(len(vs)): u = vs[i] l = u[0]*u[0]+u[1]*u[1] for v in (vs+vs)[i+1:(i+len(vs))]: t1 = (u[0]+v[0],u[1]+v[1]) t2 = t1[0]*t1[0]+t1[1]*t1[1] if t2 >= l: u = t1 l = t2 if l > best: best = l u = vs[i] l = u[0]*u[0]+u[1]*u[1] for v in reversed((vs+vs)[i+1:(i+len(vs))]): t1 = (u[0]+v[0],u[1]+v[1]) t2 = t1[0]*t1[0]+t1[1]*t1[1] if t2 >= l: u = t1 l = t2 if l > best: best = l return best nums1 = [-1, 6] nums2 = [5, -4] print(solve(nums1, nums2))
Input
[-1, 6], [5, -4]
Output
52
- Related Questions & Answers
- Program to find maximum possible value of smallest group in Python
- C++ Program to Find Maximum Value of any Algebraic Expression
- C++ program to find maximum possible value of XORed sum
- Program to find expected value of maximum occurred frequency values of expression results in Python
- Python Program to Construct an Expression Tree of a given Expression
- Find all possible outcomes of a given expression in C++
- What is the maximum possible value of an integer in Python?
- Python program to find the maximum of three numbers
- Program to find expected value of given equation for random numbers in Python
- Program to find minimum possible maximum value after k operations in python
- Program to find maximum sum of multiplied numbers in Python
- Program to find maximum possible population of all the cities in python
- Program to find out the substrings of given strings at given positions in a set of all possible substrings in python
- Java program to find maximum of three numbers
- Find the maximum possible value of the minimum value of modified array in C++
Advertisements