DigitalFire
BAN USERimport java.util.HashMap;
class Anagram{
public static void main(String arg[]){
String s1= "laid";
String s2 = "dial";
int[] arr = new int[26];
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
for(int i=0;i<s1.length();i++){
arr[s1.indexOf(c1[i])]++;
}
for(int i=0;i<s2.length();i++){
arr[s2.indexOf(c1[i])]--;
}
for(int i=0;i<26;i++){
if(arr[i]!=0){
System.out.println("Not an Anagram "+i);
return;
}
}
System.out.println("Anagram");
}
}
We can use something like a min-heap. Where the node value is the number of connections handled by the server. Each server can maintain a list of clients it is serving.
In addition to this we can have a HashMap to store the <client,server> pair to retrieve the server.
#include<iostream>
#include<string.h>
using namespace std;
void reverse(char * start, char * end){
char ch;
while(start < end){
ch = *start;
*start++ = *end;
*end-- = ch;
}
}
int main(){
char c[] = "Career";
int length = strlen(c);
reverse(c,c+length-1);
cout<<c;
}
How do you use a Trie for this problem?
- DigitalFire April 26, 2013struct node* createMirror(struct node* root){
if(root==NULL){
return NULL;
}
struct node* temp = createMirror(root->left);
root->left = createMirror(root->right);
root->right = temp;
return root;
}
If you read value from an integer pointer you get the entire number .. How are you getting the individual digits out??
- DigitalFire April 22, 2013We can use Drustenfeld's Algorithm -- Time - O(n) and Space - O(1)
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<time.h>
using namespace std;
class Deck {
public:
int array[52];
Deck() {
for(int i=0;i<52;i++)
array[i] = i+1;
}
void shuffle();
void swp(int * begin, int * ebd);
void print();
};
void Deck::shuffle() {
srand(time(NULL));
for(int i = 51;i>=0;i--) {
int r = rand() % (i+1);
swp((int *)(array+i),(int *) (array+r));
}
}
void Deck::swp(int * num1,int * num2) {
int temp = *num1;
*num1 = *num2;
*num2 = temp;
return;
}
void Deck::print(){
for(int i=0;i<52;i++){
cout<<array[i]<<" ";
}
cout<<endl;
}
int main() {
Deck obj;
obj.shuffle();
obj.print();
return 0;
}
#include<iostream>
#include<stdlib.h>
using namespace std;
void swp(int * begin, int * end) {
int temp;
while(begin < end) {
temp = *begin;
*begin++ = *end;
*end-- = temp;
}
}
void rotate(int * array,int k, int length) {
cout<<length<<endl;
swp((int *)array, (int *) (array+length-1));
swp((int *)(array+(length-k)), (int *)(array+length-1));
}
int main() {
int array[5] = {12,455,76,23,45};
int k = 3;
rotate((int *)array, k, (int) sizeof(array)/sizeof(int));
for(int i=0;i<(int) sizeof(array)/sizeof(int);i++) {
cout<<array[i]<<" ";
}
cout<<endl;
return 0;
}
This is using the standard Tortoise & Hare approach.
private void appendLast(int n){
//Declare a slow and a fast pointer.
LinkListNode slow = head;
LinkListNode fast = head;
//If no number is passed.
if(n==0) return;
else{
//Take the fast pointer to nth node from the last.
while(n>0 && fast.next!=null){
fast = fast.next;
--n;
}
//If the number of nodes are less than the given number n.
if(fast.next==null && n!=0){
System.out.println("Cannot be done!.");
return;
}else{
//Move the fast and the slow pointer.
while(fast.next != null){
slow = slow.next;
fast = fast.next;
}
//Make the switch.
fast.next = head;
head = slow.next;
slow.next = null;
}
}
}
Is it not CreateTree(array, left, mid-1);
- DigitalFire February 20, 2013The program returns the current index when you push to stack. So say you create an new stack by calling createStack(data) which adds the data array[0]:"e1:-1" , when you add the second element you call push(prevIndex) and it will add array[1]:"e2:0" and it returns 1. So when you pop(currentIndex) you start from 1 and you get the index of the previous element in the stack it 0 and return that.
- DigitalFire December 13, 2012Sorry I meant index when I said address.
- DigitalFire December 13, 2012private Node LCA_Normal(Node root, int a, int b){
if(root==null){
return null;
}
if((root.data>a && root.data<b)||(root.data<a && root.data>b)) {
return root;
}
if(root.data>a && root.data>b) {
return LCA_Normal(root.left,a,b);
}
if(root.data<a && root.data<b) {
return LCA_Normal(root.right,a,b);
}
return root;
}
Complexity -- O(logn)
- DigitalFire November 12, 2012The depth should be initialized to 0 i guess.
int depth = 0;
Because even if there is a single root. Your marker will be read and it depth will be incremented.
- DigitalFire November 12, 2012Here I am just using the inorder traversal and blocking any values outside the range using a simple if condition.
r1 - Lower Bound
r2 - Upper Bound
int BST::range(node* root,int r1,int r2) {
if(root == NULL) {
return 0;
}
if(root->left) { range(root->left,r1,r2); }
if(root->data > r1 && root->data < r2) {
cout<<root->data<<" ";
}
if(root->right) { range(root->right,r1,r2); }
}
Max heap implementation --
- DigitalFire July 12, 2013