[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 ...
随机推荐
- Maven Archetype
1. 从project创建archetype 在项目根目录下,运行 mvn archetype:create-from-project 创建的archetype工程在app_folder/target ...
- JSTL标准标签库 (使用foreach打印集合)
<%@page import="java.util.*"%><%@ page language= "java" contentType=&qu ...
- [整理]Matlab之中心平滑滤波
滑动平均(moving average):在地球物理异常图上,选定某一尺寸的窗口,将窗口内的所有异常值做算术平均,将平均值作为窗口中心点的异常值.按点距或线距移动窗口,重复此平均方法,直到对整幅图完成 ...
- python之socket 网络编程
提到网络通信不得不复习下osi七层模型: 七层模型,亦称OSI(Open System Interconnection)参考模型,是参考模型是国际标准化组织(ISO)制定的一个用于计算机或通信系统间互 ...
- 简洁的java代码
最近在codewars上刷题,学习到了不少简洁优雅的代码. codewars和leetcode都是刷题网站,codewars的题目难度分类比较广,适合各种不同水平的coder刷题. 刷完题后,看一下其 ...
- Multi-armed Bandit Problem与增强学习的联系
选自<Reinforcement Learning: An Introduction>, version 2, 2016, Chapter2 https://webdocs.cs.ualb ...
- 用GUI完成了斗地主发牌
JAVA真的很强大,简单的步骤他自己就可以帮助我们解决了,简单,方便,并且还有着非常大的开发潜力
- 在ubuntu 16.04系统环境中搭建NAS(samba/iscsi/nfs)
在ubuntu 16.04系统中搭建NAS环境 一.基本配置1:设置静态IPvi /etc/network/interfaces#iface ens32 inet dhcpiface ens32 in ...
- 初试Nodejs——使用keystonejs创建博客网站1(安装keystonejs)
我正在阿里云上创建一个简单的个人博客网站,刚好正在尝试NodeJs,决定找一款基于NodeJs的CMS来完成这个工作,最后找到了KeyStoneJS. KeyStoneJS是基于Express和Mon ...
- 记一次创建LVM的日志记录
先上一张鸟哥LVM的图.感觉这张最清楚了. #以下以Xshell的日志记录系统直接记录.上面添加了一些个人理解的注释 [BEGIN] 2016/9/13 9:22:24 #先查看下硬盘的情况. [ro ...