Skip to content

Instantly share code, notes, and snippets.

View narender2031's full-sized avatar
🎯
Focusing

Narender Singh narender2031

🎯
Focusing
View GitHub Profile
@narender2031
narender2031 / continueNumberCount.js
Last active March 15, 2021 19:26
Given a binary array, find the maximum number of consecutive 1s in this array.
/**
* Given a binary array, find the maximum number of consecutive 1s in this array.
*. Input: [1,1,0,1,1,1]
* Output: 3
* Explanation: The first two digits or the last three digits are consecutive 1s.
* The maximum number of consecutive 1s is 3.
*/
function findMaxConsecutiveOnes(nums) {
const ArrayCount = nums.length;
@narender2031
narender2031 / TapeEquilibrium.js
Created March 14, 2020 08:42
Minimize the value |(A[0] + ... + A[P-1]) - (A[P] + ... + A[N-1])|.
/*
A non-empty array A consisting of N integers is given. Array A represents numbers on a tape.
Any integer P, such that 0 < P < N, splits this tape into two non-empty parts: A[0], A[1], ..., A[P − 1] and A[P], A[P + 1], ..., A[N − 1].
The difference between the two parts is the value of: |(A[0] + A[1] + ... + A[P − 1]) − (A[P] + A[P + 1] + ... + A[N − 1])|
In other words, it is the absolute difference between the sum of the first part and the sum of the second part.
For example, consider array A such that:
/*
An array A consisting of N different integers is given.
The array contains integers in the range [1..(N + 1)],
which means that exactly one element is missing.
For example, given array A such that:
A[0] = 2
A[1] = 3
A[2] = 1
@narender2031
narender2031 / frogJump.js
Created March 12, 2020 20:34
Count minimal number of jumps from position X to Y.
/*
A small frog wants to get to the other side of the road.
The frog is currently located at position X and wants to get to a position greater than or equal to Y.
The small frog always jumps a fixed distance, D.
Count the minimal number of jumps that the small frog must perform to reach its target.
For example, given:
X = 10
@narender2031
narender2031 / frogJump.js
Created March 12, 2020 20:34
Count minimal number of jumps from position X to Y.
/*
A small frog wants to get to the other side of the road.
The frog is currently located at position X and wants to get to a position greater than or equal to Y.
The small frog always jumps a fixed distance, D.
Count the minimal number of jumps that the small frog must perform to reach its target.
For example, given:
X = 10
@narender2031
narender2031 / oddOccuranceInArray.js
Created March 12, 2020 19:18
OddOccurrencesInArray [recursion]
/*
A non-empty array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired.
For example, in array A such that:
A[0] = 9 A[1] = 3 A[2] = 9
A[3] = 3 A[4] = 9 A[5] = 7
A[6] = 9
the elements at indexes 0 and 2 have value 9,
the elements at indexes 1 and 3 have value 3,
@narender2031
narender2031 / index.js
Created March 12, 2020 18:18
Find value that occurs in odd number of elements.
/*
A non-empty array A consisting of N integers is given. The array contains an odd number of elements,
and each element of the array can be paired with another element that has the same value,
except for one element that is left unpaired.
For example, in array A such that:
A[0] = 9 A[1] = 3 A[2] = 9
A[3] = 3 A[4] = 9 A[5] = 7
A[6] = 9
the elements at indexes 0 and 2 have value 9,
@narender2031
narender2031 / index.js
Last active March 12, 2020 18:19
Rotate an array to the right by a given number of steps.
/*
An array A consisting of N integers is given. Rotation of the array means
that each element is shifted right by one index, and the last element of the array is moved
to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7]
(elements are shifted right by one index and 6 is moved to the first place).
*/
let A = [1, 2, 3, 4]
let K = 4
function solution(A: number[], K: number): number[] {
@narender2031
narender2031 / bineryGap.js
Last active March 12, 2020 18:20
Find longest sequence of zeros in binary representation of an integer.
/*
A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.
For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. The number 32 has binary representation 100000 and has no binary gaps.
*/
"use strict";
function solution(A) {
let buneryValue = A.toString(2);
@narender2031
narender2031 / binerySearch.js
Last active March 12, 2020 18:21
Binary Search
const list = [23];
function binarySearch(arr: number[], lookfor: number): number {
let arrLength = arr.length;
let left = 0;
let right = arrLength - 1;
if (arrLength < 2) {
return arr[0] === lookfor ? 0: -1;