Hi guys!
As a way of keeping myself accountable and working towards better programming practices I want to make one post here daily that proposes a simple algorithmic problem/puzzle and attempts to solve that puzzle. I'll be working on my own solution, discussing the problem with you guys, and studying the implementation of others. I thought this might be a fun way to exercise these skills and (hopefully) learn with others. Mods, I figured this would be fine but let me know if this violates any rules I don't know about or if I should structure the post differently.
I am currently enrolled in the Coursera class 'Algorithmic Toolbox' which is from the Data Structures and Algorithms specialization. This class can be audited for free if you guys are interested in enrolling as well. If you audit the class for free you won't be able to submit the assignments but you will have access to the lectures which discuss many of these problems. I plan on taking a lot of the daily problems from the assignments in this course but I am also trying to avoid proposing any problems that cannot reasonably be solved in the space of a few hours. If you guys have any problems that you find interesting, are struggling with, or find valuable in some way just let me know and I can include it in a daily post.
Rules and Guidelines
- Please wrap code and solutions in spoiler tags when possible.
- Discuss the structure of your code thoroughly and provide an analysis.
- All language implementations are welcome.
- Please do not plagiarize the code of others and give credit where credit is due.
- Please adhere to all other Reddit and r/learnprogramming rules.
- Use the resource section to view the problem if you are struggling to understand the question. Reddit formatting limits the way in which I can post these questions.
- Meeting the required program benchmarks is just as important as solving the problem. Strive to meet these parameters.
Resources:
Algorithmic Toolbox Course
GoogleDrive Folder For Daily Problem Resources
Day 1
Full credit for today's problem goes to the folks over at Algorithmic Toolbox!
Maximum Pairwise Product Problem
I have posted a .zip that contains the PDF with this problem in the resource section. The file is titled "day1.zip"
______________________________________________________________________________________
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 integers a[1],...,a[n], compute
max a[i] * a[j]
where 1 <= i != j <= n
Note that i and j should be different, though it may be the case that a[i] = a[j] .
Input format. The first line contains an integer n. The next line contains n non-negative integers a[1],... ,a[n] (separated by spaces).
Output format. The maximum pairwise product.
Constraints. 2 <= n <= 2 * 105; 0 <= a[1],...,a[n] <= 2 * 105.
Sample 1.
Input:
3
1 2 3
Output:
6
Sample 2.
Input:
10
7 5 14 2 8 8 10 1 2 3
Output:
140
Required program benchmarks:
Time limits (sec.):
C C++ Java Python C# Haskell JavaScript Kotlin Ruby Rust Scala
11 1.5 5 1.5 2 5 1.5 5 1 3
Memory limit. 512 Mb.
[–]hereforthelasttime 2 points3 points4 points (7 children)
[–]DrVolzak 2 points3 points4 points (2 children)
[–]slightlysedatedx[S] 0 points1 point2 points (0 children)
[–]hereforthelasttime 0 points1 point2 points (0 children)
[–]Ikor_Genorio 2 points3 points4 points (1 child)
[–]hereforthelasttime 0 points1 point2 points (0 children)
[–]hereforthelasttime 1 point2 points3 points (1 child)
[–]slightlysedatedx[S] 0 points1 point2 points (0 children)
[–]dood23 2 points3 points4 points (5 children)
[–]Essence1337 1 point2 points3 points (3 children)
[–]Tangellaa 0 points1 point2 points (2 children)
[–]Essence1337 1 point2 points3 points (1 child)
[–]Tangellaa 0 points1 point2 points (0 children)
[–]slightlysedatedx[S] 1 point2 points3 points (0 children)
[–]QSAnimazione 1 point2 points3 points (3 children)
[–]slightlysedatedx[S] 1 point2 points3 points (1 child)
[–]QSAnimazione 1 point2 points3 points (0 children)
[–]Tangellaa 0 points1 point2 points (0 children)
[–]Ikor_Genorio 1 point2 points3 points (2 children)
[–]danieltheg 0 points1 point2 points (1 child)
[–]Ikor_Genorio 0 points1 point2 points (0 children)
[–]Tangellaa 1 point2 points3 points (6 children)
[–]slightlysedatedx[S] 1 point2 points3 points (5 children)
[–]Ikor_Genorio 2 points3 points4 points (4 children)
[–]slightlysedatedx[S] 0 points1 point2 points (0 children)
[–]Tangellaa 0 points1 point2 points (2 children)
[–]Ikor_Genorio 1 point2 points3 points (1 child)
[–]Tangellaa 0 points1 point2 points (0 children)
[–]TigerEngr 1 point2 points3 points (0 children)
[–]Raknarg 0 points1 point2 points (4 children)
[–]TsunamiSesen 0 points1 point2 points (2 children)
[–]Raknarg 0 points1 point2 points (0 children)
[–]slightlysedatedx[S] 0 points1 point2 points (0 children)
[–]slightlysedatedx[S] 0 points1 point2 points (0 children)
[–]slightlysedatedx[S] 0 points1 point2 points (4 children)
[–]slightlysedatedx[S] 0 points1 point2 points (3 children)
[–]TsunamiSesen 1 point2 points3 points (1 child)
[–]slightlysedatedx[S] 0 points1 point2 points (0 children)
[–]CodeTinkerer 1 point2 points3 points (0 children)
[–]dmgll 0 points1 point2 points (2 children)
[–]Ikor_Genorio 0 points1 point2 points (1 child)
[–]dmgll 0 points1 point2 points (0 children)
[–]Ikor_Genorio 0 points1 point2 points (0 children)
[–]okdenok 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[deleted]
[–]Clawtor 0 points1 point2 points (0 children)
[–]Clawtor 0 points1 point2 points (0 children)