n = n << 1
n = n >> 1
n & 1 ? "ODD" : "EVEN"
*max_element(a, a+n)
*min_element(a, a+n)
sort(a, a+n)
sort(a, a+n, greater<int>())
swap(a, b)
or you can use
a ^= b;
b ^= a;
a ^= b;
ll t; cin>>t; while(t--){}
__gcd(a, b)
or you can use
ll gcd(ll a, ll b){return b ? gcd(b, a % b) : a;}
or you can use
ll gcd(ll a,ll b){ll r; while(b){r = a % b; a = b; b = r;} return a;}
floor(log10(n)) + 1
void SieveOfEratosthenes(int n){
bool prime[n+1];
memset(prime, true, sizeof(prime));
for (int p = 2; p * p <= n; p++){
if (prime[p] == true){
for (int i = p * 2; i <= n; i += p)
prime[i] = false;
}
}
for (int p = 2; p <= n; p++){
if (prime[p]){
cout << p << " ";
}
}
}
void primeFactors(int n){
while (n % 2 == 0){
printf("%d ", 2);
n = n / 2;
}
for (int i = 3; i <= sqrt(n); i = i + 2){
while (n % i == 0){
cout << i;
n = n / i;
}
}
if (n > 2){
cout << n;
}
}
or you can use Sieve of Eratosthenes
(a * b) / __gcd(a, b)
int maxSubArraySum(int a[], int size){
int max_so_far = INT_MIN, max_ending_here = 0;
for (int i = 0; i < size; i++){
max_ending_here = max_ending_here + a[i];
if (max_so_far < max_ending_here){
max_so_far = max_ending_here;
}
if (max_ending_here < 0){
max_ending_here = 0;
}
}
return max_so_far;
}
bool isPowerOfTwo(int n){
return n && (!(n&(n-1)));
}
copy_n(source, n, destination);
long double nthRoot(ll A, ll N)
{
// intially guessing a random number between
// 0 and 9
long double xPre = rand() % 10;
// smaller eps, denotes more accuracy
long double eps = 1e-3;
// initializing difference between two
// roots by INT_MAX
long double delX = INT_MAX;
// xK denotes current value of x
long double xK;
// loop untill we reach desired accuracy
while (delX > eps)
{
// calculating current value from previous
// value by newton's method
xK = ((N - 1.0) * xPre +
(long double)A/pow(xPre, N-1)) / (long double)N;
delX = abs(xK - xPre);
xPre = xK;
}
return xK;
}
void getFrequency(string strInput,map<char,int> & fMap)
{
map<char,int>::iterator it; //iterator to find the entries in map
for(int i = 0 ; i < strInput.length() ; i++ )
{
char ch = strInput.at(i);
it = fMap.find(ch); //find the character in the map
if( it == fMap.end() ) //if not present in the map
{
fMap.insert( make_pair(ch,1) );//add an entry
}
else
{
it->second++; //else increment the frequency
}
}
}
void printFrequency(map<char,int> & fMap)
{
map<char,int>::iterator it;//iterator to loop through all the chars
for( it = fMap.begin() ; it != fMap.end() ; ++it )
{
cout<<"["<< it->first<<"]"<<"->"<<it->second<<endl;
}
}