Matrix A, consisting of N rows and M columns, is given, with each cell containing the value 0 or 1. Rows are numbered from 0 to N−1 (from top to bottom). Columns are numbered from 0 to M−1 (from left to right). The values inside the matrix can be changed: you can select as many columns as you want, and in the selected column(s), every value will be flipped (from 0 to 1, or from 1 to 0).
The goal is to obtain the maximum number of rows whose contents are all the same value (that is, we count rows with all 0s and rows with all 1s).
Write a function:
function solution(A);
that, given matrix A, returns the maximum number of rows containing all the same values that can be obtained after flipping the selected columns.
Examples:
1. Given matrix A with N = 3 rows and M = 4 columns:
the function should return 2. After flipping the values in column 1, the two last rows contain all equal values. Row 1 contains all 0s and row 2 contains all 1s.
2. Given matrix A with N = 4 rows and M = 4 columns:
the function should return 4. After flipping the values in two of the columns (columns 0 and 2), all the rows have the same value. Rows number 0 and 2 contain all 1s, and rows number 1 and 3 contain all 0s.
Write an efficient algorithm for the following assumptions:
- N and M are integers within the range [1..100,000];
- N * M is not greater than 100,000.
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ '0000', '0100', '1011' ]
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ '0101', '1010', '0101', '1010' ]
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ [ '00,010,00,0110', '00,010,00,0111' ], [ '01,000,01,0010', '01,000,01,0011' ], [ '10,111,10,1101', '10,111,10,1100' ] ]
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ [ '01,000,01,0011', '01,000,01,0010' ], [ '10,111,10,1100', '10,111,10,1101' ], [ '01,000,01,0011', '01,000,01,0010' ], [ '10,111,10,1100', '10,111,10,1101' ] ]
solution.js:6 const B = A.map((elem) => elem.reduce((p, c) => `${p}${c}`, "") ^ SyntaxError: missing ) after argument list at createScript (vm.js:80:10) at Object.runInNewContext (vm.js:135:10) at getSolution (/tmp/exec.js:404:29) at Promise.resolve.then (/tmp/exec.js:438:34) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
solution.js:6 const B = A.map((elem) => elem.reduce((p, c) => `${p}${c}`, "") ^ SyntaxError: missing ) after argument list at createScript (vm.js:80:10) at Object.runInNewContext (vm.js:135:10) at getSolution (/tmp/exec.js:404:29) at Promise.resolve.then (/tmp/exec.js:438:34) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
solution.js:11 } ^ SyntaxError: missing ) after argument list at createScript (vm.js:80:10) at Object.runInNewContext (vm.js:135:10) at getSolution (/tmp/exec.js:404:29) at Promise.resolve.then (/tmp/exec.js:438:34) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
solution.js:11 } ^ SyntaxError: missing ) after argument list at createScript (vm.js:80:10) at Object.runInNewContext (vm.js:135:10) at getSolution (/tmp/exec.js:404:29) at Promise.resolve.then (/tmp/exec.js:438:34) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ [ '0000', '1111' ], [ '0100', '1011' ], [ '1011', '0100' ] ]
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ [ '0101', '1010' ], [ '1010', '0101' ], [ '0101', '1010' ], [ '1010', '0101' ] ]
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ [ '0000', '1111' ], [ '0100', '1011' ], [ '0100', '1011' ] ]
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ [ '0101', '1010' ], [ '0101', '1010' ], [ '0101', '1010' ], [ '0101', '1010' ] ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const counts = {};
const B = A.map((elem) => {
return [
elem.reduce((p, c) => `${p}${c}`, ""),
elem.reduce((p, c) => `${p}${Number(!c)}`, "")
].sort();
}).forEach((e) => counts[e[0]])
console.log(B)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const counts = {};
const B = A.map((elem) => {
return [
elem.reduce((p, c) => `${p}${c}`, ""),
elem.reduce((p, c) => `${p}${Number(!c)}`, "")
].sort();
}).forEach((e) => counts[e[0]] = (counts[e[0]] || 0) + 1)
console.log(B)
console.log(counts)
}
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
undefined { '0000': 1, '0100': 2 }
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
undefined { '0101': 4 }
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const counts = {};
const B = A.map((elem) => {
return [
elem.reduce((p, c) => `${p}${c}`, ""),
elem.reduce((p, c) => `${p}${Number(!c)}`, "")
].sort();
})
.forEach((e) => counts[e[0]] = (counts[e[0]] || 0) + 1)
console.log(B)
console.log(counts)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const counts = {};
A.map((elem) => {
return [
elem.reduce((p, c) => `${p}${c}`, ""),
elem.reduce((p, c) => `${p}${Number(!c)}`, "")
].sort();
})
.forEach((e) => counts[e[0]] = (counts[e[0]] || 0) + 1)
console.log(Object.valuesOf(counts))
console.log(counts)
}
TypeError: Object.valuesOf is not a function at solution (solution.js:15:24) at solutionWrapper (/tmp/exec.js:414:28) at Promise.resolve.then (/tmp/exec.js:440:24) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
TypeError: Object.valuesOf is not a function at solution (solution.js:15:24) at solutionWrapper (/tmp/exec.js:414:28) at Promise.resolve.then (/tmp/exec.js:440:24) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const counts = {};
A.map((elem) => {
return [
elem.reduce((p, c) => `${p}${c}`, ""),
elem.reduce((p, c) => `${p}${Number(!c)}`, "")
].sort();
})
.forEach((e) => counts[e[0]] = (counts[e[0]] || 0) + 1)
console.log(Object.values(counts))
console.log(counts)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const counts = {};
A.map((elem) => {
return [
elem.reduce((p, c) => `${p}${c}`, ""),
elem.reduce((p, c) => `${p}${Number(!c)}`, "")
].sort();
})
.forEach((e) => counts[e[0]] = (counts[e[0]] || 0) + 1)
console.log(Object.values(counts))
console.log(counts)
}
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 1, 2 ] { '0000': 1, '0100': 2 }
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 4 ] { '0101': 4 }
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const counts = {};
A.map((elem) => {
return [
elem.reduce((p, c) => `${p}${c}`, ""),
elem.reduce((p, c) => `${p}${Number(!c)}`, "")
].sort();
})
.forEach((e) => counts[e[0]] = (counts[e[0]] || 0) + 1)
console.log(Object.values(counts).sort())
console.log(counts)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const counts = {};
A.map((elem) => {
return [
elem.reduce((p, c) => `${p}${c}`, ""),
elem.reduce((p, c) => `${p}${Number(!c)}`, "")
].sort();
})
.forEach((e) => counts[e[0]] = (counts[e[0]] || 0) + 1)
console.log(Object.values(counts).sort())
console.log(counts)
}
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 1, 2 ] { '0000': 1, '0100': 2 }
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 4 ] { '0101': 4 }
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const counts = {};
A.map((elem) => {
return [
elem.reduce((p, c) => `${p}${c}`, ""),
elem.reduce((p, c) => `${p}${Number(!c)}`, "")
].sort();
})
.forEach((e) => counts[e[0]] = (counts[e[0]] || 0) + 1)
console.log(Object.values(counts).sort(() ))
console.log(counts)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const counts = {};
A.map((elem) => {
return [
elem.reduce((p, c) => `${p}${c}`, ""),
elem.reduce((p, c) => `${p}${Number(!c)}`, "")
].sort();
})
.forEach((e) => counts[e[0]] = (counts[e[0]] || 0) + 1)
console.log(Object.values(counts).sort((a, b) => a - b ))
console.log(counts)
}
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 1, 2 ] { '0000': 1, '0100': 2 }
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 4 ] { '0101': 4 }
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const counts = {};
A.map((elem) => {
return [
elem.reduce((p, c) => `${p}${c}`, ""),
elem.reduce((p, c) => `${p}${Number(!c)}`, "")
].sort();
})
.forEach((e) => counts[e[0]] = (counts[e[0]] || 0) + 1)
console.log(Object.values(counts).sort((a, b) => b - a ))
console.log(counts)
}
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 2, 1 ] { '0000': 1, '0100': 2 }
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 4 ] { '0101': 4 }
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const counts = {};
A.map((elem) => {
return [
elem.reduce((p, c) => `${p}${c}`, ""),
elem.reduce((p, c) => `${p}${Number(!c)}`, "")
].sort();
})
.forEach((e) => counts[e[0]] = (counts[e[0]] || 0) + 1)
console.log(Object.values(counts).sort((a, b) => b - a )[0])
console.log(counts)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const counts = {};
A.map((elem) => {
return [
elem.reduce((p, c) => `${p}${c}`, ""),
elem.reduce((p, c) => `${p}${Number(!c)}`, "")
].sort();
})
.forEach((e) => counts[e[0]] = (counts[e[0]] || 0) + 1)
console.log(Object.values(counts).sort((a, b) => b - a )[0])
console.log(counts)
}
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
2 { '0000': 1, '0100': 2 }
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
4 { '0101': 4 }
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const counts = {};
A.map((elem) => {
return [
elem.reduce((p, c) => `${p}${c}`, ""),
elem.reduce((p, c) => `${p}${Number(!c)}`, "")
].sort();
})
.forEach((e) => counts[e[0]] = (counts[e[0]] || 0) + 1)
console.log(Object.values(counts).sort((a, b) => b - a )[0])
console.log(counts)
}
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
2 { '0000': 1, '0100': 2 }
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
4 { '0101': 4 }
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
1 { '0000': 1 }
[[0, 0, 0, 0]]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const counts = {};
A.map((elem) => {
return [
elem.reduce((p, c) => `${p}${c}`, ""),
elem.reduce((p, c) => `${p}${Number(!c)}`, "")
].sort();
})
.forEach((e) => counts[e[0]] = (counts[e[0]] || 0) + 1)
console.log()
console.log(counts)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const counts = {};
A.map((elem) => {
return [
elem.reduce((p, c) => `${p}${c}`, ""),
elem.reduce((p, c) => `${p}${Number(!c)}`, "")
].sort();
})
.forEach((e) => counts[e[0]] = (counts[e[0]] || 0) + 1)
return Object.values(counts).sort((a, b) => b - a )[0];
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const counts = {};
A.map((elem) => {
return [
elem.reduce((p, c) => `${p}${c}`, ""),
elem.reduce((p, c) => `${p}${Number(!c)}`, "")
].sort();
})
.forEach((e) => counts[e[0]] = (counts[e[0]] || 0) + 1)
return Object.values(counts).sort((a, b) => b - a )[0];
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const counts = {};
A.map((elem) => {
return [
elem.reduce((p, c) => `${p}${c}`, ""),
elem.reduce((p, c) => `${p}${Number(!c)}`, "")
].sort();
})
.forEach((e) => counts[e[0]] = (counts[e[0]] || 0) + 1)
return Object.values(counts).sort((a, b) => b - a )[0];
}
The solution obtained perfect score.