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
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).
hsriat 1,654
Recognized Expert Top Contributor - <script type="text/javascript">
-
-
//inputs
-
var position = 2;
-
var arr = new Array (1,2,3,4,5);
-
-
//function
-
var offset = 0;
-
while (arr[1]) {
-
var p = position - offset - 1;
-
while (!arr[p])
-
p -= arr.length;
-
while (arr[p]) {
-
offset = arr.length - p - 1;
-
arr.splice(p, 1);
-
//document.write(arr + '<br>' );
-
p += position - 1;
-
}
-
}
-
document.write ('<br>Answer = ' + arr[0]);
-
</script>
I didn't find it very easy.
Regards
- <script type="text/javascript">
-
-
//inputs
-
var position = 2;
-
var arr = new Array (1,2,3,4,5);
-
-
//function
-
var offset = 0;
-
while (arr[1]) {
-
var p = position - offset - 1;
-
while (!arr[p])
-
p -= arr.length;
-
while (arr[p]) {
-
offset = arr.length - p - 1;
-
arr.splice(p, 1);
-
//document.write(arr + '<br>' );
-
p += position - 1;
-
}
-
}
-
document.write ('<br>Answer = ' + arr[0]);
-
</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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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?
|
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...
|
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...
|
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...
| |
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)
|
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...
|
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"
|
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 | |...
|
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...
|
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...
| |
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. ...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |