Posted by: Dilbar June 27, 2010
Could anyone please help me with these two problems.
Login in to Rate this Post:     0       ?        
Can any body help me in solving these two questions. Would really appreciate the help thanks...


Question1.




Every element in an array of integers points to a relative location from the current element. Precisely, if A[k] = m, the jump from k should land at k+A[k]=k+m.


Write a function



int arrayJmp(int[] A);



that returns the number of jumps until the pointer jumps out of the array when starting from the firstelement.


For example:





A[0]=2, A[1]=3, A[2]=1, A[3]=1, A[4]=3



The pointer's 1st jump is from 0 to 2, 2nd jump from 2 to 3, 3rd jump from 3 to 4, 4th jump from 4 to 7, but 7 is out of the array. The number of jumps until the pointer jumps out of the array is 4.


Return -1 if the sequence of jumps never ends.





====================================================================



Question 2:




Write a function



int nesting(string S);



which given a string S made of characters ( and ) returns 1if S is properly nested, 0 otherwise.


In precise terms, a string is propetly nested if it has one of the following forms:



  • empty,

  • (S), where S is properly nested,

  • ST, where both S and T are properly nested.

For example, given the string





(()(())())



your function should return 1, and for the string





())



your function should return 0.




Read Full Discussion Thread for this article