This is so simple but brilliant. I’m doing a course on algorithms on Coursera, and the lecturer mentions how even the simplest problems (his example was integer multiplication) can have a far more efficient, clever solutions. This is a pretty good example of that as well.

It’s mostly based on divide and conquer algorithms, but it’s really about the analysis and applications of algorithms for beginners with a base in basic math and programming.

I’ve finished a week, so I can’t comment on the overall quality of the course, but I’m certainly enjoying the video lectures and the (admittedly easy so far) math and analysis. You also have the freedom to code in whichever language you want, which is fantastic since I get to use Python and just focus on the algorithms not syntax. Plus it seems to be completely free.

There’s also a second part to this course, which is tougher and more focused on a variety of algorithms.

EDIT: You found the link and know the course, but I guess I’ll just leave this here anyway
Also I was torn between this and the Princeton one, but I definitely needed a stronger base to do the latter, so there wasn’t really much of a choice.

I sometime want only solutions and I haven’t studied algorithms deeply, so that’s the reason. I liked your solution. A real programmer is who minimizes the effort. Your code reflects algorithmic thinking.

Here is my solution, No idea if it is faster in computing, but it is another approach surly:

function getIndexToIns(arr, num) {
// Find my place in this sorted array.
var myVar = arr.toString() +" , "+ (num);
myVar = JSON.parse("[" + myVar + "]");
myVar = myVar.sort(function(a, b)
{
return a - b;
});
return myVar.indexOf(num);

here is mine, can it be shortened anymore or how to remove the newArr declaration ?

function getIndexToIns(arr, num) {
// Find my place in this sorted array.
var newArr = arr.push(num);
return arr.sort(function(a,b) {return a-b;}).indexOf(num);
}
getIndexToIns([20, 10, 30 , 60, 50, 40], 35);

tekhaus, brilliant solution!
My solution is here. I think I did some extra steps.
Shorter is better!
function getIndexToIns(arr, num) {
var args = arr.slice.call(arguments);
var flattened = args.reduce(function(a,b) {
return a.concat(b);
},[]);

function getIndexToIns(arr, num) {
// Find my place in this sorted array.
arr = arr.sort( function( a, b ){
return a - b;
} );
for( var i = 0; i < arr.length; i++ ) {
if( arr[i] >= num ) { // found first higher element put num here
return i;
}
}
return arr.length;
}
getIndexToIns([40, 60], 50);

I didn’t add the num to the array becuase the challenge said all it wanted was the index of where it would go if it was to be added. I love reading everyone else’s solutions it really makes me think about my code and why i did what i did compared to other people. Great job to everyone that solved it.

this is my solution if it helps… I always take extreme cases and separate them out of the main loop…
`function getIndexToIns(arr, num) {
//alert(arr.sort(function(a,b){return a-b;}));
var newArr = arr.sort(function(a,b){
return a -b;
});
if( num > newArr[newArr.length - 1])
return newArr.length;
for(var i = 0; i<newArr.length; i++){
if(newArr[i] === num)
return i;
else if(num > newArr[i] && num <newArr[i+1]){
return i+1;
}