In a given array, find the number of sub-arrays that have the following properties:
Submit number of such sub-arrays as answer.
The array is described in file AinA.txt . First number ( ) is the number of elements in the given array. The rest of it are numbers that go in the array.
Example with array of 8 elements.
Array: 3 1 7 5 5 7 3 7
Input Format and Details:
Questions can be asked here .
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
If a number occurs k > 1 times in the array, then we have ( 2 k ) ways to pick that number as the first and last number of the sub-array. Hence, the answer is
x ∈ s e t ( A ) ∑ ( 2 k x )
In other words, for each unique number, we count its frequency and sum the number of ways to be selected as the first and last number of the sub-array.
Now, how do we implement this? A sneak peak into the test data gives us a sense that the numbers are huge (roughly 1 0 9 ). Hence we will use a
map
data structure to store the frequency of the numbers.This code returns 6 4 8 3 4 9 .