Skip to content

Instantly share code, notes, and snippets.

View thunder775's full-sized avatar
🎯
Focusing

Rahul Masih thunder775

🎯
Focusing
View GitHub Profile
// isPositiveDominant([1, 1, 1, 1, -3, -4])// false
// // There is only 1 unique positive value (1).
// // There are 2 unique negative values (-3, -4).
// isPositiveDominant([5, 99, 832, -3, -4])// true
// isPositiveDominant([5, 0])// true
isPositiveDominant([0, -4, -1])// false
function isPositiveDominant(arr) {
function sortByLength(arr = []) {
for (let j = 0; j < arr.length; j++) {
for (let i = 0; i < arr.length - j; i++) {
const [x, y] = [arr[i], arr[i + 1]]
if (arr[i + 1] && arr[i].length > arr[i + 1].length) swap(arr, i, i + 1)
}
}
console.log(arr)
return arr
function nQueenPlacement(board) {
let rowPositions = [];
let colPositions = [];
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board[i].length; j++) {
if (board[i][j] === 1) {
if (rowPositions.indexOf(i) === -1 && colPositions.indexOf(j) === -1 && pairModulusDiffers(rowPositions, colPositions, i, j)) {
rowPositions.push(i);
colPositions.push(j);
function decipherThis(string) {
const words = string.split(' ');
return words.map(word => decode(word.split(''))).join(' ');
}
function decode(wordArr) {
let hex = ''
let secChar
for (let [idx, char] of wordArr.entries()) {
if (Number(char) || char == 0) {
function fold(arr) {
let start = 0;
let end = arr.length - 1;
const res = []
while (start < end) {
res.push(arr[start] + arr[end]);
start++;
end--;
}
if (arr.length % 2 === 1) res.push(arr[start])
function onesInfection(matrix) {
const rowSet = new Set()
const colSet = new Set()
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] == 1) {
rowSet.add(i)
colSet.add(j)
}
}
const country_codes_array = ["AF","AL","DZ","AS","AD","AO","AI","AQ","AG","AR","AM","AW","AU","AT","AZ","BS","BH","BD","BB","BY","BE","BZ","BJ","BM","BT","BO","BQ","BA","BW","BV","BR","IO","BN","BG","BF","BI","KH","CM","CA","CV","KY","CF","TD","CL","CN","CX","CC","CO","KM","CG","CD","CK","CR","HR","CU","CW","CY","CZ","CI","DK","DJ","DM","DO","EC","EG","SV","GQ","ER","EE","ET","FK","FO","FJ","FI","FR","GF","PF","TF","GA","GM","GE","DE","GH","GI","GR","GL","GD","GP","GU","GT","GG","GN","GW","GY","HT","HM","VA","HN","HK","HU","IS","IN","ID","IR","IQ","IE","IM","IL","IT","JM","JP","JE","JO","KZ","KE","KI","KP","KR","KW","KG","LA","LV","LB","LS","LR","LY","LI","LT","LU","MO","MK","MG","MW","MY","MV","ML","MT","MH","MQ","MR","MU","YT","MX","FM","MD","MC","MN","ME","MS","MA","MZ","MM","NA","NR","NP","NL","NC","NZ","NI","NE","NG","NU","NF","MP","NO","OM","PK","PW","PS","PA","PG","PY","PE","PH","PN","PL","PT","PR","QA","RO","RU","RW","RE","BL","SH","KN","LC","MF","PM","VC","WS","SM","ST","SA","SN","RS","SC","SL","SG",
function maxSubarray(arr, k) {
if (arr.length < k) return 0
let currSubSum = arr.slice(0, k).reduce((a, b) => a + b);
let sum = currSubSum;
for (let i = k; i < arr.length; i++) {
currSubSum = currSubSum + arr[i] - arr[i - k]
sum = Math.max(sum, currSubSum)
}
return sum
}
function getMaxProfit(array) {
let profit = 0;
for (let i = 0; i < array.length; i++) {
let max = array[i]
for (let j = i + 1; j < array.length; j++) {
if (max < array[j]) max = array[j]
}
profit += max - array[i]
}
return profit
function validIp(ip) {
const ipArray = ip.split(".")
console.log(ipArray.length)
return ipArray.length === 4 && !ipArray.some(address => 0 > Number(address) || Number(address) > 255)
}
console.log(validIp('1.2.3.4'))
console.log(validIp('123.45.67.89'))
console.log(validIp('1.2.3.4.5'))
console.log(validIp('1.2.3'))