Skip to content

Instantly share code, notes, and snippets.

View rohan-paul's full-sized avatar
🎯
Focusing

Rohan Paul rohan-paul

🎯
Focusing
View GitHub Profile
{
"results" : [
{
"address_components" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
To store a grid of values, we have several options. We can use an array of row arrays and use two property accesses to get to a specific square, like this:
var grid = [["top left", "top middle", "top right"], ["bottom left", "bottom middle", "bottom right"]];
console.log(grid[1][2]);
// . bottom right
Or we can use a single array, with size width × height, and decide that
the element at (x,y) is found at position x + (y × width) in the array.
function myFunction(param) {
console.log(this + " says hello " + param);
}
myFunction.call("Paul", "world")
// Output => Paul says hello world
var Website = {
updatePageView: function() {
for(var i = 0; i < arguments.length; i++) {
this.pageViewNumber += arguments[i];
}
return this.pageViewNumber;
}
}
var profilePage = {
function myFunc() {
return Array.prototype.slice.call(arguments);
// args is now a real Array, so can use the sort() method from Array
}
console.log(myFunc('USA', 'UK', 'India'));
console.log(myFunc('USA', 'UK', 'India').sort());
// Will output =>
// [ 'USA', 'UK', 'India' ]
// [ 'India', 'UK', 'USA' ]
/*Problem - Given an array, find the int that appears an odd number of times. There will always be only one integer that appears an odd number of times.
Some examples -
[1] // => 1 (odd number of ones, no other numbers)
[1, 1, 2] // => 2 (even number of ones, odd number of twos)
[1, 1, 3, 5, 5] // => 3 (even number of ones and fives, odd number of threes)
[1, 2, 1, 2, 1] // => 1 (even number of twos, odd number of ones)
[1, 1, 2, 2] // => undefined behavior (no number with an odd number of occurrences)
[1, 2] // => undefined behavior (more than one number with an odd number of occurrences)*/
function findOdd(A) {
const findOdd = (xs) => (xs).reduce((a, b) => a ^ b);
// First generate a long enough array sequence to test the code with.
var generateArray = Array.from(new Array(100000),(val,index)=>index);
var startMyCode = Date.now();
function findOddMyCode(A) {
var len = A.length;
var A_sort = A.slice().sort();
var count = {};
function crossSubarray(array, left, middle, right) {
var leftSum = -Infinity;
var rightSum = -Infinity;
var sum = 0;
// Include elements on left of mid
for (var i = middle; i >= left; i--) {
if (sum + array[i] >= leftSum) {
leftSum = sum + array[i];
}
sum += array[i];
/*The idea behind -
A) Kadane's Algo - Basically I have to look for all contiguous sub-arrays of size 4, and also keep track of the maximum sum contiguous sub-array until the end. Whenever I find a new contiguous sub-array, I check if the current sum is greater than the max_sum so far and updates it accordingly.
B) In the first loop is I am just generating the sum of the sub-array of the first 4 elements.
C) In the second loop, I am traversing a sliding window - at each iteration, I am deducting the first element from left and adding next element to the right. And after doing this, updaing the max_so_far to its highest value, by comparing it to its previous hightest value.
*/
function findMaxAverage(nums, k) {
var curr_max = 0;
for (var i = 0; i < k; i++) {