let N = 12 let arr = [1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 1]
Your task is to find the maximum number of times an odd number is continuously repeated in the array.
can anyone please tell me approach for this?
this is hint 1 is repeated 4 times from index 0 to index 3 =>4times
2 is repeated 5 times from index 4 to index 8 =>5times
1 is repeated 3 times from index 9 to index 11 =>3times
Odd number in array are 1.
1 occurs4times and3times continuously, so 4 is maximum number of times an odd number is continuously repeated in this array
`
function longestRepeatedOdd(N, array) {
//write code here
let count = 0;
for (let i = 0; i <= array.length-1; i ){
if (array[i] % 2 !== 0){
count
}else if (array[i] % 2 === 0){
break;
}
}
console.log(count)
}
`
CodePudding user response:
Your answer is definitely valid just replace break with continue.
function longestRepeatedOdd(N, array) {
//write code here
let count = 0;
for (let i = 0; i <= array.length-1; i ){
if (array[i] % 2 !== 0){
count
}else if (array[i] % 2 === 0){
continue;
}
}
return count
}
CodePudding user response:
The code first initializes the maxCount and currentCount variables to 0. Then, it loops through each element in the array and checks if it is odd. If it is, it increments the currentCount by 1. If it is not, it resets the currentCount to 0. Finally, it compares the currentCount to the maxCount and updates the maxCount if the currentCount is greater. This allows us to find the maximum number of times an odd number is continuously repeated in the array.
function longestRepeatedOdd(N, array) {
// Initialize the maximum count to 0
let maxCount = 0;
// Initialize the current count to 0
let currentCount = 0;
// Loop through each element in the array
for (let i = 0; i <= array.length-1; i ) {
// Check if the current element is odd
if (array[i] % 2 !== 0) {
// If it is, increment the current count
currentCount ;
} else {
// If it is not, reset the current count
currentCount = 0;
}
// Update the maximum count if the current count is greater
if (currentCount > maxCount) {
maxCount = currentCount;
}
}
// Return the maximum count
return maxCount;
}
CodePudding user response:
Try reduce
and creating a JS object that reports its value
const count = arr.reduce((accumulator, value) => {
return {...accumulator, [value]: (accumulator[value] || 0) 1};
}, {});
Object { 1: 7, 2: 5 }
console.log(count);
Number one repeated 7 times and number two 5 times.
CodePudding user response:
You can use Array.reduce()
to keep track of the current run of odd numbers and the maximum.
We use an accumulator value with a max
and current
field, the current field the current run of odd numbers and the max field is the longest run.
If the current run exceeds the max we set the max to this value.
function longestRepeatedOdd(arr) {
return arr.reduce((acc, n, idx) => {
acc.current = (n === arr[idx - 1] ? acc.current: 0) (n % 2);
acc.max = (acc.current > acc.max) ? acc.current: acc.max;
return acc;
}, { max: 0, current: 0 }).max;
}
const inputs = [
[1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 1],
[7, 7, 4, 4, 1, 1, 1, 1, 1, 1, 4, 3],
[1, 2, 2, 2]
]
inputs.forEach((input, idx) => {
console.log(`Test input #${idx 1} result:`, longestRepeatedOdd(input))
})