- 87.5k
- 14
- 104
- 323
How to find count of Count matching pairs efficiently than O(n^2modular equality)
Problem
Problem: Given an array of natural numbers a
. Find the number of such pairs of elements (a_i, a_j)
, where abs(a_i − a_j) mod 200 == 0
and i < j
Solution
I came up with this solution:
// n - the length of numbers array
// numbers - the array of natural numbers
function getNumberOfGoodPairs(n, numbers) {
let count = 0;
const remainders = new Map();
for (const num of numbers) {
const remainder = num % 100;
const modifiedNum = (num - remainder) / 100;
if (remainders.has(remainder)) {
const nums = remainders.get(remainder);
for (const num of nums) {
if (Math.abs(num - modifiedNum) % 2 === 0) ++count;
}
nums.push(modifiedNum);
} else {
remainders.set(remainder, [modifiedNum]);
}
}
return count;
}
Notations:
Notations
for (const arrayElement of array)
- get elements from thearray
one by one and put this element intoarrayElement
. It is same as:for (let i = 0; i < array.length; ++i) { const arrayElement = array[i]; }
new Map()
- is dictionary%
- ismod
array.push(el)
- addel
to the end ofarray
remainders.get(remainder)
- returns the array of numbers which remainder is equal toremainder
when dividing by 100remainders.set(remainder, [modifiedNum])
- add to dictionary new keyremainder
and value[modifiedNum]
.[modifiedNum]
- a dynamic array with one elementmodifiedNum
If I'm right the time complexity in worst case is O(n^2)
O(n2).
Please help me to optimize this algorithm.
How to find count of pairs efficiently than O(n^2)
Problem: Given an array of natural numbers a
. Find the number of such pairs of elements (a_i, a_j)
, where abs(a_i − a_j) mod 200 == 0
and i < j
I came up with this solution:
// n - the length of numbers array
// numbers - the array of natural numbers
function getNumberOfGoodPairs(n, numbers) {
let count = 0;
const remainders = new Map();
for (const num of numbers) {
const remainder = num % 100;
const modifiedNum = (num - remainder) / 100;
if (remainders.has(remainder)) {
const nums = remainders.get(remainder);
for (const num of nums) {
if (Math.abs(num - modifiedNum) % 2 === 0) ++count;
}
nums.push(modifiedNum);
} else {
remainders.set(remainder, [modifiedNum]);
}
}
return count;
}
Notations:
for (const arrayElement of array)
- get elements from thearray
one by one and put this element intoarrayElement
. It is same as:for (let i = 0; i < array.length; ++i) { const arrayElement = array[i]; }
new Map()
- is dictionary%
- ismod
array.push(el)
- addel
to the end ofarray
remainders.get(remainder)
- returns the array of numbers which remainder is equal toremainder
when dividing by 100remainders.set(remainder, [modifiedNum])
- add to dictionary new keyremainder
and value[modifiedNum]
.[modifiedNum]
- a dynamic array with one elementmodifiedNum
If I'm right the time complexity in worst case is O(n^2)
Please help me to optimize this algorithm.
Count matching pairs (modular equality)
Problem
Given an array of natural numbers a
. Find the number of such pairs of elements (a_i, a_j)
, where abs(a_i − a_j) mod 200 == 0
and i < j
Solution
I came up with this solution:
// n - the length of numbers array
// numbers - the array of natural numbers
function getNumberOfGoodPairs(n, numbers) {
let count = 0;
const remainders = new Map();
for (const num of numbers) {
const remainder = num % 100;
const modifiedNum = (num - remainder) / 100;
if (remainders.has(remainder)) {
const nums = remainders.get(remainder);
for (const num of nums) {
if (Math.abs(num - modifiedNum) % 2 === 0) ++count;
}
nums.push(modifiedNum);
} else {
remainders.set(remainder, [modifiedNum]);
}
}
return count;
}
Notations
for (const arrayElement of array)
- get elements from thearray
one by one and put this element intoarrayElement
. It is same as:for (let i = 0; i < array.length; ++i) { const arrayElement = array[i]; }
new Map()
- is dictionary%
- ismod
array.push(el)
- addel
to the end ofarray
remainders.get(remainder)
- returns the array of numbers which remainder is equal toremainder
when dividing by 100remainders.set(remainder, [modifiedNum])
- add to dictionary new keyremainder
and value[modifiedNum]
.[modifiedNum]
- a dynamic array with one elementmodifiedNum
If I'm right the time complexity in worst case is O(n2).
Please help me to optimize this algorithm.
Problem: Given an array of natural numbers a
. Find the number of such pairs of elements (a_i, a_j)
, where abs(a_i − a_j) mod 200 == 0
and i < j
I came up with this solution:
// n - the length of numbers array
// numbers - the array of natural numbers
function getNumberOfGoodPairs(n, numbers) {
let count = 0;
const remainders = new Map();
for (const num of numbers) {
const remainder = num % 100;
const modifiedNum = (num - remainder) / 100;
if (remainders.has(remainder)) {
const nums = remainders.get(remainder);
for (const num of nums) {
if (Math.abs(num - modifiedNum) % 2 === 0) ++count;
}
nums.push(modifiedNum);
} else {
remainders.set(remainder, [modifiedNum]);
}
}
return count;
}
Notations:
for (const arrayElement of array)
- get elements from thearray
one by one and put this element intoarrayElement
. It is same as:for (let i = 0; i < array.length; ++i) { const arrayElement = array[i]; }
new Map()
- is dictionary%
- ismod
array.push(el)
- addel
to the end ofarray
remainders.get(remainder)
- returns the array of numbers which remainder is equal toremainder
when dividing by 100remainders.set(remainder, [modifiedNum])
- add to dictionary new keyremainder
and value[modifiedNum]
.[modifiedNum]
- a dynamic array with one elementmodifiedNum
If I'm right the time complexity in worst case is O(n^2)
Please help me to optimize this algorithm. You can provide the pseudo code to explain the your algorithm
Problem: Given an array of natural numbers a
. Find the number of such pairs of elements (a_i, a_j)
, where abs(a_i − a_j) mod 200 == 0
and i < j
I came up with this solution:
// n - the length of numbers array
// numbers - the array of natural numbers
function getNumberOfGoodPairs(n, numbers) {
let count = 0;
const remainders = new Map();
for (const num of numbers) {
const remainder = num % 100;
const modifiedNum = (num - remainder) / 100;
if (remainders.has(remainder)) {
const nums = remainders.get(remainder);
for (const num of nums) {
if (Math.abs(num - modifiedNum) % 2 === 0) ++count;
}
nums.push(modifiedNum);
} else {
remainders.set(remainder, [modifiedNum]);
}
}
return count;
}
Notations:
for (const arrayElement of array)
- get elements from thearray
one by one and put this element intoarrayElement
. It is same as:for (let i = 0; i < array.length; ++i) { const arrayElement = array[i]; }
new Map()
- is dictionary%
- ismod
array.push(el)
- addel
to the end ofarray
remainders.get(remainder)
- returns the array of numbers which remainder is equal toremainder
when dividing by 100remainders.set(remainder, [modifiedNum])
- add to dictionary new keyremainder
and value[modifiedNum]
.[modifiedNum]
- a dynamic array with one elementmodifiedNum
If I'm right the time complexity in worst case is O(n^2)
Please help me to optimize this algorithm. You can provide the pseudo code to explain the your algorithm
Problem: Given an array of natural numbers a
. Find the number of such pairs of elements (a_i, a_j)
, where abs(a_i − a_j) mod 200 == 0
and i < j
I came up with this solution:
// n - the length of numbers array
// numbers - the array of natural numbers
function getNumberOfGoodPairs(n, numbers) {
let count = 0;
const remainders = new Map();
for (const num of numbers) {
const remainder = num % 100;
const modifiedNum = (num - remainder) / 100;
if (remainders.has(remainder)) {
const nums = remainders.get(remainder);
for (const num of nums) {
if (Math.abs(num - modifiedNum) % 2 === 0) ++count;
}
nums.push(modifiedNum);
} else {
remainders.set(remainder, [modifiedNum]);
}
}
return count;
}
Notations:
for (const arrayElement of array)
- get elements from thearray
one by one and put this element intoarrayElement
. It is same as:for (let i = 0; i < array.length; ++i) { const arrayElement = array[i]; }
new Map()
- is dictionary%
- ismod
array.push(el)
- addel
to the end ofarray
remainders.get(remainder)
- returns the array of numbers which remainder is equal toremainder
when dividing by 100remainders.set(remainder, [modifiedNum])
- add to dictionary new keyremainder
and value[modifiedNum]
.[modifiedNum]
- a dynamic array with one elementmodifiedNum
If I'm right the time complexity in worst case is O(n^2)
Please help me to optimize this algorithm.
ProblemProblem: Given an array of natural numbers a
. Find the number of such pairs of elements (a_i, a_j)
, where abs(a_i − a_j) mod 200 == 0
and i < j
I came up with this solution:
// n - the length of numbers array
// numbers - the array of natural numbers
function getNumberOfGoodPairs(n, numbers) {
let count = 0;
const remainders = new Map();
for (const num of numbers) {
const remainder = num % 100;
const modifiedNum = (num - remainder) / 100;
if (remainders.has(remainder)) {
const nums = remainders.get(remainder);
for (const num of nums) {
if (Math.abs(num - modifiedNum) % 2 === 0) ++count;
}
nums.push(modifiedNum);
} else {
remainders.set(remainder, [modifiedNum]);
}
}
return count;
}
Notations:
for (const arrayElement of array)
- get elements from thearray
one by one and put this element intoarrayElement
. It is same as:for (let i = 0; i < array.length; ++i) { const arrayElement = array[i]; }
new Map()
- is dictionary%
- ismod
array.push(el)
- addel
to the end ofarray
remainders.get(remainder)
- returns the array of numbers which reminderremainder is equal toremainder
when dividing by 100remainders.set(remainder, [modifiedNum])
- add to dictionary new keyreminderremainder
and value[modifiedNum]
.[modifiedNum]
- a dynamic array with one elementmodifiedNum
If I'm right the time complexity in worst case is O(n^2)
Please help me to optimize this algorithm. You can provide the pseudo code to explain the your algorithm
Problem: Given an array of natural numbers a
. Find the number of such pairs of elements (a_i, a_j)
, where abs(a_i − a_j) mod 200 == 0
and i < j
I came up with this solution:
// n - the length of numbers array
// numbers - the array of natural numbers
function getNumberOfGoodPairs(n, numbers) {
let count = 0;
const remainders = new Map();
for (const num of numbers) {
const remainder = num % 100;
const modifiedNum = (num - remainder) / 100;
if (remainders.has(remainder)) {
const nums = remainders.get(remainder);
for (const num of nums) {
if (Math.abs(num - modifiedNum) % 2 === 0) ++count;
}
nums.push(modifiedNum);
} else {
remainders.set(remainder, [modifiedNum]);
}
}
return count;
}
Notations:
for (const arrayElement of array)
- get elements from thearray
one by one and put this element intoarrayElement
. It is same as:for (let i = 0; i < array.length; ++i) { const arrayElement = array[i]; }
new Map()
- is dictionary%
- ismod
array.push(el)
- addel
to the end ofarray
remainders.get(remainder)
- returns the array of numbers which reminder is equal toremainder
when dividing by 100remainders.set(remainder, [modifiedNum])
- add to dictionary new keyreminder
and value[modifiedNum]
.[modifiedNum]
- a dynamic array with one elementmodifiedNum
If I'm right the time complexity in worst case is O(n^2)
Please help me to optimize this algorithm. You can provide the pseudo code to explain the your algorithm
Problem: Given an array of natural numbers a
. Find the number of such pairs of elements (a_i, a_j)
, where abs(a_i − a_j) mod 200 == 0
and i < j
I came up with this solution:
// n - the length of numbers array
// numbers - the array of natural numbers
function getNumberOfGoodPairs(n, numbers) {
let count = 0;
const remainders = new Map();
for (const num of numbers) {
const remainder = num % 100;
const modifiedNum = (num - remainder) / 100;
if (remainders.has(remainder)) {
const nums = remainders.get(remainder);
for (const num of nums) {
if (Math.abs(num - modifiedNum) % 2 === 0) ++count;
}
nums.push(modifiedNum);
} else {
remainders.set(remainder, [modifiedNum]);
}
}
return count;
}
Notations:
for (const arrayElement of array)
- get elements from thearray
one by one and put this element intoarrayElement
. It is same as:for (let i = 0; i < array.length; ++i) { const arrayElement = array[i]; }
new Map()
- is dictionary%
- ismod
array.push(el)
- addel
to the end ofarray
remainders.get(remainder)
- returns the array of numbers which remainder is equal toremainder
when dividing by 100remainders.set(remainder, [modifiedNum])
- add to dictionary new keyremainder
and value[modifiedNum]
.[modifiedNum]
- a dynamic array with one elementmodifiedNum
If I'm right the time complexity in worst case is O(n^2)
Please help me to optimize this algorithm. You can provide the pseudo code to explain the your algorithm