mcg1coding
BAN USER
- 1of 1 vote
AnswersGiven an array of unique numbers. Find the number of pairs that make up the difference. This must be solved in under O(N^2)
function getPairs(int[] array, int k){ HashMap<Integer,Integer> values = new HashMap<Integer,Integer>(); for(int i = 0; i < array.length; i++){ if(!values.containsKey(array[i])){ values.put(array[i],1); } } int pairs = 0; for(int i = 0; i < array.length; i++){ int diff = array[i] - k; if(values.containsKey(diff)){ pairs++; } } return pairs; }
This will give O(N) time. O(N) Space
- mcg1coding in United States| Report Duplicate | Flag | PURGE
Fidessa Software Developer Arrays - 3of 3 votes
AnswersGiven estimated stock quotes, in an array, print the maximum profit from a buy and sell. i.e [19, 22, 15, 35, 40, 10, 20] would show a profit of 25(40 -15). The sale must come after the buy. Solve this in O(N) time.
- mcg1coding in United States<?php function print_max_profit($arr){ if(count($arr) < 1) return 0; $len = sizeof($arr); $profit = 0; $least = $arr[0]; for($i = 0; $i < $len; $i++){ $least = min($least,$arr[$i]); $profit = max($profit, $arr[$i] - $least); } return $profit; }
| Report Duplicate | Flag | PURGE
Facebook Solutions Architect
RepBeccaElyn, Accountant at ADP
Basic literacy is generally a requirement for order-filling positions, because order fillers need to be able to understand written lists ...
Repannasteven1246, Analyst at Accenture
Creative, highly visual fashion professional who can brilliantly mix and match the technical expertise and intuition like fabric and color ...
Repmelonydmaxwell, maintenence engineer at AMD
Hi, I am working as a health information technician and my work is to collect and maintain a patient's ...
Repannawellson007, Area Sales Manager at 8x8
Hey my name is anna And i am working as a content writer in Search engine optimization company.My component ...
Repdennahood, Top mobile application Development Company in India at Arista Networks
Je suis Denna, une rédactrice professionnelle avec une expérience complète et de solides atouts en rédaction et en rédaction Web ...
Repbrakpeter2, AT&T Customer service email at Absolute Softech Ltd
My Profession Is photography for a number of years.My Friends were interested in photography, so I was always exposed ...
Repteresawells858, Java Developer at 247quickbookshelp
I am Teresa. I work as a Software engineer at Asian Fusion Company. I was born in the US and ...