Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
freem
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Openai/693843f2-4e44-8005-8882-a17446caa5c4
(section)
Add languages
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===== While iterating j from left to right: ===== * decrement current element from rightCount (because now j is not on the right anymore) '' compute: - countLeft = leftCount[2 '' nums[j]] - countRight = rightCount[2 * nums[j]] '' add countLeft '' countRight to answer * increment leftCount[nums[j]] === <syntaxhighlight lang="java">class Solution { === public int countTriplets(int[] nums) { int n = nums.length; long MOD = 1_000_000_007; Map<Integer, Integer> left = new HashMap<>(); Map<Integer, Integer> right = new HashMap<>(); // Fill right map with frequencies for (int x : nums) { right.put(x, right.getOrDefault(x, 0) + 1); } long ans = 0; for (int j = 0; j < n; j++) { int val = nums[j]; // Remove nums[j] from the right side since j is current center right.put(val, right.get(val) - 1); int target = val * 2; long countLeft = left.getOrDefault(target, 0); long countRight = right.getOrDefault(target, 0); ans = (ans + (countLeft * countRight) % MOD) % MOD; // Add nums[j] to left map left.put(val, left.getOrDefault(val, 0) + 1); } return (int) ans; } } </syntaxhighlight>
Summary:
Please note that all contributions to freem are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 (see
Freem:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)