[UCSD白板题] Maximum Pairwise Product
Problem Description
Task.Given a sequence of non-negative integers \(a_0, ..., a_{n-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,\(\max \limits_{0\leq{i \neq j}\leq {n-1}}\ a_ia_j\)).Different elements here mean \(a_i\) and \(a_j\) with \(i \neq j\) (it can be the case that \(a_i=a_j\)).
Input format.The first line of the input contains an integer \(n\).The next line contains\(n\)non-negative integers \(a_0, ..., a_{n-1}\) (separated by spaces).
Constraints.\(2 \leq n \leq 2 \cdot 10^5; 0 \leq a_0, ..., a_{n-1} \leq 10^5\).
Output format.Output a single number - the maximum pairwise product.
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
Sample 3.
Input:
5
4 6 2 6 1
Output:
36
Solution
# Uses python3
n = int(input())
a = [int(x) for x in input().split()]
assert(len(a) == n)
fstMax = sndMax = 0
for idx in range(0, n):
if fstMax < a[idx]:
fstMax, sndMax = a[idx], fstMax
elif sndMax < a[idx]:
sndMax=a[idx]
print(fstMax*sndMax)
[UCSD白板题] Maximum Pairwise Product的更多相关文章
- [UCSD白板题] Minimum Dot Product
Problem Introduction The dot product of two sequences \(a_1,a_2,\cdots,a_n\) and \(b_1,b_2,\cdots,b_ ...
- [UCSD白板题] Pairwise Distinct Summands
Problem Introduction This is an example of a problem where a subproblem of the corresponding greedy ...
- [UCSD白板题] Maximize the Value of an Arithmetic Expression
Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...
- [UCSD白板题] Take as Much Gold as Possible
Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...
- [UCSD白板题] Binary Search
Problem Introduction In this problem, you will implemented the binary search algorithm that allows s ...
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- [UCSD白板题] Compute the Edit Distance Between Two Strings
Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...
- [UCSD白板题] Primitive Calculator
Problem Introduction You are given a primitive calculator that can perform the following three opera ...
- [UCSD白板题] Points and Segments
Problem Introduction The goal in this problem is given a set of segments on a line and a set of poin ...
随机推荐
- Linux Daemon 类程序
1.后台daemon程序(精灵程序) 在Linux中专门提供了一个函数来完成这个daemon化的过程,这个函数的原型如下 int daemon (int __nochdir, int __noclos ...
- TSkinData 皮肤控件后最大最小提示英文Close的解决方法
1.D:\soft\控件\VclSkin5.40-D7-D2010 New\source 控件安装位置 2.WinSkinForm.pas 查找Close 3.function TWinSkinFor ...
- Java中public,private,protected,和默认的区别
Java中public,private,protected,和默认的区别 1.private修饰词,表示成员是私有的,只有自身可以访问: 2.protected,表示受保护权限,体现在继承,即子类可以 ...
- 书单.md
0823 John Hoskin, An Ilustrated History of Thailand.Asia Books Co., Ltd.2015 0729 Gerald Graff, Cath ...
- Xilium.CefGlue CEF Chrome 自动上传文件不弹出对话框 CefDialogHandler
关键代码如下,如何使用Handler,还不清楚的请继续搜索 internal sealed class WyzCefDialogHandler : CefDialogHandler { ...
- notepad++的CoolFormat代码格式化插件使用
因为notepad++的NppAStyle插件只支持格式化C.C++.C#.Java这四种编程语言的代码,所以本人推荐使用这个CoolFormat的插件,相比于NPPAStyle,CoolFormat ...
- Ajax 知识点
AJAX 即"Asynchronous Javascript And XML"(异步JavaScript和XML) Ajax 不是某种编程语言,只是一种在无需重新加载整个网页的情况 ...
- python 整齐输出与编码读写
# -*- coding:utf-8 -*- # Author:mologa for x in range(1,11): print(repr(x).rjust(2),repr(x*x).rjust( ...
- 关于 Netty Channel 的 Autoread
Netty 4 的 Channel 多了一个 autoread 参数, 它的用处是在让 channel 在触发某些事件以后(例如 channelActive, channelReadComplete) ...
- boot from volume
nova boot --flavor 1 --block-device source=image,id=<image_id>,dest=volume,size=5,shutdown=pre ...