Monday 21 January 2013

TopCoder SRM 567 250 Problem


The Ninja Turtles often battle the Foot Clan ninjas. The Turtles celebrate each victory with a pizza party. The amount of pizza they eat depends on the number of opponents they have defeated. Denote the number of defeated opponents as N. Three of the four Turtles have a moderate appetite and only consume floor(N / K) pizzas each. The fourth Turtle is always hungry and eats floor(N / 3) pizzas.

You are given ints P and K, where P is the total number of pizzas the Turtles ate after a battle. If there exists at least one value of N such that after defeating N opponents the Turtles would eat exactly P pizzas at the party, return the smallest such N. Otherwise, return -1.

I have solved this in a simple way...check it out...:)

#include<iostream>
#include<cmath>
using namespace std;
class NinjaTurtles
{
public:
int countOpponents(int P , int K )
{
int x = 1;
int temp = 0 , temp1 = 0;
int y = (int)((3*K*P) / ( K + 3) );
cout<<y<<endl;
while( (x < K) || (x < 3))
{
            temp = (int)floor(x/K);
temp1 = (int)floor(x/3);
            temp = temp * 3;
            if((temp + temp1) == P)
{
return x;
}  
x++;
            }
while(x <= y)
{
            temp = (int)floor(x/K);
temp1 = (int)floor(x/3);

temp = temp * 3;
if((temp + temp1) == P)
{
return x;
}
x++;
}
return -1;
}
};
                                                                               BY:  SAURABH BHATIA



Sunday 13 January 2013

ekSMS.com and YourStory.in Hiring Challenge


Although this is giving a wrong answer according to the compiler of hackers earth but according to me it is appropriate solution for the problem. so if you find any bug in this plz do comment on it.

PROBLEM:
There are many ways to order a list of integers from 1 to n. For example, if n = 3, the list could be : [3 1 2].
But there is a special way to create another list from the given list of integers. In this list, position of integer i is the i-th number in the given list. So, the above list will be written as: [2 3 1]. This list is called inverse list. Now there exists some list whose inverse list is identical. For example, inverse list of [1 2 3] is same as original list. Give a list of integers you have to determine whether the list is inverse or not.
The input contains several test cases. The first line is the number of test cases t (1 <= t <= 100) . The first line of each test case contains an integer n (1 <= n <= 100000). Then a list of the integers 1 to n follows in the next line.
SOLUTION:   I have write this simple code for this problem.

#include<iostream>
#include<vector>
#include<string>
#include<stdio.h>
using namespace std;
class saurabh
{
      public:
             void sau(int t)
             {
                  long n = 0;
                  long tmp = 0;
                 
                  for(int l = 0 ; l < t ; l ++)
                  {
                  scanf("%ld",&n);
                  vector <long> num(n);
                    vector<long>cm(n);
                   tmp = 0;
                    for(long i = 0 ; i < num.size() ; i++)
                    {
                             scanf("%ld",&tmp);
                             num[i] = tmp;
                             cm[tmp-1] = i+1;
                    }
                    if(cm == num)
                    {
                          printf("inverse");
                    }
                    else
                    {
                        printf("not inverse");
                    }
                    num.clear();
                    cm.clear();
                  }
             }
             };
             int main()
             {
                 saurabh obj;
                 int t = 0;
                 scanf("%d",&t);
                 obj.sau(t);
                 cin.get();
                 return 0;
             }
                                                                    BY: SAURABH BHATIA            


Friday 11 January 2013

PERMUTATION & COMBINATION PROBLEM EASY SOLUTION

AS YOU HAVE SEEN MANY PROBLEMS IN VARIOUS CODING COMPETITION LIKE TOPCODER , CODECHEF , CODEFORCE FOR OUTPUT THE NUMBER OF OCCURRENCES OF THE VARIOUS ITEMS TOGETHER OR DIFFERENTLY. AS IN THIS CODE I HAVE TAKEN TWO ITEMS C AND D AND THIS PROGRAMME WILL OUTPUT THE COMBINATIONS OF THESE TWO IN 5 PLACES. THIS IS A SIMPLE CODE WRITTEN BY ME.




#include<iostream>
#include<string>
using namespace std;
int main()
{
    int numLength = 5;
    string tmp ;
    int x = 0;
    for(int k = 0 ; k <(1<<numLength); k++) // it forms the combinations
    {
   
             x = k;
            for(int i = 0 ; i <numLength ; i++)
            {
                   
                    if( (x & 1) == 1)
                    tmp+= 'c';
                    else
                    tmp+= 'd';
                    x>>= 1; // as it right shift the value so the combination occurs
                   
                    }
           cout<<k<<'\t'<<tmp<<endl;
           tmp = "";
           }
           cout<<tmp<<endl;
           x = 5;
           cout<<(x>>1)<<endl;
           cout<<(x&1)<<endl;
                       
                   
                         
    cin.get();
    return 0;
}

                                                                   BY : SAURABH BHATIA
           

