473,395 Members | 1,341 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Last number standing problem: extract number at specified position in array

this is regarding the last number standing problem.
it requires the number in an array to be extracted at a specified position .
then the array needs to traverse through the right position of the extracted number until there's the last number in the array. that number is the answer.
for eg.
if the specified position is 2
then in the array 1,2,3,4,5

it should go this way
1,3,4,5
1,3,5
3,5
3 ---.the answer
Jul 1 '08 #1
3 1762
acoder
16,027 Expert Mod 8TB
What have you tried so far? Please post your code.

Please remember to provide a meaningful Title for any threads started (see the FAQ entry Use a Good Thread Title).
Jul 1 '08 #2
hsriat
1,654 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.  
  3. //inputs
  4. var position = 2;
  5. var arr = new Array (1,2,3,4,5);
  6.  
  7. //function
  8. var offset = 0;
  9. while (arr[1]) {
  10.     var p = position - offset - 1;
  11.     while (!arr[p])
  12.     p -= arr.length;
  13.     while (arr[p]) {
  14.         offset = arr.length - p - 1;
  15.         arr.splice(p, 1);
  16.         //document.write(arr + '<br>' );
  17.         p += position - 1;
  18.     }
  19. }
  20. document.write ('<br>Answer = ' + arr[0]);
  21. </script>
I didn't find it very easy.

Regards
Jul 1 '08 #3
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.  
  3. //inputs
  4. var position = 2;
  5. var arr = new Array (1,2,3,4,5);
  6.  
  7. //function
  8. var offset = 0;
  9. while (arr[1]) {
  10.     var p = position - offset - 1;
  11.     while (!arr[p])
  12.     p -= arr.length;
  13.     while (arr[p]) {
  14.         offset = arr.length - p - 1;
  15.         arr.splice(p, 1);
  16.         //document.write(arr + '<br>' );
  17.         p += position - 1;
  18.     }
  19. }
  20. document.write ('<br>Answer = ' + arr[0]);
  21. </script>
I didn't find it very easy.

Regards





thank you for the help. the concept of offset helped a lot.
thank you very much.
Jul 2 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Jason | last post by:
Hi, I need a way to use random numbers in c++. In my c++ project, when using the mingw compiler I used a mersenne twister that is publicly available and this did its job well. Now I have...
9
by: Sharon | last post by:
hi, I want to extract a string from a file, if the file is like this: 1 This is the string 2 3 4 how could I extract the string, starting from the 10th position (i.e. "T") and...
21
by: Jaspreet | last post by:
I was working on some database application and had this small task of getting the second highes marks in a class. I was able to do that using subqueries. Just thinking what is a good way of...
35
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
13
by: Greg | last post by:
Most suggestions on this topic recommend to use a page footer and make it visible only on the last page. My problem is that the footer is half of the height of a page which means the detail would...
43
by: Xancatal | last post by:
Hey everybody. I need help on this one. I need to verify that a number entered by a user is not either a negative number (-100.00), or an alphabet (a, b, c, X, Y) as well as other number other than...
6
by: =?Utf-8?B?VGhvbWFzWg==?= | last post by:
Hi, Is it possible to read a file in reverse and only get the last 100 bytes in the file without reading the whole file from the begining? I have to get info from files that are in the last 100...
3
by: artev | last post by:
if I have an array where the number are write in string format, how can I recognize when a string is in realty a number? example if I have this "50" "house" "light" "100blue" "yellow20"...
16
by: sessmurda | last post by:
I am writing a fairly simple script that is supposed to print out lines from a txt file and group them based on where I want them. The general format of the files I am using the script on is as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.