473,587 Members | 2,547 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

6 New Member
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 1768
acoder
16,027 Recognized Expert Moderator MVP
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 Recognized Expert Top Contributor
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
thesinnerishere
6 New Member
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
4197
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 shelled out on VC++ 6.0 compiling that same code is proving difficult. I am not too worried how I generate random numbers in c++, as long as it is...
9
16962
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 extract 35 characters (including "T") from a file and then go to next line?
21
13464
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 getting second highest value in an integer array. One method I know of is to make the 1st pass through the array and find the highest number. In the...
35
11396
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", "r+"); fseek(file, -2, SEEK_END); fscanf(file, "%d", &c); this works fine if the integer is only a single character. When I get into larger...
13
14910
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 show on only the top half of each page with whitepace at the bottom of each page. How can I get the detail to fill each page with my "half page...
43
6543
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 positive integers or a decimal point. For example: Enter amount: and was capturing the float varialbe as in: scanf ("%f", &myVar)
6
5793
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 bytes of the file and some of these files are 600Mb -1 GB in size. I am getting "outofMemory.." exceptions on the largest files and the other files...
3
1614
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" "-100"
16
2301
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 follows: 662376 |GCGG | | | 662375 |CGCC | | | 662374 |GCGG | | | 662373 |CATC | | | 662371 |TCCC | |...
0
7920
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7849
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3844
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.