Tuesday, January 28, 2014

JavaScript 1





var aax_size='300x250';

var aax_pubname = 'studio444-20';

var aax_channel = '1100';

var aax_passback = "";



Sunday, January 19, 2014

Magic array

Problem
Given an array of +ve as well as -ve numbers, find out whether it is possible or not to convert it to 0 by adding/subtracting operations on all the elements.

Examples
arr[]={1,2,3}
1+2-3 = 0
Hence ans = YES

arr[]={3,6,2}
3+6-2 != 0
3-6-2 !=0
-3-6-2 !=0
-3-6+2 !=0
-3+6-2 !=0
-3+6+2 !=0
3-6+2 !=0
3+6+2 !=0
Hence ans= NO

Input/Output format:
First line will contain the number of test-cases t.
First line of each test-case contains a non-negative number n, the number of elements in the array.
Next line contains n positive or negative numbers.

#include <iostream>
using namespace std;

int main()
{
int t,A[100],n;
cin>>t;
while(t--)
{
cin>>n;
for(int i=0;i<n;i++)
{
cin>>A[i];
}
int sum=0;
for(int i=0;i<n;i++)
{
sum+=A[i];
}
if(sum == 0)
{
cout<<"Possible"<<endl;
}
else if(n > 1 && sum % 2 == 0)
{
cout<<"Possible"<<endl;
}
else
{
cout<<"Not possible"<<endl;
}
}
return 0;
}


Input:
5
5
1 2 3 4 5
4
-4 3 2 1
1
1
1
2
1
0

Output:
Not possible
Possible
Not possible
Not possible
Possible

Saturday, January 18, 2014

Given a binary tree find out whether the left and right sub-trees are mirror images of each other

#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* createnode(int x)
{
struct node* new_node=(struct node*)malloc(sizeof(struct node*));
new_node->data=x;
new_node->left=NULL;
new_node->right=NULL;
return new_node;
}

int isMirror(struct node* node1, struct node* node2)
{
if(node1 == NULL && node2 != NULL)
{
return 0;
}
else if(node1 != NULL && node2 == NULL)
{
return 0;
}
else if(node1 == NULL && node2 == NULL)
{
return 1;
}
else if(node1->data != node2->data)
{
return 0;
}
return isMirror(node1->left, node2->right) && isMirror(node1->right, node2->left);
}


int height(struct node* root)
{
if(root==NULL)
{
return 0;
}
else
{
int lheight=1+height(root->left);
int rheight=1+height(root->right);
return lheight>rheight?lheight:rheight;
}
}

void levelorderprint(struct node* root,int level)
{
if(root && level==0)
{
printf("%d ",root->data);
}
else if(root)
{
levelorderprint(root->left,level-1);
levelorderprint(root->right,level-1);
}
}

void levelorder(struct node* root)
{
int h=height(root);
for(int i=0;i<h;i++)
{
levelorderprint(root,i);
printf("\n");
}
}

int main()
{
struct node* root1=createnode(1);
root1->left=createnode(2);
root1->right=createnode(2);
root1->left->right=createnode(4);
root1->left->left=createnode(3);
root1->right->right=createnode(3);
root1->right->left=createnode(4);
//root1->right->right->left=createnode(8);
printf("The input tree is:\n");
levelorder(root1);
int result = 0;
result = isMirror(root1, root1);
if(result == 1)
{
printf("The left and right subtrees are mirror images\n");
}
else
{
printf("The left and right subtrees are not mirror images\n");
}
return 0;
}

Sunday, January 5, 2014

Setting up Apache, PHP and MySQL on MAC

Useful links:
http://coolestguidesontheplanet.com/install-configure-apache-mysql-php-phpmyadmin-osx-10-8-mountain-lion/

http://coolestguidesontheplanet.com/how-to-install-php-mysql-apache-on-os-x-10-6/