//Binary Search implementation //Time Complexity: O (log n) //return int {-1 , mid index}
int binarySearch(int tar, vector<int>& arr){
int lo=0, md, hi=arr.size()-1;
while(hi>=lo){
md = lo + (hi - lo) / 2;
if(arr[md]==tar) return md;
else if(arr[md]<tar) lo=md+1;
else hi=md-1;
}
return -1;
}
---------------------------------------------------------------------------------------------------
//Binary Search by using Stls //Time Complexity: O (log n)
// for binary search in containers like vector (let target element=6)
binary_search(v.begin(), v.end(), 6);
// return 1 or 0 as present or not
---------------------------------------------------------------------------------------------------
//binary search with lower and upper bound by using stls
int x, ans; //x is passing value, ans is a value you want, v --> vector or any container
ans = lower_bound(v.begin(), v.end(), x) - v.begin(); //ans >= x (equal number or first number after x)
ans = upper_bound(v.begin(), v.end(), x) - v.begin(); // ans > x (first number after x)

//implementation
int lower_bound(vector<int> nums, int target){
int l = 0, r = nums.size()-1, m = 0;
while(l < r) {
m = (l+r)/2;
if(nums[m] < target)
l = m+1;
else
r = m;
}
return r;
}

int upper_bound(vector<int> nums, int target){
int l = 0, r = nums.size()-1, m = 0;
while(l < r) {
m = (l+r)/2;
if(nums[m] <= target)
l = m+1;
else
r = m;
}
return r;
}
---------------------------------------------------------------------------------------------------
// Two Pointer implementation
// Two pointer technique based solution to find
// if there is a pair in A[0..N-1] with a given sum.
int isPairSum(int A[], int N, int X)
{
// represents first pointer
int i = 0;

// represents second pointer
int j = N - 1;

while (i < j) {

int curSum = A[i]+arr[j];

// If we find a pair
if curSum == X)
return 1;

// If sum of elements at current
// pointers is less, we move towards
// higher values by doing i++
else if (curSum < X)
i++;

// If sum of elements at current
// pointers is more, we move towards
// lower values by doing j--
else
j--;
}
return 0;
}
//Sum of numbers from 1 ---> n
int n, sum;
sum = (n*(n+1))/2;

//flooring integer number (int)        "floor أرضيه"
int a=5, b=2;
int n = a/b; // n= 2

//Ceiling integer number (int)          "ceil سقف"
int a=5, b=2;
int ans = (a+(b-1))/b; //n=3

//Division Theory
int a=10, b=3;     // a ---> numerator البسط / b ---> denominator المقام
int ans = a/b;     // n= 3
//meaning:
//1- you can give everyone of b ans
//2- ans is a number of numbers from 1--> a which is divisible by b (find multiple in Range)

//Number of Steps
# Infinity, and it's brother minus infinity,
# comes in handy once in a while.

my_inf = float('Inf')
99999999 > my_inf
-> False

my_neg_inf = float('-Inf')
my_neg_inf > -99999999
-> False
def fibonacci(n):
# return a list of fibonacci numbers
numbers = [0, 1]
for i in range(2,n):
numbers.append(numbers[i-1] + numbers[i-2])
return numbers[0:n]
def max_by(lst, fn):
return max(map(fn, lst))
const arrAvg = arr => arr.reduce((a,b) => a + b, 0) / arr.length
// arrAvg([20, 10, 5, 10]) -> 11.25
const arrSum = arr => arr.reduce((a,b) => a + b, 0)
// arrSum([20, 10, 5, 10]) -> 45
const arrMax = arr => Math.max(...arr);
// arrMax([20, 10, 5, 10]) -> 20
const arrMin = arr => Math.min(...arr);
// arrMin([20, 10, 5, 10]) -> 5

star

Wed Aug 17 2022 20:36:19 GMT+0000 (UTC) https://www.geeksforgeeks.org/implementing-upper_bound-and-lower_bound-in-c/

#c++ #math #binary_search #two_pointer
star

Wed Aug 17 2022 20:33:33 GMT+0000 (UTC)

#c++ #math
star

Mon Feb 28 2022 08:47:17 GMT+0000 (UTC) https://www.quora.com/What-are-some-cool-Python-tricks

#python #math #physics #stem
star

Thu Jan 06 2022 08:26:09 GMT+0000 (UTC) https://stackoverflow.com/questions/21857619/fibonacci-sequence-using-list-in-python

#python #math #python3
star

Mon Mar 23 2020 07:13:49 GMT+0000 (UTC) https://www.30secondsofcode.org/python/s/max-by/

#python #python #math #list
star

Sun Jan 05 2020 19:39:47 GMT+0000 (UTC) https://codeburst.io/javascript-arrays-finding-the-minimum-maximum-sum-average-values-f02f1b0ce332

#javascript #webdev #arrays #math
star

Sun Jan 05 2020 19:37:35 GMT+0000 (UTC) https://codeburst.io/javascript-arrays-finding-the-minimum-maximum-sum-average-values-f02f1b0ce332

#javascript #webdev #arrays #math
star

Sun Jan 05 2020 19:35:54 GMT+0000 (UTC) https://codeburst.io/javascript-arrays-finding-the-minimum-maximum-sum-average-values-f02f1b0ce332

#javascript #webdev #javascript #arrays #math
star

Sun Jan 05 2020 19:31:48 GMT+0000 (UTC) https://codeburst.io/javascript-arrays-finding-the-minimum-maximum-sum-average-values-f02f1b0ce332

#javascript #webdev #arrays #math