TIC TAC TOE GAME IN ANDROID

I HAVE MADE A SIMPLE CODE FOR MAKING TIC TAC TOE GAME IN ANDROID .FIRST YOU HAVE TO DESIGNED BUTTONS BY USING ECLIPSE THAN HAVE TO DO CODING IN ANDROID.


package saurabh.rox.namespace;   // AS MY NAME OF THE PACKAGE IS SAURABH

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class GameActivity extends Activity {
/** Called when the activity is first created. */
int count = 1;
int flag = 0;
Button[] A = new Button[9];
final Context context = this;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setTitle("TicTacToe");
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("TicTacToe");
alertDialog
.setMessage("WELCOME TO THE 'TicTacToe' GAME \nMADE BY SAURABH OF (CSE 2nd YEAR)");
alertDialog.setCancelable(false);
alertDialog.setPositiveButton("Play",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
alertDialog.setNegativeButton("Exit",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
GameActivity.this.finish();

}
});
AlertDialog alertDia = alertDialog.create();
alertDia.show();

A[0] = (Button) findViewById(R.id.button1);
A[1] = (Button) findViewById(R.id.button2);
A[2] = (Button) findViewById(R.id.button3);
A[3] = (Button) findViewById(R.id.button4);
A[4] = (Button) findViewById(R.id.button5);
A[5] = (Button) findViewById(R.id.button6);
A[6] = (Button) findViewById(R.id.button7);
A[7] = (Button) findViewById(R.id.button8);
A[8] = (Button) findViewById(R.id.button9);

A[0].setText("");
A[1].setText("");
A[2].setText("");
A[3].setText("");
A[4].setText("");
A[5].setText("");
A[6].setText("");
A[7].setText("");
A[8].setText("");

}

public void set() {
A[0].setText("");
A[1].setText("");
A[2].setText("");
A[3].setText("");
A[4].setText("");
A[5].setText("");
A[6].setText("");
A[7].setText("");
A[8].setText("");
count = 1;
A[0].setClickable(true);
A[1].setClickable(true);
A[2].setClickable(true);
A[3].setClickable(true);
A[4].setClickable(true);
A[5].setClickable(true);
A[6].setClickable(true);
A[7].setClickable(true);
A[8].setClickable(true);

}

// FOR DIALOG BOX

public void dialog() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
alertDialogBuilder.setTitle("TicTacToe");
if (count >= 10) {
alertDialogBuilder
.setMessage("THERZ A TIE BETWEEN THE PLAYERS \nCLICK 'Yes' TO PLAY AGAIN AND 'No' TO EXIT.");
} else if (count % 2 == 0) {

alertDialogBuilder
.setMessage("O HAS WON THE GAME \nCLICK 'Yes' TO PLAY AGAIN AND 'No' TO EXIT.");
} else {
alertDialogBuilder
.setMessage("X HAS WON THE GAME \nCLICK 'Yes' TO PLAY AGAIN AND 'No' TO EXIT.");
}
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
Toast.makeText(getApplicationContext(),
"New Game Has Been Started \nAll The Best.",
Toast.LENGTH_SHORT).show();

}
});
alertDialogBuilder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
Toast.makeText(getApplicationContext(),
"Thanks For Playing The Game",
Toast.LENGTH_SHORT).show();
GameActivity.this.finish();

}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
// this is just for showing info for a second Toast.makeText(this,
// "This is the Toast message", Toast.LENGTH_LONG).show();

}

// FOR CHECKING

public void check() {
flag = 0;
if (count >= 10) {
dialog();
set();

}
for (int i = 0; i < 3; i++) {
if (A[i].getText() != "") {
if (A[i].getText().equals(A[i + 3].getText())
&& A[i].getText().equals(A[i + 6].getText())) {
if (count % 2 == 0) {
dialog();
set();
} else {
dialog();
set();
}
}
}
}
for (int j = 0; j < 7; j = j + 3) {
if (A[j].getText() != "") {
if (A[j].getText().equals(A[j + 1].getText())
&& A[j].getText().equals(A[j + 2].getText())) {
if (count % 2 == 0) {
dialog();
set();
} else {
dialog();
set();
}

}
}
}
if (A[0].getText() != "") {
if (A[0].getText().equals(A[4].getText())
&& A[0].getText().equals(A[8].getText())) {
if (count % 2 == 0) {
dialog();
set();
} else {
dialog();
set();
}

}
}
if (A[2].getText() != "") {
if (A[2].getText().equals(A[4].getText())
&& A[2].getText().equals(A[6].getText())) {
if (count % 2 == 0) {
dialog();
set();
} else {
dialog();
set();
}

}

}

}

public void onMyButtonClick(View view) {

if (count % 2 == 0) {
((Button) view).setText("X");
((Button) view).setClickable(false);

} else {
((Button) view).setText("O");
((Button) view).setClickable(false);
}
count++;
check();

}

}

                                                      BY : SAURABH BHATIA