Skip to content

Instantly share code, notes, and snippets.

/*Find the row with maximum number of 1s
Given a boolean 2D array, where each row is sorted. Find the row with the maximum number of 1s.
Example
Input matrix
0 1 1 1
0 0 1 1
1 1 1 1 // this row has maximum 1s
0 0 0 0
@ajaynitt
ajaynitt / Palindrome.cpp
Created September 30, 2014 13:06
if an integer is Palindrome or not
/*Check if a number is Palindrome
*/
#include <iostream>
#include<cstring>
using namespace std;
#include<cmath>
bool isPalindrome(int n)
{
int ndigit=0;
int num=n;
@ajaynitt
ajaynitt / ArrayRotate.txt
Created October 8, 2014 19:10
Array Rotation :Write a function rotate(ar[], d, n) that rotates arr[] of size n by d elements.(left)
Input arr[] = [1, 2, 3, 4, 5, 6, 7],
d = 2, n =7
Ans:arr[] = [3, 4, 5, 6, 7, 1, 2]
solution: (Ar Br)r
steps:
first reverse d elements
then reverse remaining (n-d) elements
now reverse entire elements.
Example:
@ajaynitt
ajaynitt / ms_quest.txt
Created October 9, 2014 05:01
Past Microsoft Interview Question
HOW TO ROTATE A MATRIX 90 DEGREES WITHOUT USING ANY EXTRA SPACE?
Ans:
O(n^2) time and O(1) space algorithm ( without any workarounds and hanky-panky stuff! )
Rotate by +90:
Transpose
Reverse each row
@ajaynitt
ajaynitt / strtok.cpp
Created October 11, 2014 15:28
strtok example
/* strtok example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
Mutex:
a mutex is locking mechanism used to synchronize access to a resource. Only one task (can be a thread or process based on OS abstraction) can acquire the mutex. It means there will be ownership associated with mutex, and only the owner can release the lock (mutex).
Is a key to a toilet. One person can have the key - occupy the toilet - at the time. When finished, the person gives (frees) the key to the next person in the queue
Semaphore:
Semaphores are typically used as a signaling mechanism between processes.
A typical example is a producer-consumer process pair (not explained here to keep the answer to a readable length).
@ajaynitt
ajaynitt / Assignment Operator.cpp
Created October 13, 2014 03:13
Assignment operator for a class having pointers of another class
/*
class widget
{
private:
Bitmap *pb1;//pointer to heap allocated object
Bitmap *pb2;
};
*/
@ajaynitt
ajaynitt / copyConstructor.cpp
Last active August 29, 2015 14:07
Copy All parts of an Object
//Item 12 from effective c++
//helper function to make a log entry
void logCall(const std::string & name);//make a log entry
//base class
class Customer
{
public:
(1)DECLTYPE:
In the C++ programming language, decltype is a keyword used to query the type of an expression
int i = 33;
decltype(i) j = i*2;
OR
decltype(f) f2 = f; // the type of a lambda function is unique and unnamed
(2)lexical_cast in boost
we can implement like this to server the purpose
@ajaynitt
ajaynitt / sort012.cpp
Last active August 29, 2015 14:07
Sort an array of 0s, 1s and 2s
/*
Given an array A[] consisting 0s, 1s and 2s, write a function that sorts A[]. The functions should put all 0s first, then all 1s and all 2s in last.
Example
Input = {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1};
Output = {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2}
soln:Dutch National Flag Algorithm, or 3-way Partitioning —
*/
void sort012(int a[], int arr_size)