## Abhijeet Rokade

BAN USERStudent At NITW

suppose there are N user, MatrixM of N*N represet the relationship of user(M[i][j] is 1 means user i & user j are friend, M[i][j] is 0 means they are not direct friend but they may be mutual friend) hence to find the degree of connection of user, we have to find the shortest path from userI to userJ using shortest path alorithm

- Abhijeet Rokade August 09, 2012suppose there are N user, MatrixM of N*N represet the relationship of user(M[i][j] is 1 means user i & user j are friend, M[i][j] is 0 means they are not direct friend but they may be mutual friend) hence to find the degree of connection of user, we have to find the shortest path from userI to userJ using shortest path alorithm

- Abhijeet Rokade August 09, 2012#include<iostream>

using namespace std;

#define N 8

int main(){

int A[]= {-2,-3,-5,40,-10,-10,100,1};

int minI, maxI, max, i, j, temp;

minI= maxI= 0;

max= A[0];

for(i= 0; i< N; i++){

temp= 0;

for(j= i; j< N; j++ ){

temp+= A[j];

if(temp> max){

minI= i;

maxI= j;

max= temp;

}

}

}

cout<<max<<endl;

cout<<"minI "<<minI<<" maxI "<<maxI<<endl;

for(i= minI; i<= maxI; i++)

cout<<A[i]<<" ";

return 0;

}

We Use Trie Or Pre-Tree Data structure for Dictionary

- Abhijeet Rokade August 07, 2012struct data{

char ch[100]; // this will store 200 Digits

struct data *next;

};

if digit in 100 Multiple

overhead of memory= (200+ 2*4)/ 200= 1.04%

int A[]={0, 1, 5, 2, 7, 9};

void incrementArray(int A[])

{

int carry = 1, i;

for(i = sizeof(A)/4-1; i>0 && carry; i--) // integer of 4Byte

{

A[i]+=carry;

carry= A[i]/10;

A[i]= A[i]%10;

}

if(carry)

A[0]=1;

return ;

}

In C We can not reallocate char aaa[] & bbb[];

we Can reallocate only char pointer

Given:

int Read4096(char *buf);

Solution:

We Assume that Main Program Has char buf[n] to store Read data

int Read(char *buf, int n){

static char temp[4096], *ptr= temp-1;

int r, l=0;

if(ptr>= temp){

if(n> 4096- ptr){

strcat(buf, ptr);

n-= 4096- ptr;

l+= 4096- ptr;

ptr= temp;

}else{

strncat(buf, ptr, n);

ptr+= n;

l= n;

n= 0;

return l;

}

}

while(n>= 4096){

r= Read4096(temp);

strncat(buf, temp, r);

l+= r;

n= n-r;

}

if(n){

r= Read4096(temp);

strncat(buf, temp, n);

l+= n;

ptr= temp+ n;

}

return l;

}

for INORDER

#include<stdio.h>

#include<malloc.h>

typedef struct node{

int data;

struct node *l;

struct node *r;

}node;

node *getnode(int d){

node *temp= (node *)malloc(sizeof(node));

temp->data= d;

temp->l= NULL;

temp->r= NULL;

return temp;

}

node *BST(int A[], int min, int max){

int mid= (min+ max)/ 2;

node *temp;

if(max== min)

return getnode(A[min]);

else {

temp= getnode(A[mid]);

if(mid!= min)

temp->l= BST(A, min, mid-1);

temp->r= BST(A, mid+1, max);

return temp;

}

}

void Inorder(node *root){

if(root){

Inorder(root->l);

printf("%d--", root->data);

Inorder(root->r);

}

}

int main(){

int A[12]= {1, 5, 7, 8 ,9 ,12,13, 19, 25, 26, 27, 31};

node *root= BST(A, 0, 11);

Inorder(root);

return 0;

}

int Rand_Num_Equal_Pro(const int n){

static int Flag[n];

int r, temp;

time_t t;

struct tm *t;

t= localtime(&t);

r= t->tm_sec+ t->tm_min*10+ t->tm_hour*100;

srand(r);

r= rand();

r= r%n;

temp= r+1;

if(Flag[r]){

while(Flag[temp]&& (temp%n)!= r) temp++;

if((temp%n)== r){

for(int i= 0; i<n; i++)

Flag[i]= 0;

return r;

}

else{

Flag[temp%n]= 1;

return temp%n;

}

}

else{

Flag[r]= 1;

return r;

}

}

Nice

- Abhijeet Rokade August 07, 2012Time Complexity: O(log n)

int First_Instance(int A[], int min, int max, int k){

int mid = (min+ max)/2;

int index, temp;

if(min<=max){

if(k< A[mid])

index= First_Instance(A, min, mid- 1, k);

else if(k> A[mid])

index= First_Instance(A, min, mid- 1, k);

else

while(A[mid]!= k){

temp= mid;

mid++;

}

return temp;

}

return -1;

}

Initialise a And b to Zero In Main Program

Com_Anc(struct Node *root, struct Node *p, struct Node *q, int &a, int &b){

struct Node *temp ;

if(root){

if(! (*a) && root == p)

*a = 1;

if(! (*b) && root == q)

*b = 1;

if(!(a&&b)){

if(root->l)

temp = Com_Anc(root->l, p, q, a, b);

if(root->r && !(a&&b))

temp = Com_Anc(root->r, p, q, a, b);

}

if(a&&b) return root;

else return temp;

}

return NULL;

}

**CareerCup**is the world's biggest and best source for software engineering interview preparation. See all our resources.

#include<iostream>

- Abhijeet Rokade August 09, 2012using namespace std;

#define N 8

int main(){

int A[]= {2, 3, 5, 40, 10, 16, 100, 9};

int i;

long long int pro=1;

for(i= 0; i< N; i++){

pro*= A[i];

}

for(i= 0; i<= N; i++)

cout<<pro/A[i]<<" ";

return 0;

}