Комментарии:
Bro simple is that one if condition if(arr[I+1]-arr[I]===4)
{
return are[I+1]+2;
}
else{
return;
}
let arr = [1,3,5,7,9,13]
function misodd(arr){
let misnum;
for(let i=0;i<arr.length;i++){
if(arr[i]+2!=arr[i+1]){
return arr[i]+2;
}
}
return -1;
}
let ans=misodd(arr)
console.log(ans)
//FIND THE MISSING ODD NUMBER WITHIN THE RANGE SPECIFIED IN THE ARRAY
const input = [5,7,9,11,15,17];
for(let i=input[0]; i<=input[input.length-1]; i++){
if((i % 2 == 1) && (!input.includes(i))){
console.log(i);// OUTPUT - 13
}
}
ok so let assume given arry is sorted
const input = [5,7,9,11,15,17]
let output = null
for(let start = input[0]+2;start<=input[input.length-1];start+=2){
if(!input.includes(start)){
output = start
break;
}
}
let arr = [1, 5, 7, 9, 11,13];
let j = arr[0];
for (let i = 0; i < arr.length; i++) {
if (arr[i] !== j) {
console.log(j)
break
}
j+= 2
}
KeepItSimple
This does work fine!
const inp = [5,7,9,11,15,17];
const outp = [];
for(let i=0; i < inp.length; i++){
if(inp[i]+2 != inp[i+1]){
outp.push(inp[i]+2);
}
}
console.log(outp);
let tab=[5,7,9,11,15,17]
let fullODD=[]
let missing
let min= Math.min(...tab)
let max= Math.max(...tab)
for (let i=min;i<=max;i++) {
i%2!==0 && fullODD.push(i)
}
for (let i=0; i<fullODD.length;i++) {
!tab.includes(fullODD[i]) ? missing=fullODD[i] : null
}
console.log(missing) // 13
let arr = [5,7,9,11,15,17];
for(value of arr){
let a = value +2;
if(!arr.includes(a)){
console.log(a);
break
}
}
function findMissingOdd(arr){
for(let i =0;i<arr.length;i++){
if(arr[i+1]-arr[i] > 2) return arr[i] + 2;
}
return "No element is Missing!"
}
const input =[5,7,9,11,15,17];
const findMissingOdd =(low,high)=>{
const arr=[];
for(let i=low;i<=high;i++){
if(i%2 !==0){
arr.push(i)
}
}
return arr.find((ele)=>!input.includes(ele));
}
console.log(findMissingOdd(input[0],input[input.length-1]))
I guess using some mathematics can be helpful.
But i would use it only if array was large and we need to solve it in minimum time complexity else i would use sorting approach
1) Find max and min at once from array
2)find sum of odd numbers from 1 to nth term(let x)
3)find total sum of elemnts given in array (let y)
4)find total sum of odd number from min to max that we found above(let z)
5)lastly: (x-y)-(x-z)
This can be simplified but you compolsary need x to find y and z
Please correct if i am wrong.
I just ried my approach
easy first sort then extract min and max after than loop range from min to max than extrat odd number list that number not in arr that is missing after first missing get break the array
ОтветитьIt is in AP(Arithmetic Progression)
Just check Common Difference.
Formula: a+(n-1)*d
a is first number, d is common diff
I see in the comments no one has used two pointers for this. With that we can achieve linear time complexity. Use start and end. Run while loop until end >= start. start++ and end--
Ответитьconst arr =[5,7,9,11,15,17,19,23];
const data = new Set(arr)
for(i=5; i<=arr[arr.length-1]; i++){
if(i%2 !==0 ){
if(!data.has(i)){
console.log(i)
}
}
}
let value = [5, 7, 9, 11, 15];
let missingValue = [];
for (let i = 5; i < 15; i = i + 2) {
if (!value.includes(i)) {
missingValue.push(i);
}
}
console.log("🚀 ~ file: basic.js:4 ~ missingValue:", missingValue);
function missingOddNum(arr) {
let completeArr = [arr[0]];
for (let i = 0; i < arr.length; i++) {
let value = completeArr[i] + 2;
completeArr.push(value)
}
return completeArr.filter((num) => !arr.includes(num))
}
const input2 = [5, 7, 9, 11, 15, 17];
function findOddNumber2(input2) {
let minVal = Math.min(...input2);
let maxVal = Math.max(...input2);
// expected odd number
let expectedOddNumber = [];
for (let i = minVal; i <= maxVal; i++) {
if (i % 2 !== 0) {
expectedOddNumber.push(i);
}
}
// using linear search to find the missing odd number
for (let i = 0; i < input2.length; i++) { // or use forEach
if (input2[i] !== expectedOddNumber[i]) {
return expectedOddNumber[i];
}
}
}
console.log(findOddNumber2(input2));
const arr= [3,5,7,9,13]
let odd=arr[0];
for(let i=0;i<arr.length;i++){
if(arr[i]!=odd){
console.log (odd);
}
else{
odd+=2
}
}
const data = [5, 7, 11, 15, 17]
for(let i =data[0]; i <= data[data.length -1]; i ++ ) {
if( i % 2 !== 0){
if(!data.includes(i)) {
console.log(i)
}
}
}
const arr=[5,7,9,11, 15,17]
function findMiss(arr){
for(let i=0; i<arr.length; i++){
if( (arr[i+1]-arr[i]) !== 2){
return arr[i]+2
}
}
}
console.log(findMiss(arr))
function oddcalc(arr){
for(let i=0;i<arr.length;i++){
if(Math.abs(arr[i]-arr[i+1])>2){
missing = (arr[i]+arr[i+1])/2;
return missing;
}
}
}
let val =oddcalc([5, 7, 9, 11, 13, 15, 19]);
console.log(val)
@coderDost Please let us know how to break forEach loop ? without using try-catch
Ответитьfor (let i=0; i<input.length-1; i++ ){
if(!input.includes(input[i]+2)) return input[i]+2;
}
//AP addition formula s= n/2(frst +last)
let len = input.length ;
return (input[0] +input[len-1])(len+1)/2 - input.reduce((a,c)=>a+c);
Solution 1:
const input = [5,7,9,11,15,17]
const findMissing =(arr)=>{
let temp = arr[0]
for(let i= 0; i< arr.length; i++){
if(temp !== arr[i]){
return temp;
}
temp += 2
}
}
console.log(findMissing(input))
let missingOdd = null;
for (let i = nums[0]; i <= nums[nums.length - 1]; i += 2) {
if (!nums.includes(i)) {
missingOdd = i;
break;
}
}
its front end dev interview?? and which type of qus asking in front end dev
Ответитьconst input = [5,7,9,11,15,19];
for(let i=1;i<input.length;i++){
if(input[i-1]+2 != input[i]){
console.log(input[i-1]+2)
break
}
}
let missingNumber;
for (let i = input[0]; i <= input[input.length - 1]; i += 2) {
if (!input.includes(i)) {
missingNumber = i;
break;
}
}
console.log("Missing number:", missingNumber);
function missingOdd(arr) {
for(let i = 1; i < arr.length; i++) {
const last = arr[i - 1];
const current = arr[i];
if(current - last > 2) {
return last + 2;
}
}
}
Binary Search is possible here is the answer:
function findMissingOddNumber (arr) {
let left = 0
let right = arr.length - 1
let startingNumber = arr[0]
let ans = -1
while (left <= right) {
const mid = Math.floor((left + right) / 2)
let expectedNumber = startingNumber + 2 * mid
if (arr[mid] === expectedNumber) {
// Continue searching on the right side
left = mid + 1
} else {
// Continue searching on the left side
ans = expectedNumber
right = mid - 1
}
}
// If no missing number is found
return ans
}
let arr = [5,7,9,11,15,17]
function missNumber(arr){
for(let i =0 ; i < arr.length ; i++ ){
if(!(arr[i] + 2 === arr[i + 1])) return arr[i] + 2
}
}
console.log(missNumber(arr))
function findMissingNumber(arr) {
let missingNumber = null;
for (let i = 0; i < arr.length; i++) {
if (arr[i] !== arr[0] + i * 2) {
missingNumber = arr[0] + i * 2;
break;
}
}
return missingNumber;
}
const arr=[5,7,9,11,15,17];
for (let i=arr[0];i<=arr[arr.length-1];i++){
if(i%2!==0){
if(!arr.includes(i)){
console.log(i);
}
}
}
function missOdd(arr){
let first=0, last=arr.length-1, mid = 0 ,first_element = arr[0];
if(first_element+2*last == arr[last] ) return "no element is missed";
while(first<=last){
mid = Math.floor((first+last)/2);
if(arr[mid]==first_element+2*mid) first = mid+1;
else last = mid-1;
}
return first_element + 2*first;
}
console.log(missOdd([5,7,11,13,15,17]));
😀
const input = [3,5,7,9,11,15,17];
for(let i=0;i<input.length;i++){
if((input[i]+2) != input[i+1]){
console.log(input[i]+2);
break;
};
};
const input = [5,7,9,11,15,17];
const a = [];
const b = [];
for(let i=0;i<input.length;i++){
if(input[i] % 2 !== 0){
a.push(input[i]);
};
};
for(j=5; j<=17; j++){
if(j % 2 !== 0){
b.push(j);
};
};
for(i=0;i<a.length;i++){
if(a[i] !== b[i]){
console.log(b[i]);
break;
};
};
My solution for Sum 1:
let input = [21,23,25,27,31,33,35,37,39,41,45,47,49,53];
for (let i=input[0];i<=input[input.length-1];i++){
if((i%2 !== 0) && (!input.includes(i))){
console.log(i);
}
}
const input=[1,3,5,7,11,13,15,17]
let b;
for (let i=1;i<input.length-1;i++){
if (input[i+1]-input[i]!=input[i]-input[i-1]){
b=(input[i+1]+input[i])/2;
break;
}
}
console.log(b)
const input = [5, 7, 9, 11, 15, 17]
// 13
const missNums = []
function findMissingNum (input){
const minValue = Math.min(...input)
const maxValue = Math.max(...input)
for(let i=minValue; i<maxValue; i++){
if(i % 2 !== 0){
if(input.indexOf(i) === -1){
missNums.push(i)
}
}
}
return missNums
}
console.log(findMissingNum(input));
Binary search is POSSIBLE here.
We can determine which element should be present at ith index using simpe A.P formula and if correct element present, move right else move left side.
Thanks for this content.
@coderdost Binary search is possible here,
Solution: (Explanation at last)
const input = [5, 7, 9, 11, 15, 17,19, 21];
first = 0
last = input.length-1
mid = (first+last)/2
while (first<last){
if (input[mid]===input[0]+(mid*2)&&input[mid+1]!=input[0]+((mid+1)*2)){
mid=mid+1
console.log("Element missing at index ",mid)
console.log("Missing Element ",input[0]+(mid*2))
break
}
if (input[mid]!=input[0]+(mid*2)){
last=mid
mid = (first+last)/2
}
else{
first = mid
mid = ((first+last)/2)+1
}
mid=Math.round(mid)
}
Explanation:
In this question there is a correlation between the index and the element present on the index, the element in the N'th index should always be equal to input[0]+(N*2)
With the help of binary search, we can find the position at the array where the element at mid is the correct element according to this correlation, but the next element is not,
then the missing element can be calculated by putting N = mid+1 in this formula. Also in case of multiple missing numbers, this will always print the first missing number as we are searching for the last correct element according to this formula. If there are no missing numbers, then nothing will be printed.
If the array is an AP, then the approach will remain the same and we will just replace the formula with the one for that particular AP
Coder Dost, Hi
I did not get you "n" concept in last
can you explain me
let input =[5,7,11,13,15,17];
let out = 13
function s(input){
for(let i= 0; i<input.length; i++){
let temp = input[i]
if(temp+2 !== input[i+1])
{
return input[i]+2
}
}
}
// s(input)
console.log(s(input))
my code works with any case:
const input = [1,2,3,4,5,6,7,9];
function checkMate(arr) {
let diff = arr[1] - arr[0];
let i = 0;
let j = arr.length-1;
let ans;
while (i < j) {
if(arr[i] + diff === arr[i+1]) {
i++;
} else {
ans = arr[i] + diff;
console.log(ans);
break;
}
}
}
checkMate(input);
let input = [5, 7, 9, 11,15, 17];
for (let i = 0; i < input.length; i++) {
let diff = input[i + 1]-input[i];
if (diff !== 2){
console.log(input[i]+2);
break;
}
}
let data =[1,3,5,7,9,11,15,17,19,21]
let data2=[];
for(let i=0;i<data.length-1;i++){
if(data[i]+2 !== data[i+1]){
data2.push(data[i]+2);
}
}
console.log(data2);
IS THIS CORRECT WAY:
const input=[5,7,9,11,15,17]
let first =input[0]
for (let i of input){
if (i ===first){
let next = first+2
first=next
}
else{
console.log(first)
}
}
Binary "search", name itself suggests you will be searching for something but since you don't know what to search for then obviously BS will not work here.
Ответить