how to find an almost increasing sequence Array - javascript

I have had an algorithm problem below:
"Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.
Example
For sequence = [1, 3, 2, 1], the output should be
almostIncreasingSequence(sequence) = false;
There is no one element in this array that can be removed in order to get a strictly increasing sequence.
For sequence = [1, 3, 2], the output should be
almostIncreasingSequence(sequence) = true.
You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, you can remove 2 to get the strictly increasing sequence [1, 3]."
My code (for Javascript) was:
function almostIncreasingSequence(sequence) {
var count =0;
for (i =0 ; i<sequence.length-1 ; i++){
if (sequence[i+1]<=sequence[i]){
count++;
}
}
return count <2;
}
However if in the case of sequence =[1,2,3,4,3,4,5]
My code will be wrong;
Please explain to me an algorithm that can be used to solve this problem.
And also please explain in step by step so that I can understand.
Sorry for not being clear as it is my first question here.

This may help.
function almostIncreasingSequence(sequence) {
var exists = false; // Assume the sequence returns false by default
var mainSequence = sequence.slice(); // Clone sequence so that we can use it again
m_seq: // The name of the first loop, so that we can exit from it
for (var i = 0; i < mainSequence.length; i++) { // Loop through the array
sequence.splice(i,1); // Remove an item from the array
s_seq: // The name of the loop, so that we can exit from it
for (var j = 0; j < mainSequence.length-1; j++) { // Loop through sliced array, to find an increasing sequence
if (sequence[j+1] <= sequence[j]) { // Check if next is smaller
exists = false; // if so define false
break s_seq; // exit from the loop
}
exists = true; // if the loop has not been interrupted, return true
}
sequence = mainSequence.slice(); // Reassign the main sequence to be used again.
//console.log('s',mainSequence);
if (exists) {break m_seq;} // İf condition is met, why bother to stay in the loop???
}
return exists;
}
var testing = [1,2,3,4,3,4,5];
var testingAgain = [1,2,3,4,3,7,9];
var testingFinal = [1,1];
// almostIncreasingSequence(testing);
console.log(almostIncreasingSequence(testing));
console.log(almostIncreasingSequence(testingAgain));
console.log(almostIncreasingSequence(testingFinal));

Related

Why does an object exist in two sum solution? [duplicate]

