AnanthaNag KUNDANALA
BAN USER
java, ADF programmer with 4+ years of experience.
take all the elements in an array elements[ rows ][ columns ] ( elements [ 9 ][ 9 ]; )
//keep in mind that 1+2+3+4+5+6+7+8+9=45;
Row Validation :
1. consider first row.
2. check whether all the elements are above 0. if not return and print ' not valid'.
3. add all the elements and find the sum.
4. if sum is 45, then go to next row and iterate the above three steps until the 9th row.
5. if sum is not 45, then return and print ' not valid'.
// Do the same validation for all the 9 columns.
// Do the same validation for all the 9 sub grids.
If above 3 validations are passed, print that the puzzle is valid.
String sortedNumbers = "123456789 9876543210";
for (int i = 31; i <= 3000; i++) {
String temp = "" + i;
if (!sortedNumbers.contains(temp)) {
System.out.println(temp);
}
}
declare the given matrix as static.
boolean isAdjacentToDesert( int x, int y){
if(element(x,y) is adjacent to desert){
return true;
}
else
return false;
}
public String updateDesert(){
for( iterate through rows){
for ( iterate through coloumns){
check if the element ( rowNumber, coloumnNumber) is desert or not.
if ( ! desert)
{
isAdjacentToDesert(rowNumber, coloumnNumber);
if(adjacent)
change the status of the element to '2'.
}
}} // end of for loops.
Now change the status '2' to deserts ( '1' ) .
}
public class ReverseOfAString {
public String reverse(String str) {
char[] string = str.toCharArray();
int len = string.length - 1;
int middleElement = len / 2;
for (int i = 0; i <= middleElement; i++) {
char temp = string[i];
string[i] = string[len - i];
string[len - i] = temp;
}
return new String(string);
}
public static void main(String argc[]) {
ReverseOfAString r = new ReverseOfAString();
String rev = r.reverse("INDIA");
System.out.println(rev);
}
}
import java.util.HashSet;
import java.util.Set;
public class Employee {
int id;
String firstName;
String lastName;
Employee manager;
Set subordinates = new HashSet<Employee>();
Employee(int id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
void addManager(Employee manager) {
this.manager = manager;
manager.subordinates.add(this);
}
void addSubordinate(Employee subordinate) {
this.subordinates.add(subordinate);
subordinate.manager = this;
}
void findDetails() {
if (this.manager != null) {
System.out.println("Manager is : " + this.manager);
} else {
System.out.println("NO manager for " + this);
}
if (this.subordinates.size() > 0) {
System.out.println("***SUB ORDINATES***");
for (Object subordinate : this.subordinates) {
Employee emp = (Employee) subordinate;
System.out.println(emp);
}
} else {
System.out.println("No subordinates for : " + this);
}
}
@Override
public String toString() {
return this.id + " : " + this.firstName + "." + this.lastName;
}
public static void main(String argc[]) {
Employee a = new Employee(1, "a", "aa");
Employee b = new Employee(2, "b", "bb");
Employee c = new Employee(3, "c", "cc");
Employee d = new Employee(4, "d", "dd");
Employee e = new Employee(5, "e", "ee");
Employee f = new Employee(6, "f", "ff");
Employee g = new Employee(7, "g", "gg");
a.addSubordinate(b);
a.addSubordinate(c);
b.addSubordinate(e);
b.addSubordinate(f);
c.addSubordinate(g);
a.findDetails();
}
}
Hey exactely ... Its my mistake only ... Till now , I am guessing that 2 below row glasses will get the juice from one glass only rather than 2 glasses. Now will modify and revert back.Hey thanks a ton again. please revert me at kundanala@gmail.com.
- AnanthaNag KUNDANALA December 29, 2011@ vijay : yeah vijay. with pleasure . You can reach me at kundanala@gmail.com
- AnanthaNag KUNDANALA December 29, 2011If you see the question properly , they stated very clearly that ' the extra liquid would be flown into the glasses 2 and 3 in equal quantities. ' But if you see your output , that condition was violated. I am not getting you why you said that my output was wrong. Can you tell me why ?
- AnanthaNag KUNDANALA December 29, 2011I got the below output for the input values ( 50, 5, 4)
In glass-1 : 5.0 litres
In glass-2 : 5.0 litres
In glass-4 : 5.0 litres
In glass-8 : 1.875 litres
In glass-9 : 1.875 litres
In glass-5 : 5.0 litres
In glass-10 : 1.875 litres
AnanthNag KUNDANALA
In glass-11 : 1.875 litres
In glass-3 : 5.0 litres
In glass-6 : 5.0 litres
In glass-12 : 1.875 litres
In glass-13 : 1.875 litres
In glass-7 : 5.0 litres
In glass-14 : 1.875 litres
In glass-15 : 1.875 litres
Following code will let to take to the goal ....
class GlassesStatus {
double liquidInLiters, capacityOfEachGlass;
int numberOfLevelsOfGlasses;
int level = 0;
GlassesStatus(double liquid, double capacity, int levels) {
this.liquidInLiters = liquid;
this.capacityOfEachGlass = capacity;
this.numberOfLevelsOfGlasses = levels;
}
void describeGlassesStatus() {
System.out.println("In Method ...");
int glassNumber = 1;
int level = 1;
describe(glassNumber, liquidInLiters);
}
private void describe(int glassNumber, double liquidInLiters) {
if (level <= numberOfLevelsOfGlasses) {
if (liquidInLiters < 0) {
System.out.println("No Liquid in " + glassNumber);
} else {
if (liquidInLiters <= capacityOfEachGlass) {
System.out.println("In glass-" + glassNumber + " : " + liquidInLiters + " litres");
} else {
System.out.println("In glass-" + glassNumber + " : " + capacityOfEachGlass + " litres");
liquidInLiters = liquidInLiters - capacityOfEachGlass;
describe((glassNumber * 2), liquidInLiters / 2);
describe((glassNumber * 2) + 1, liquidInLiters / 2);
level++;
}
}
}
}
}
AnanthaNag KUNDANALA
- AnanthaNag KUNDANALA December 28, 2011And may I have you in my mail contacts list? If you have no issues to be in my contacts, please revert me at kundanala@gmail.com
- AnanthaNag KUNDANALA December 27, 2011yup. thanks a ton for enlightenment. Till now, I never thought of overflow of additions & other mathematical operations with respect to the range of the primitive datatypes. Thanks once again.
- AnanthaNag KUNDANALA December 27, 2011got you. So, I guess replacing 'int' with some other bigger primitive datatype (like long), I will be in safe side. Is n't it ?
- AnanthaNag KUNDANALA December 27, 2011Overflow ???
will you please give me an example ?
boolean flag = true;
boolean isSumTree(Node rootNode) {
if (rootNode != null) {
int sum = 0;
if (rootNode.leftNode != null) {
sum = rootNode.leftNode.value;
}
if (rootNode.rightNode != null) {
sum = sum + rootNode.rightNode.value;
}
if (rootNode.value == sum) {
if ((rootNode.leftNode.leftNode != null) && (rootNode.leftNode.rightNode != null)) {
isSumTree(rootNode.leftNode);
}
if ((rootNode.rightNode.leftNode != null) && (rootNode.rightNode.rightNode != null)) {
isSumTree(rootNode.rightNode);
}
} else {
flag = false;
return false;
}
}
return flag;
}
AnanthaNag KUNDANALA
- AnanthaNag KUNDANALA December 27, 2011Below is the full length class implementation.
public class Main {
public static void main(String argc[]) {
int n = 1234567891;
int big = findBiggest(n);
System.out.println("big is : " + big);
}
private static int findBiggest(int n) {
int big = 0;
String temp = "" + n;
for (int i = 0; i <= (temp.length() - 4); i++) {
int buf = Integer.parseInt(temp.substring(i, (i + 4)));
if (big <= buf) {
big = buf;
}
}
return big;
}
}
AnanthaNag KUNDANALA
- AnanthaNag KUNDANALA December 27, 2011private static int findBiggest(int n) {
int big = 0;
String temp = "" + n;
for (int i = 0; i <= (temp.length() - 4); i++) {
int buf = Integer.parseInt(temp.substring(i, (i + 4)));
if (big <= buf) {
big = buf;
}
}
return big;
}
Following is the full class implementation ...
public class Main {
public static void main(String argc[]) {
int matrix[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12},{13,14,15,16}};
int[][] newMatrix = rotateMatrix(matrix);
display(newMatrix);
}
private static int[][] rotateMatrix(int[][] matrix) {
int[][] rotatedMatrix = new int[matrix.length][matrix.length];
int len = matrix.length - 1;
for (int i = 0; i <= len; i++) {
for (int j = 0; j <= len; j++) {
rotatedMatrix[j][len - i] = matrix[i][j];
}
}
return rotatedMatrix;
}
private static void display(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println("");
}
}
}
AnanthaNag KUNDANALA
- AnanthaNag KUNDANALA December 27, 2011Following method will give the required rotational functionality.
private static int[][] rotateMatrix(int[][] matrix) {
int[][] rotatedMatrix = new int[matrix.length][matrix.length];
int len = matrix.length - 1;
for (int i = 0; i <= len; i++) {
for (int j = 0; j <= len; j++) {
rotatedMatrix[j][len - i] = matrix[i][j];
}
}
return rotatedMatrix;
}
AnanthaNag KUNDANALA
- AnanthaNag KUNDANALA December 27, 2011The following 'sum' method will return the requirement .
private static int sum(LinkedList l1, LinkedList l2) {
int sum = 0;
int decimal = 1;
int max = 0, min = 0;
if (l1.size() > l2.size()) {
max = l1.size() - 1;
min = l2.size() - 1;
} else {
max = l2.size() - 1;
min = l1.size() - 1;
}
for (int i = max; i >= 0; i--) {
if ((l1.size() - 1) == max) {
sum = sum + (decimal * (Integer) (l1.get(i)));
} else {
if (min >= 0) {
sum = sum + (decimal * (Integer) (l1.get(min)));
min--;
}
}
if ((l2.size() - 1) == max) {
sum = sum + (decimal * (Integer) (l2.get(i)));
} else {
if (min >= 0) {
sum = sum + (decimal * (Integer) (l2.get(min)));
min--;
}
}
decimal = decimal * 10;
}
return sum;
}
AnanthaNag KUNDANALA
- AnanthaNag KUNDANALA December 27, 2011
Thank you all ...
- AnanthaNag KUNDANALA October 02, 2013