This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Refactoring for Software Design Smells: Managing Technical Debt this week in the OO, Patterns, UML and Refactoring forum!

# How to solve

shabbir zakir
Ranch Hand

Joined: Nov 12, 2000
Posts: 183
If size = 4, triArray looks like:

int[][] makeArray( int size)
{ int[][] triArray = new int[size] [];
int val=1;
for( int i = 0; i < triArray.length; i++ )
{ triArray[i] = new int[i+1];
for( int j=0; j < triArray[i].length; j++ )
{ triArray[i][j] = val++;
}
}
return triArray;
}
a)
1 2 3 4
5 6 7
8 9
10
b)
1 4 9 16
c)
1 2 3 4
d)
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
e)
1
2 3
4 5 6
7 8 9 10
Is there any easy way to solve this type of sums
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Best way to figure it out is just to walk through it.
You have a nested loop.
In the outer loop you go through creating an index starting at 0 and looping until you have made 4 loops. Each time through you are creating an int array in triArray that holds as many as the number of times that you have been in the loop. (when i=0 the first time, you make an array with 0+1, next time you make an array of 1+1 ETC.)
Inside each of the outer loops is another loop that only goes for as long as the length that you made that level of triArray. So the first time through, you made an array of length 1, so you fill it with the value of val which is 1. The second time through you have a length of 2 which you fill with 2 and then 3, the thrid time you have a length of 3 which you fill with 4 then 5 then 6 etc.

"JavaRanch, where the deer and the Certified play" - David O'Meara
Subramaniam Venkatesan
Ranch Hand

Joined: Feb 24, 2001
Posts: 48
Yes there are lot of ways to solve these kind of problems..
you should first check whether any choices can be dropped.. you can apply it to any problem.
1.) figure out the problem
..ie. make an algorithm
2.) check out the choices.. (if this works you're lucky )
3.) logical reasoning.. mathematics (find out the realtion existing)
there is always some relations that exist in loops, but you cant waste all you time finding it.. find it quick, if you can't and if the given choices that looks right are atleast 2 then walk through as said by
Cindy Glass:
Best way to figure it out is just to walk through it.

this problem..
algorithm:
size=2;
val=1;
loop 11 -- 0 to 3
loop 21 -- 0 to 3
array[11][21] --> val++;

obviously its a 4 by 4 matrix .. checkout the choices.. there's only one choice.. so just pick it
(choice d) )up
val--> 1 till (4*4=)16 you should have 16 values, checkout whices have 16 values and drop the other choices.
but it will never be this easy always, there will be a tougher
conditional statement and values will change in some order.. so find the relation and you will get it.

**assignment**
find the missing terms..

a.) 3, 5, 18, 95, 1728, ???

b.) 7, 13, 56, 182, 3192, 33306, ???

cool isnt,

[This message has been edited by Subramaniam Venkatesan (edited March 01, 2001).]
Subramaniam Venkatesan
Ranch Hand

Joined: Feb 24, 2001
Posts: 48
a.) 3, 5, 18, 95, 1728,

3, 5, 18=(3*(5+1)), 95=(5*(18+1)),1728=(18+(95+1)) and hence you get 164225.

<b.) 7, 13, 56, 182, 3192, 33306, >

the alternate are in series..
7, 56=(7*(7+1)), 3192=(56*(56+1))..
the other alternate terms have the same relation too..
13, 182=(13*(13+1)), 33306=(182*(182+1))
hence from the first alt. series, missing term will be (3192*3193)
good, I finished the assignment!

[This message has been edited by Subramaniam Venkatesan (edited March 02, 2001).]

I agree. Here's the link: http://aspose.com/file-tools

subject: How to solve