Im just wondering who can explain the algorithm of this solution step by step. I dont know how hashmap works. Can you also give a basic examples using a hashmap for me to understand this algorithm. Thank you!
var twoSum = function(nums, target) {
let hash = {};
for(let i = 0; i < nums.length; i++) {
const n = nums[i];
if(hash[target - n] !== undefined) {
return [hash[target - n], i];
}
hash[n] = i;
}
return [];
}
Your code takes an array of numbers and a target number/sum. It then returns the indexes in the array for two numbers which add up to the target number/sum.
Consider an array of numbers such as [1, 2, 3] and a target of 5. Your task is to find the two numbers in this array which add to 5. One way you can approach this problem is by looping over each number in your array and asking yourself "Is there a number (which I have already seen in my array) which I can add to the current number to get my target sum?".
Well, if we loop over the example array of [1, 2, 3] we first start at index 0 with the number 1. Currently, there are no numbers which we have already seen that we can add with 1 to get our target of 5 as we haven't looped over any numbers yet.
So, so far, we have met the number 1, which was at index 0. This is stored in the hashmap (ie object) as {'1': 0}. Where the key is the number and the value (0) is the index it was seen at. The purpose of the object is to store the numbers we have seen and the indexes they appear at.
Next, the loop continues to index 1, with the current number being 2. We can now ask ourselves the question: Is there a number which I have already seen in my array that I can add to my current number of 2 to get the target sum of 5. The amount needed to add to the current number to get to the target can be obtained by doing target-currentNumber. In this case, we are currently on 2, so we need to add 3 to get to our target sum of 5. Using the hashmap/object, we can check if we have already seen the number 3. To do this, we can try and access the object 3 key by doing obj[target-currentNumber]. Currently, our object only has the key of '1', so when we try and access the 3 key you'll get undefined. This means we haven't seen the number 3 yet, so, as of now, there isn't anything we can add to 2 to get our target sum.
So now our object/hashmap looks like {'1': 0, '2': 1}, as we have seen the number 1 which was at index 0, and we have seen the number 2 which was at index 1.
Finally, we reach the last number in your array which is at index 2. Index 2 of the array holds the number 3. Now again, we ask ourselves the question: Is there a number we have already seen which we can add to 3 (our current number) to get the target sum?. The number we need to add to 3 to get our target number of 5 is 2 (obtained by doing target-currentNumber). We can now check our object to see if we have already seen a number 2 in the array. To do so we can use obj[target-currentNumber] to get the value stored at the key 2, which stores the index of 1. This means that the number 2 does exist in the array, and so we can add it to 3 to reach our target. Since the value was in the object, we can now return our findings. That being the index of where the seen number occurred, and the index of the current number.
In general, the object is used to keep track of all the previously seen numbers in your array and keep a value of the index at which the number was seen at.
Here is an example of running your code. It returns [1, 2], as the numbers at indexes 1 and 2 can be added together to give the target sum of 5:
const twoSum = function(nums, target) {
const hash = {}; // Stores seen numbers: {seenNumber: indexItOccurred}
for (let i = 0; i < nums.length; i++) { // loop through all numbers
const n = nums[i]; // grab the current number `n`.
if (hash[target - n] !== undefined) { // check if the number we need to add to `n` to reach our target has been seen:
return [hash[target - n], i]; // grab the index of the seen number, and the index of the current number
}
hash[n] = i; // update our hash to include the. number we just saw along with its index.
}
return []; // If no numbers add up to equal the `target`, we can return an empty array
}
console.log(twoSum([1, 2, 3], 5)); // [1, 2]
A solution like this might seem over-engineered. You might be wondering why you can't just look at one number in the array, and then look at all the other numbers and see if you come across a number that adds up to equal the target. A solution like that would work perfectly fine, however, it's not very efficient. If you had N numbers in your array, in the worst case (where no two numbers add up to equal your target) you would need to loop through all of these N numbers - that means you would do N iterations. However, for each iteration where you look at a singular number, you would then need to look at each other number using a inner loop. This would mean that for each iteration of your outer loop you would do N iterations of your inner loop. This would result in you doing N*N or N2 work (O(N2) work). Unlike this approach, the solution described in the first half of this answer only needs to do N iterations over the entire array. Using the object, we can find whether or not a number is in the object in constant (O(1)) time, which means that the total work for the above algorithm is only O(N).
For further information about how objects work, you can read about bracket notation and other property accessor methods here.
You may want to check out this method, it worked so well for me and I have written a lot of comments on it to help even a beginner understand better.
let nums = [2, 7, 11, 15];
let target = 9;
function twoSums(arr, t){
let num1;
//create the variable for the first number
let num2;
//create the variable for the second number
let index1;
//create the variable for the index of the first number
let index2;
//create the variable for the index of the second number
for(let i = 0; i < arr.length; i++){
//make a for loop to loop through the array elements
num1 = arr[i];
//assign the array iteration, i, value to the num1 variable
//eg: num1 = arr[0] which is 2
num2 = t - num1;
//get the difference between the target and the number in num1.
//eg: t(9) - num1(2) = 7;
if(arr.includes(num2)){
//check to see if the num2 number, 7, is contained in the array;
index1 = arr.indexOf(num2);
//if yes get the index of the num2 value, 7, from the array,
// eg: the index of 7 in the array is 1;
index2 = arr.indexOf(num1)
//get the index of the num1 value, which is 2, theindex of 2 in the array is 0;
}
}
return(`[${index1}, ${index2}]`);
//return the indexes in block parenthesis. You may choose to create an array and push the values into it, but consider space complexities.
}
console.log(twoSums(nums, target));
//call the function. Remeber we already declared the values at the top already.
//In my opinion, this method is best, it considers both time complexity and space complexityat its lowest value.
//Time complexity: 0(n)
function twoSum(numbers, target) {
for (let i = 0; i < numbers.length; i++) {
for (let j = i + 1; j < numbers.length; j++) {
if (numbers[i] + numbers[j] === target) {
return [numbers.indexOf(numbers[i]), numbers.lastIndexOf(numbers[j])];
}
}
}
}

