Admin message
The 17.0 major release is coming on May 16, 2024! This version brings many exciting improvements to GitLab, but also removes some deprecated features. We are introducing three breaking change windows during which we expect breaking changes to be deployed to GitLab.com. You can read more about it on our blogpost . The second breaking change window begins 2024-04-29 09:00 UTC and ends 2024-05-01 22:00 UTC .
- Coursera-Algorithmic-Toolbox
- assignment solutions
- 1.2 max pairwise product.py
Maximum Pairwise Product
Cloistered Monkey
2018-06-24 16:42
Since the test only uses python standard library I'll try and stick to that, but since the stress-test isn't part of the assignment I'll cheat and use numpy to generate the random input.
Problem Statement
Find the maximum product of two distinct numbers in a sequence of non-negative integers.
- Input: A sequence of non-negative integers.
- Output: The maximum value that can be obtained by multiplying two different elements from the sequence.
Given a sequence of non-negative numbers \(a_1,\ldots,a_n\), compute
\[ \max_{1 \le i \neq j \le n} a_i a_j \]
\(i\) and \(j\) should be different, although \(a_i\) and \(a_j\) might be equal.
Example Values
Some constants.
This is just a translation of some of the problem statement values to python so we can use them.
These are some functions to help validate the algorithms.
Brute Force Implementation
This is given as part of the problem. It traverses all the values and finds the largest product.
Because we are traversing all the numbers twice, the brute-force version has a run time of \(O(n^2)\). Since the \(n\) can be from \(2\) to \(2 \times 10^5\) that means our maximum run time will be \(4 \times 10^10\), which is too large.
Running this through the grader gives this output.
Instead of using nested for-loops, we can search the numbers twice to find the two biggest numbers, this changes the run time to \(2n\) or \(O(n)\).
This one passes the grader, doing surprisingly well, even though I was thinking it would need more optimizing.
Another Improvement
Rather than go through the second loop, I thought that since the previous maximum value is always the next highest value so far, we can just save it directly.
Stress Test
Even thought we're already passing, part of the assignment was to create a stress test to really exercise the algorithm once you have it passing.
Boosted Brute Force
To try and get this working I'm going to use numba to (hopefully) speed it enough to make the stress test runnable.
Since we need the top two values we can get a more efficient algorithm by sorting the values.
Maximum pairwise product →
Sep 2, 2017 • algorithms , C++
Let’s take a quick look at a simple problem:
Given a list of numbers, how would you determine the maximum pairwise product of numbers in the list?
For example, if we have the list [1; 3; 5; -2] the maximum pairwise product is 15 , which is the product of 3 and 5 .
The straigt-forward approach is to compute all possible pairwise products and identify the maximum among the intermediate results:
Since we have two loops that iterate over the list of numbers, the time complexity is O(n 2 ).
Can we do better? Yes, we can. The idea is to find the maximum and the second maximum elements in the list - and their product would be the maximum pairwise product.
Simple and sweet!
Search This Blog
Makingcodesimple.
Simple solutions with explanation for the problems on competitive sites like hackerearth and many many more things like how to make your code faster, shorter and simple only at makingcodesimple.blogspot.com
Coursera's Algorithmic toolbox assignment solutions( Sum of Two Digits, Maximum Pairwise Product ) :
Problem 1: sum of two digits, solution: (in c++), ( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder).
This code is simple. There's no need for any explanation.
Problem 2: Maximum Pairwise Product
Guys , if you have any queries related to a question or need more explanation, comment down below!
Post a Comment
Popular posts from this blog, hackerearth's basic programming solutions( seating arrangement, zoos, anagrams ) :, hackerearth's basic programming solutions( minimize cost, magical word, best index ) :.
Challenge: Maximum Pairwise Product
Maximum Pairwise Product Problem.
- Coding exercise
Maximum Pairwise Product Problem
Find the maximum product of two distinct numbers in a sequence of non-negative integers.
Input: A sequence of n n n non-negative integers.
Output: The maximum value that can be obtained by multiplying two different elements from the sequence.
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.
IMAGES
VIDEO
COMMENTS
Programming Assignment 1: Programming Challenges (OPTIONAL) Solving The Maximum Pairwise Product Programming Challenge in C++; Maximum Pairwise Product Programming Challenge; Using PyCharm to solve programming challenges (optional experimental feature) Acknowledgements (Optional)
assignment solutions; 1.2 max pairwise product.py; Find file Blame History Permalink added folder assignment solutions. Moved all files to that folder · 1f7cd5cb Anoubhav Agarwaal authored Sep 12, 2018.
Find the maximum product of two distinct numbers in a sequence of non-negative integers. Input: A sequence of non-negative integers. Output: The maximum value that can be obtained by multiplying two different elements from the sequence. Given a sequence of non-negative numbers \(a_1,\ldots,a_n\), compute \[ \max_{1 \le i \neq j \le n} a_i a_j \]
There are 6 modules in this course. This online course covers basic algorithmic techniques and ideas for computational problems arising frequently in practical applications: sorting and searching, divide and conquer, greedy algorithms, dynamic programming. We will learn a lot of theory: how to sort data and how it helps for searching; how to ...
Fast algorithm. In search of a faster algorithm, we play with small examples like [5, 6, 2, 7, 4] [5,6,2,7,4] [5, 6, 2, 7, 4].Eureka—it suffices to multiply the two largest elements of the array: 7 7 7 and 6 6 6!Since we need to find the largest and the second-largest elements, we need only two scans of the sequence.
Given a sequence of non-negative integers a0,…,an−1, find the maximum pairwise product, that is, the largest integer that can be obtained by multiplying two different elements from the sequence (or, more formally, max0≤i≠j≤n−1aiaj). Different elements here mean ai and aj with i≠j (it can be the case that ai=aj). Input format.
Things I learnt from assignment 1 — max pairwise product problem: The dreaded O(N) Copying a list in Python is as simple as list.copy() ... · 14 min read · Apr 1, 2024--227. Hazel Paradise.
Evaluate solutions to the Maximum Pairwise Product Problem. Evaluate solutions to the Maximum Pairwise Product Problem. Log In Join for free. Log In Join for free. ... If their results differ, the test and both answers are output, and the program stops. Otherwise, the loop repeats. Level up your interview prep. Join Educative to access 70 ...
Let's take a quick look at a simple problem: Given a list of numbers, how would you determine the maximum pairwise product of numbers in the list? For example, if we have the list [1; 3; 5; -2] the maximum pairwise product is 15, which is the product of 3 and 5. The straigt-forward approach is to compute all possible pairwise products and identify the maximum among the intermediate results ...
Simple solutions for coursera's Algorithmic toolbox assignment questions: Sum of Two Digits, Maximum Pairwise Product by Making code simple! ... Maximum Pairwise Product Solution: (in c++) ... HackerEarth's basic programming solutions( Minimize Cost, Magical Word, Best Index ) : -
This repository will contain my work from the Master Algorithmic Programming Techniques Specialization that was created by UC San Diego and delivered through Coursera. - mablatnik/Algorithmic-Toolbox
Problem. Maximum Pairwise Product Problem. Find the maximum product of two distinct numbers in a sequence of non-negative integers. Input: A sequence of n n non-negative integers. Output: The maximum value that can be obtained by multiplying two different elements from the sequence. Maximum Pairwise Product Problem.
There are 6 modules in this course. This online course covers basic algorithmic techniques and ideas for computational problems arising frequently in practical applications: sorting and searching, divide and conquer, greedy algorithms, dynamic programming. We will learn a lot of theory: how to sort data and how it helps for searching; how to ...
It's the maximum pairwise products problem. input a sequence of non-negative intergers, and output the maximum product of any 2 numbers in the sequence. - Kevin mai Feb 22, 2022 at 6:11
This repository contains all the solutions for the assignments of the course - Algorithmic Toolbox offered on Coursera. - tjanhvi/Coursera-Algorithmic-Toolbox-1
This python program takes user input of the number of elements in a list in the variable b and then takes user input of the list elements in list a. It then finds out the maximum product of a pair of the list elements. For example, if the list elements are 5,4 and 6, then the maximum product is 30. Can we simplify it in a way such that it takes ...
0. This is my code for the maximum pairwise product. It takes an array of numbers, sorts them to find the first and second maximum numbers, then return the product of them. The code works for small arrays and small values. But it fails with some numbers, and also it epically fails with large numbers such as 10000 and so.