in Education by
I am trying to implement the Josephus election problem using an array as a mock circular linkedlist. The item and next arrays represent nodes for circular linked list. On every Mth element one element is marked for removal. The next array skips over the marked index. When there is one element left in the array we print out the element and the item that is left. The problem is from Sedgewick C++ Algorithms books Chapter Third Edition. exercise 3.31. The output I am getting is incorrect, I am not lost. void array_represent_linked_list(int num_ints, int M) { int* item = new int[num_ints]; int* next = new int[num_ints]; //populated item and next array with elements for (int index = 0; index < num_ints; index++) { item[index] = index + 1; if (index == (num_ints - 1)) { next[index] = 0; } else { next[index] = index + 1; } } int nums_left = num_ints; int x = 0; int count = 0; int last_element_index = 0; while ( nums_left > 1) { //if current value divisible by M-2? if ((count % M-2) == 0) { if ((nums_left - 1) == 1) { //record the next index which is the last element last_element_index = next[x]; } else { //mark for removal of element from array next[x] = next[next[x]]; } nums_left -= 1; } //move to next element of array x = next[x]; count++; } std::cout << item[last_element_index]<< " " << last_element_index<< std::endl; } output for array_represent_linked_list(9,5); //item[x] =8 , next[x] = 7 JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
After writing a brute force method which looks at each individual element in the item array and setting each element to 0 when we reach a skip factor of M. I was able to test with this code snippet and get the correct answers. Breaking down the problem, every M-1 elements should set the next[index] to next[next[index]] , and we traverse the array with x = next[x]. void array_represent_linked_list(int num_ints, int M) { int* item = new int[num_ints]; int* next = new int[num_ints]; //populated item and next array with elements for (int index = 0; index < num_ints; index++) { item[index] = index + 1; if (index == (num_ints - 1)) { next[index] = 0; } else { next[index] = index + 1; } } int nums_left = num_ints; //used to track elements in item array int index = 0; //used to count number of elemenets traversed in the list int count = 0; //store the return values int return_index = 0; int return_val = 0; while (nums_left > 1) { count += 1; if (count == (M - 1)) { //reset the count after reaching 4 elements count = 0; //update next element next[index] = next[next[index]]; //decrease nums found nums_left -= 1; } //traverse to next element index = next[index]; } return_index = index; return_val = item[next[index]]; std::cout << return_index << " " << return_val << std::endl; }

Related questions

0 votes
    It is common practice to declare stack variables (variables allocated on the excecuting stack rather than dynamically ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I am working on a very large scale computing library that is using STL heavily. The library is being ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 12, 2022 in Education by JackTerrance
0 votes
    When compiling boost filesystem (1_46_1) with Intel 12 Release 4, and Visual Studio 10, I get this error ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    _________ is an indication that a fatal problem has occurred and execution of the function stops. (a) message (b ... of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    I have a c++11 type alias: using coord = std::array; Can I define the operator + for coord? ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 29, 2022 in Education by JackTerrance
0 votes
    I have a c++11 type alias: using coord = std::array; Can I define the operator + for coord? ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I have a c++11 type alias: using coord = std::array; Can I define the operator + for coord? ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 22, 2022 in Education by JackTerrance
0 votes
    I have a bunch of vector classes. I have a 2D point vec2_t, a 3D point vec3_t and a 4D point ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    Consider the following code. What is a good hashing function for the array in Key to be used in ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have a bunch of vector classes. I have a 2D point vec2_t, a 3D point vec3_t and a 4D point ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    ______ splits a data frame and results an array (hence the da). Hopefully you're getting the idea here. ... Regression of R Programming Select the correct answer from above options...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    Why does a sorted array get processed faster than an unsorted array, even though the size of the arrays are ... processed faster. Why? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    How to solve array problem? $kuponlar = $core->query("SELECT * FROM kupon WHERE kupon_durum = ?", ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    How to solve array problem? $kuponlar = $core->query("SELECT * FROM kupon WHERE kupon_durum = ?", ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 13, 2022 in Education by JackTerrance
0 votes
    I am working on a C++ program, which uses cURL library, which is written in plain C. When I try ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Aug 1, 2022 in Education by JackTerrance
...