javascript: how to make function run faster

so i have to write a function which meets the following requirements:
Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.
Example:
For sequence = [1, 3, 2, 1], the output should be
almostIncreasingSequence(sequence) = false;
There is no one element in this array that can be removed in order to get a strictly increasing sequence.
For sequence = [1, 3, 2], the output should be
almostIncreasingSequence(sequence) = true.
You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, you can remove 2 to get the strictly increasing sequence [1, 3].
Input/Output
[time limit] 4000ms (js)
[input] array.integer sequence
Guaranteed constraints:
2 ≤ sequence.length ≤ 105,
-105 ≤ sequence[i] ≤ 105.
so my code works except for one problem--there are 30 tests it has to pass with the time constraint of 4000ms, but it always times out on the 30th test, every time. i have tried modifying it so that it runs faster, but each time i do so it no longer works correctly. although i technically only have to write one function, i broke it up into three separate functions. here's my code:
var greater = function(a, b) {
if (a < b) {
return true
} else {
return false
}
}
function greaterThan(arr) {
for (var i = 0; i < arr.length-1; i++) {
var curr = arr[i]
var next = arr[i + 1]
if (greater(curr, next) === false) {
return false
}
}
return true
}
function almostIncreasingSequence(sequence) {
for(var i = 0; i < sequence.length; i++) {
var newArr = sequence.slice()
newArr.splice(i, 1)
if (greaterThan(newArr) === true) {
return true
}
}
return false
}
so how can i make it run faster without using two for-loops/iterations to do this?
Improving the algorithm may bring better results than improving the code. Here's the problem:
If the sequence is not strictly increasing at index i, such that a[i]>=a[i+1] is true, either a[i] or a[i+1] must be removed to possibly make the array strictly increasing - they can't both be left in place.
If the input array is to be fixable by removing only one element, and it decreases after the ith element it must become strictly increasing by removing the element with subscript i or (i+1).
Compare the efficiency of checking the original array and at most two sub arrays before returning true or false, with checking the same number of arrays as the original array's length. I'll leave re-writing the code to you - it's not my homework :-)

javascript - How to check if array has at least one negative value?

I created an array of integers and wanted to know if it had one or more negative values in it.
I do not want to create a for() loop and check if each element in the array is positive or negative because I only want to return once (ex: I don't want my function to "return false;" a million times).
One option I considered was multiplying each value in the array by the absolute value of its reciprocal, so I get an array of 1s or -1s (or undefined if value is 0) and then I could sum all of the values in this second array to see if it equals the length of the array.
However, the problem with this method is it does not account for 1/0, and also it is tedious. I want to know if there is a faster way to check if an array contains at least one negative value.
--from a beginner JavaScript programmer
You could leverage Array.prototype.some which will return true or false if an item in the array matches the given condition. It'll also stop checking remaining values if the condition matches an element:
let values = [1, 4, 6, -10, -83];
let hasNegative = values.some(v => v < 0);
why do you find min value in array?
see
JavaScript: min & max Array values?
Math.min(...array)
What's the issue with the for loop needing to return false all the time?
fucntion containsNegative(myArray)
for(var i = 0; i < myArray.length(); i++)
{
if(myArray[i] < 0){
return true;
}
}
return false;
}
and if you wanted to get the amount of negative numbers
fucntion getNegative(myArray)
var count = 0;
for(var i = 0; i < myArray.length(); i++)
{
if(myArray[i] < 0){
count++
}
}
return count;
}
This will only return 1 value and break as soon as a negative is found.
function doesArrayContainNegative(array){
//if negative is found return true (breaking loop)
for(var arr of array){
if(arr < 0) return true;
}
//if no elements are negative return false
return false;
}
var array1 = [9, -3, 5, 8]
console.log(doesArrayContainNegative(array1))

Javascript Arrays - Find Duplicates [duplicate]

