Skip to content

Instantly share code, notes, and snippets.

View cruxrebels's full-sized avatar

Abhishek Agrawal cruxrebels

View GitHub Profile
@cruxrebels
cruxrebels / .bash_profile
Created July 14, 2017 10:08 — forked from natelandau/.bash_profile
Mac OSX Bash Profile
# ---------------------------------------------------------------------------
#
# Description: This file holds all my BASH configurations and aliases
#
# Sections:
# 1. Environment Configuration
# 2. Make Terminal Better (remapping defaults and adding functionality)
# 3. File and Folder Management
# 4. Searching
# 5. Process Management
@cruxrebels
cruxrebels / SearchForARange.cpp
Created May 4, 2016 21:10
Binary search for searching a range.
/*
Given a sorted array of integers, find the starting and ending position of a given target value.
Your algorithm’s runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1].
Example:
Given [5, 7, 7, 8, 8, 10]
@cruxrebels
cruxrebels / MatrixSearch.cpp
Created May 3, 2016 18:50
Binary search technique implementation.
/*
Write an efficient algorithm that searches for a value in an m x n matrix.
This matrix has the following properties:
Integers in each row are sorted from left to right.
The first integer of each row is greater than or equal to the last integer of the previous row.
Example:
Consider the following matrix:
[
[1, 3, 5, 7],
[10, 11, 16, 20],
@cruxrebels
cruxrebels / RearrangeArray.cpp
Created April 30, 2016 15:06
Math + Array problem
/*Rearrange a given array so that Arr[i] becomes Arr[Arr[i]] with O(1) extra space.
Example:
Input : [1, 0]
Return : [0, 1]
Lets say N = size of the array. Then, following holds true :
* All elements in the array are in the range [0, N-1]
* N * N does not overflow for a signed integer
https://www.interviewbit.com/problems/rearrange-array/ */
void Solution::arrange(vector<int> &A) {
auto n = A.size();
@cruxrebels
cruxrebels / ExcelColumnNumber.cpp
Created April 28, 2016 20:57
Given a column title as appears in an Excel sheet, return its corresponding column number. Example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 Tags: InterviewBit Math Problems https://www.interviewbit.com/problems/excel-column-number/
int Solution::titleToNumber(string A) {
auto n = A.length();
int value = 0;
for (auto i=0; i<n; ++i)
{
value += pow(26, i)*(A[n-(i+1)] - 'A' + 1);
}
return value;
}
@cruxrebels
cruxrebels / PowerOfTwoIntegers.cpp
Created April 27, 2016 19:12
Given a positive integer which fits in a 32 bit signed integer, find if it can be expressed as A^P where P > 1 and A > 0. A and P both should be integers. Example Input : 4 Output : True as 2^2 = 4. Tags: InterviewBit Math Problem https://www.interviewbit.com/problems/power-of-two-integers/
bool Solution::isPower(int A) {
if (A<2)
return true;
for (auto i = 2; i<=sqrt(A); ++i)
{
for (auto j = 2; j<=32; ++j)
{
if(pow(i, j)==A)
return true;
@cruxrebels
cruxrebels / PrimeSum.cpp
Created April 26, 2016 18:48
Given an even number ( greater than 2 ), return two prime numbers whose sum will be equal to given number. NOTE A solution will always exist. read Goldbach’s conjecture Example: Input : 4 Output: 2 + 2 = 4 If there are more than one solutions possible, return the lexicographically smaller solution. If [a, b] is one solution with a <= b, and [c,d…
bool isPrime(int n)
{
for(int i=2; i*i<=n; ++i)
{
if(n%i==0)
return false;
}
return true;
}
@cruxrebels
cruxrebels / GreatestCommonDivisor.cpp
Created April 26, 2016 18:44
Given 2 non negative integers m and n, find gcd(m, n) GCD of 2 integers m and n is defined as the greatest integer g such that g is a divisor of both m and n. Both m and n fit in a 32 bit signed integer. Example m : 6 n : 9 GCD(m, n) : 3 NOTE : DO NOT USE LIBRARY FUNCTIONS Tags: InterviewBit Math Problems https://www.interviewbit.com/problems/gr…
int Solution::gcd(int A, int B) {
//Euclid's algorithm
while(B!=0)
{
int r = A%B;
A = B;
B = r;
}
return A;
}
@cruxrebels
cruxrebels / BinaryRepresentation.cpp
Created April 26, 2016 15:54
Given a number N >= 0, find its representation in binary. Example: if N = 6, binary form = 110 Tags: InterviewBit Math Problems https://www.interviewbit.com/problems/binary-representation/
string Solution::findDigitsInBinary(int A) {
int r;
string result;
if(A==0)
return "0"; //prev used result += to_string(0); return result;
while(A>0)
{
r = A%2;
@cruxrebels
cruxrebels / VerifyPrime.cpp
Created April 26, 2016 15:51
Given a number N, verify if N is prime or not. Return 1 if N is prime, else return 0. Example : Input : 7 Output : True Tags: InterviewBit Math Problems https://www.interviewbit.com/problems/verify-prime/
int Solution::isPrime(int A) {
if (A<2)
return 0;
for(auto i=2; i<=sqrt(A); ++i)
{
if(A%i==0)
return 0;
}
return 1;
}