Should I be using 2D arrays for this problem (or) how do I go about solving

An array is being used to represent an organizational hierarchy. This is done by using the array subscript to map to the employee-id and the value in each element to represent the employee-id of the supervisor of the employee.
For example the first element in the array maps to the supervisor of employee-id 1, the second element maps to the supervisor of employee id 2. The corresponding value for the top person in the organization is set to -1.
For example the following array ?
7 5 7 10 -1 7 5 2 2 5
translates to the following hierarchical structure -
? employee 5 is the top boss
? employees 2, 7, and 10 report to employee 5
? employees 8 and 9 report to employee 2
? employees 1, 3 and 6 report to employee 7
? employee 4 reports to employee 10
The following diagram represents the organization structure
5
+ --- 2
| + --- 8
| + --- 9
|
+ --- 7
| + --- 1
| + --- 3
| + --- 6
|
+ --- 10
+ --- 4
5 is at level 1 in the organization.
2, 7 and 10 are at level 2
1, 3, 4, 6, 8, 9 are at level 3
Given an array of size N (the employee-ids being 1 to N), write a program to find the number of people in level L, where the top person in the organization is at level 1.

Database is overkill!
heres your assignment
package myjava.demo;
class DemoLevel{
     class LevelData{
        int members[];
        int size = 0;
        int level = 0;
        public String toString() { return "Level: " + level + " Size: " + size;}
    LevelData getNextLevel(int data[],LevelData current){
        LevelData nextlevel = new LevelData();
        nextlevel.level = current.level + 1;
        nextlevel.members = new int[data.length];
        for(int i = 0; i< data.length;i = i + 1){
            if(data[i] != -1 && current.members[data[i]-1] != 0){ //data[i] indicates i's parent. this ask if its a member of current
                nextlevel.members[i] = data[i]-1; //store its parent. not needed in this algo. but better then true and false
                nextlevel.size = nextlevel.size + 1;
            }else{ nextlevel.members[i] = 0;}
        return nextlevel;
    LevelData getFirstLevel(int data[]){
        LevelData firstlevel = new LevelData();
        firstlevel.level = 1;
        firstlevel.members = new int [data.length];
        for(int i = 0; i < data.length;i = i + 1){
            if(data[i] == -1){
                firstlevel.members[i] =   -1;
                firstlevel.size = firstlevel.size + 1;
            }else { firstlevel.members[i] = 0;}
        return firstlevel;
    void execute(){
        int data[] = { 7, 5, 7, 10, -1, 7, 5, 2, 2, 5};
        LevelData current = getFirstLevel(data);
        System.out.println(current);
        while(current.size != 0)
            current = getNextLevel(data,current);
            System.out.println(current);
public class Demo {
    public static void main(String[] args) {
        DemoLevel dl = new DemoLevel();
        dl.execute();

Similar Messages

  • When or should XML be used and why for this situation?

    I am developing a 3-tiered web application (database, server, browser) that will use JSP to display a form where the user will enter their name and address information, JDBC for the connection to a mySQL database.
    When the user enters their name and address information on the JSP displayed using the broswer, the data when submitted will be placed in the database.
    How could I use XML for this situation? Is it really needed at all since I know it can all be done using only JSP? I am just wondering how using XML could help?
    Please give me any feedback you can...if you need more detail to what I am trying to do, let me know....
    Thank you so much in advance!!!

    Hi,
    XML is really useful for representing data from different resources. E.g. it can take a database and represent it as an xml document or it could be an xml document hosted on an external server. By using XML in the jsps, the jsp is completed oblivious to where the xml data came from i.e. either a database or an xml doc. Using XML provides a level of abstraction so that the jsp only needs to know the methods to access/retrieve data not how to access the data i.e. it doesn't have to worry about how to connect to the database, and create sql statements.
    In the situation described if you can be sure that the information that the jsp retrieves will be only from a database then you do not need to use xml, otherwise by using xml you adhere to good-old object design principles.
    There is a paper on using xml and jsp see:
    http://java.sun.com/products/jsp/html/JSPXML.html
    Hope this helps.

  • I have my Adobe ID and the Adobe Digital Editions. But when I download an EPUB-ebook, I don´t see the ebook or "My Digital Editions-Bibliothek".What is the solution for this problem?

    @I have my Adobe ID and the Adobe Digital Editions. But when I download an EPUB-ebook, I don´t see the ebook or "My Digital Editions-Bibliothek".What is the solution for this problem?

    how can we solve the problem?
    I have my adob.e ID, downloaded Adobe Digital Editions.
    Whren I download EPUB-ebooks and want to use
    Digital Editions, nothing happens and appears!??
    Please give me a short feedback and help

  • With my i phone 4 , the Push notifications doesn't work for apps like (fb viber , whatsapp etc ) it only works for the official apps like message  even when im using the phone, has  this probleme with the iOs 6.0.1 and also with the iOs 6.1

    With my i phone 4 , the Push notifications doesn't work for apps like (fb viber , whatsapp etc ) it only works for the official apps like message  even when im using the phone, has  this probleme with the iOs 6.0.1 and also with the iOs 6.1

    This isn't an issue. Notice the screen prior to the one that shows usage has an iCloud section and a Manage Storage button. For this button to activate ios needs to download a few kb from icloud. Switching back to this screen forces ios to download those few kb.

  • Just finished using iTunes, closed out and then tried to get back in.  Got this message "he iTunes library .itl file is locked, on a locked disk, or you do not have write permission for this file."  How can I get back into iTunes ?

    I just finished using iTunes, closed out and then tried to get ack in.  Got this message "The iTunes library .9tl file is locked, on a locked disk, or you do not have write permission for this file."   How can I get back ino iTunes ?

    I actually figured it out...I had to go to the iTunes Library Extras.itdb file and give myself permission to have full control.  THEN, I could go and estore a previos version.  Once I had done this, I got the same message for iTunes Library Genius.itdb . . . I did the same thing with it and Voila'!!
    Hope this helps...
    SVT

  • I am using photoshop cc 2014.2 And when I use save for Web it gives me a dialog box that says it is using Latin characters I've never seen this before. Does anyone have an answer for this problem or do I need to go back to the older version of Photoshop.

    I'm using Photoshop cc 2014.21 I you save for Web and dialog box comes up that says that Photoshop is using Latin characters and that servers cannot recognize Latin characters in webpages and you may have trouble viewing these pages does anyone have an answer for this problem?

    What Firefox version is currently installed on the computer?
    You can find the full version of the current current Firefox release (37.0.2) in all languages and all operating systems here:
    *https://www.mozilla.org/en-US/firefox/all/
    If you have a very old Firefox version then you could consider to uninstall that version to clean up existing registry keys (especial the uninstall key).
    Make sure NOT to remove "personal data" when you uninstall Firefox, because that will remove all profile folders and you lose personal data like bookmarks and passwords including data in profiles created by other Firefox versions.
    Check the Firefox program folder and remove the Firefox program folder if there are still files left in it.
    *(32 bit Windows) "C:\Program Files\Mozilla Firefox\"
    *(64 bit Windows) "C:\Program Files (x86)\Mozilla Firefox\"
    It is important to delete the Firefox program folder to remove all the files and make sure that there are no problems with files that were leftover after uninstalling.
    *http://kb.mozillazine.org/Uninstalling_Firefox
    Your bookmarks and other personal data are stored in the Firefox profile folder and won't be affected by an uninstall and (re)install, but make sure NOT to remove personal data when you uninstall Firefox as that will remove all Firefox profile folders and you lose your personal data.
    *http://kb.mozillazine.org/Profile_folder_-_Firefox
    *http://kb.mozillazine.org/Profile_backup
    *http://kb.mozillazine.org/Standard_diagnostic_-_Firefox#Clean_reinstall

  • I can't connect my personal hotspot using wifi,I can able to find my iphone4 but it's just showing obtaing ip adress ??wat can I do for this problem??

    I can't connect my personal hotspot using wifi,I can able to find my iphone4 but it's just showing obtaing ip adress ??wat can I do for this problem??

    Hey there arunvdi,
    It sounds like you are unable to connect to the Wi-Fi created by your Personal Hotspot. I recommend these troubleshooting steps from the article named:
    iOS: Troubleshooting Personal Hotspot
    http://support.apple.com/kb/ts2756
    Try disabling and reenabling Personal Hotspot from Settings > Personal Hotspot or Settings > General > Cellular > Personal Hotspot.
    Reset network settings by tapping Settings > General > Reset > Reset Network Settings.
    Wi-Fi troubleshooting
    If you encounter an issue using Personal Hotspot with other Wi-Fi devices, try the following:
    Turn Personal Hotspot off.
    Verify Wi-Fi is on in Settings > Wi-Fi.
    Turn Personal Hotspot back on.
    If another device is unable to join your Wi-Fi network, ensure that the user enters the Wi-Fi password correctly. If the device cannot see your Wi-Fi network, verify your device name.
    Note: Windows may not properly display a device name that uses non-ASCII characters.
    Verify how many devices are currently using your Personal Hotspot. Depending on your wireless carrier and, the maximum number of Wi-Fi connections may be limited to three at one time.
    If the other device still cannot connect, try turning Wi-Fi off and on again on the device. Verify that your device can see other Wi-Fi networks.
    Note: If you see an "Unable to Join" or timeout error when trying to connect a device using Wi-Fi, ensure that you remain on the Personal Hotspot screen until the first device connects. After a device has connected, you can leave the Personal Hotspot screen and use your phone normally. Your device will automatically stop broadcasting using Wi-Fi if you put your device to sleep or after 90 seconds elapse with no devices connected using Wi-Fi. You can start broadcasting your Wi-Fi network again by tapping Settings > Personal Hotspot.
    If none of the previous steps resolves your issue, restore the iPhone.
    Thank you for using Apple Support Communities.
    Regards,
    Sterling

  • HT5163 I need one Ipad 128Gb with Retina iPad (3rd generation) Wi-Fi + Cellular (Black colour), where should I approach in Delhi/NCR for this product? I am unable to find this in Croma and other retailers.

    I need one Ipad 128Gb with Retina iPad (3rd generation) Wi-Fi + Cellular (Black colour), where should I approach in Delhi/NCR for this product? I am unable to find this in Croma and other retailers.
    i need one very urgently for my day to day work. Can anyone suggest from where I can get a new piece(boxed) on or before 11 March 2013? As this is already launched in India in Jan month, but still not available with retailers, might be because of less demand of this very product.
    Any help will be much appriciated.
    Waiting for some clue.
    Pawan Sharma

    The 3rd Generation iPad does not come in 128GB capacity, only 16, 32, and 64GB capacities.
    The 4th generation iPad does come in 128GB capacity.

  • I am leaglley blind I use a 5 inch window at the top of my screen to see what I am typing. This version does not let me see what I am typing because the window drops down about 1 to 2 inches, obscuring what I am typing. Is there a fix for this problem?

    If there is no fix for this problem, how do I go back to the previous version?

    laderamacguy took a look at it and added a black shape over the tab. he suggested i use a different template next time, such as the black template.

  • HT4484 i used to have an app store and lost it and i created a new one and i can not update the iphoto app because it was updated with the old account, is there a solution for this problem?

    i used to have an app store and lost it and i created a new one and i can not update the iphoto app on my macbook pro because it was updated with the old account, is there a solution for this problem?

    Content and Apple IDs -
    Content is forever tied to the Apple ID that bought it. Apple does not transfer content from one Apple ID to another. Apple does not merge Apple IDs. You will never be able to access your content bought with one Apple ID with a new Apple ID.

  • I've searched to no avail for this problem. Similar posts but none that tell me what to do. I can't add or delete any bookmarks on my iPad 2 running the newest iOS. I know how it's suppose to work, it just isn't working!

    I've searched to no avail for this problem. Similar posts but none that tell me what to do. I can't add or delete any bookmarks on my iPad 2 running the newest iOS. I know how it's suppose to work, it just isn't working!
    It started after the major update to iOS 7.
    I can't believe that this is so hard to do. It's just not letting me. I can add a bookmark to the home screen just fine, just not in a bookmarks folder anywhere I try.
    I've used Apple products since 2001 and have always loved how intuitive they are. But the Safari browser since iOS 7 has been the worst I've experienced. At least right in the beginning after that update.
    I'd really appreciate any help that doesn't just tell me how it's suppose to work...I know that.
    My iPad 4 is not affected with the problem and works as it should.

    To delete, tap "Edit" (tap to enlarge image)

  • For some strange reason the songs in my library are multiplying. For example, I really only have 4,000 songs but every time I open itunes I have 6,8,10,000 songs! Any one else have this problem.  How do I stop that from happening?

    For some strange reason the songs in my library are multiplying. For example, I really only have 4,000 songs but every time I open itunes I have 6,8,10,000 songs! Any one else have this problem.  How do I stop that from happening?  I keep having to delete my library!

    This "original file cannot be found" thing happens if the file is no longer where iTunes expects to find it. Possible causes are that you or some third party tool has moved, renamed or deleted the file, or that the drive it lives on has had a change of drive letter. It is also possible that iTunes has changed from expecting the files to be in the pre-iTunes 9 layout to post-iTunes 9 layout,or vice-versa, and so is looking in slightly the wrong place.
    Select a track with an exclamation mark, use Ctrl-I to get info, then cancel when asked to try to locate the track. Look on the summary tab for the location that iTunes thinks the file should be. Now take a look around your hard drive(s). Hopefully you can locate the track in question. If a section of your library has simply been moved, or a drive letter has changed, it should be possible to reverse the actions.
    Alternatively, as long as you can find a location holding the missing files, then you should be able to use my FindTracks script to reconnect them to iTunes .
    tt2

  • I am unable to print to the Lightning PDF software (by Avanquest Software) after updating to 6.02. It worked with the previous version of Firefox. Is there a patch or fix for this problem?

    I am unable to print to the Lightning PDF software (by Avanquest Software) after updating to 6.02. It worked with the previous version of Firefox. Is there a patch or fix for this problem?
    This is the error that is received:
    Cannot open file "C:\Users\Calandra\AppData\Local\Temp\Ask_a_Question__Firefox_Help.pdf". The process cannot access the file because it is being used by another process
    Any information that you can provide will be greatly appreciated.

    Sorry to say that, but the entire system seems ill-designed.
    If I understand correctly, then to add a new Section to a site, you have to subclass SubSection and somewhow integrate that into the system?
    I would be my expectation that you only need to subclass SubSection when you tried to introduce a new type of SubSection.
    The title & all other natural language Strings should never be hardcoded in Java code, but rather taken from some external storage (Database, XML, flat files, ...).

  • Can anyone solve (or) give me a solution for this problem.....?

    Hi Everyone!
    Can anyone solve (or) give me a solution for this problem.....?
    We have used two folders Source & Finalwork. We have get image from Source folder and put silo path & Moved to Finalwork folder.
    Is this possible to check whether both folder images Embedded color profile same or not.
    -yajiv

    Could you elaborate what you mean by this
    We have get image from Source folder and put silo path & Moved to Finalwork folder.
    Possibly again with a screenshot to illustrate the task?
    // 2012, use it at your own risk;
    #target photoshop
    var theFolder = Folder.selectDialog ("select source folder");
    if (theFolder) {
    var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd)$/i)
    var theOtherFolder = Folder.selectDialog ("select target folder");
    if (theOtherFolder) {
    var theOtherFiles = theOtherFolder.getFiles(/\.(jpg|tif|eps|psd)$/i)
    var missingFiles = new Array;
    var unmatchedProfiles = new Array;
    // work through files;
    for (var m = 0; m < theFiles.length; m++) {
              var check = false;
              var theFile = theFiles[m];
    // find name of the same name;
              for (var n = 0; n < theOtherFiles.length; n++) {
                        var theOtherFile = theOtherFiles[n];
    // if one is found;
                        if (theFile.name == theOtherFile.name) {
                                  check = true;
                                  var oneFile = app.open(File(theFile));
                                  var otherFile = app.open(File(theOtherFile));
    // check profiles;
                                  if (oneFile.colorProfileName == otherFile.colorProfileName) {
    // collect umages with unmatched profiles;
                                  else {unmatchedProfiles.push(theFile)};
    // collect missing files;
              if (check == false) {alert ("hahaha");missingFiles.push(theFile)}
    // alert of problems;
    if (unmatchedProfiles.length > 0) {
    alert ("the corresponding files for these do have a different color space:\n"+unmatchedProfiles.join("\n\n"));
    if (missingFiles.length > 0) {
    alert ("these files miss corresponding ones:\n"+missingFiles.join("\n\n"));
    ////// get psds, tifs and jpgs from files //////
    function getFiles (theFile) {
        if (theFile.name.match(/\.(jpg|tif|psd|pdf|)$/i)) {
            return true

  • Any one can plz give me the Solution for this problem

    select country ----text field
    select states ---------text field
    select country is one text field and select states is another text field .
    By using ajax if i select country like india states belonging to the india should be displayed in select states text field -----
    like if i select any country , belonging states should be displayed in selected states text field.
    can any one plz suggest me for this problem by using ajax with step by step solution.
    thanks in advance.....

    nareshannam wrote:
    can any one plz suggest me for this problem by using ajax with step by step solution.
    thanks in advance.....Well my Idea is to look for another Forum ,This is not a Ajax Forum
    maybe try [http://www.dynamicdrive.com/] or [http://www.webdeveloper.com/]

Maybe you are looking for