This question already has answers here:
Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
(97 answers)
Closed 8 years ago.
Here is my question...
Given an array populated with numbers as a function parameter, produce a resulting array which contains any duplicates number from the array.
For example, given the array [ 1, 2, 4, 4, 3, 3, 1, 5, 3 ] it should return [1, 4, 3]. For extra bonus points return a sorted array.
I am starting out with Javascript - I know the language however, using it in the correct way ( as one should ) I'm still getting to grips with.
My pseudo code for this would be to:
Create an array with the numbers above var numbers = [1, 2, 4, 4, 3, 3, 1, 5, 3];
Then create an empty array named "result" var result = [];
Create a for loop that goes through the var numbers to check for duplicates which will then populate the empty array "result" with the duplicates
for (var i = 0;i < numbers.length; i++) {
//This is where I'm stuck...
}
I'm not sure what to do within the for loop to populate the var result and to throw in to the mix... The given array has to be a function parameter which makes sense so you can change the numbers in one place.
Any feedback on my thought process on this so far is greatly appreciated but ultimately I am wanting to learn how to achieve this.
Here is a JSFiddle of my progress so far... http://jsfiddle.net/fbauW/
One way of doing this (and it's not the only way) is by checking for existing elements in the array. Take a look at JavaScript's lastIndexOf function:
http://www.w3schools.com/jsref/jsref_lastindexof_array.asp
It will return -1 if the object does not exist in your array, and if it exists, will return an index of a later position than you are in. So you can use an if statement in your loop that checks whether or not there is another index containing your number, and add it in to your results array IF AND ONLY IF the index you get back != the index you are currently on (if they equal, this means that there is only one of that element in the list).
If you need more help, comment here and I can type some code in!
Good luck!
Array.prototype.contains = function(k) {
for ( var p in this)
if (this[p] === k)
return true;
return false;
};
//this prototype function checks if an element is already in the array or not
//go through all the array and push the element to result if it is not
//this way we can eliminate duplicates
//result will contain the resultant array
function findDuplicates(Numbers) {
var arrayLength = Numbers.length, i, j, result = [];
for (i = 0; i < arrayLength; i++) {
for (j = 0; j < arrayLength; j++) {
if (a[i] == a[j] && i != j && !result.contains(a[i])) {
result.push(a[i]);
}
}
}
return result;
}

I am pushing items into an array but the script becomes unresponsive?

I'm trying to add an array's members back to itself using a for loop.
Why does this code cause an unresponsive script?
var magicarray = {
arraymemeber: [1, 2, 3, 4, 5],
duplicate: function () {
for (var i = 0; i < this.arraymemeber.length; i++) {
this.arraymemeber.push(this.arraymemeber[i]);
};
}
};
console.log(magicarray.duplicate());
Though I'm not sure why you want this, to avoid an infinite loop that you currently have get the length first and iterate only over the original length of the array.
var magicarray = {
arraymemeber: [1,2,3,4,5],
duplicate: function() {
var length = this.arraymemeber.length;
for (var i = 0; i < length; i++) {
this.arraymemeber.push(this.arraymemeber[i]);
};
}
};
console.log(magicarray.duplicate());
Because you're pushing new items in as you iterate, and your condition is based on the current .length, causing an infinite iteration (or at least as high as the .length will be allowed to go).
If you wanted to "double" the Array, you don't need a loop. You can do this:
this.arraymember.push.apply(this.arraymember, this.arraymember)
So your object would be:
var magicarray = {
arraymemeber: [1, 2, 3, 4, 5],
duplicate: function () {
this.arraymember.push.apply(this.arraymember, this.arraymember)
}
};
Every time you go around the loop, you put an item onto the array. This increases its length by 1.
Since this.arraymemeber.length increases at the same rate as i, you never get to the end of the loop.
Because in each iteration you're adding a new element to the element you're iterating over.
The i < this.arraymemeber.length check is made at the end of each iteration. The length of the array isn't cached in any way.
To prevent an infinite loop, use
for (var i = 0, len = this.arraymemeber.length; i<len; i++) {
... instead.
Just do this:
this.arraymemeber = this.arraymemeber.concat(this.arraymemeber);

Categories

Resources