simulating pawn jump in the array/vector [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Assume we have a vector A of size N.
Pawn starts at A[0] and jumps to index pointed by A[0]: value of A[0] tells how it has to move, i.e: if index is 6.
A[6]=1 -> move 1 to the right, to A[7]
A[6]=-2 -> move 2 to the left, to A[4]
If pawn gets to the last index and it is positive, the pawn gets out of scope
example:
A 0 | 1 | 1 | 3 | 4 | 5
2 -1 4 1 4 2
Max value that each element contains is 1 000 000 and N < 1 000 000. function should return 4.
TASK: write a function int arrayJmp ( const vector<int> &A )that returns -1 if pawn will never get out of a table or returns number of moves if it will jump out of the array. worst case complexity should be O(n). you can find my answer below. is this right?

Are you using 'table', 'array' and 'vector' interchangeably?
The request is quite simple:
if ((i = array[ index ]) < 0 || i >= sizeof(array)) ;//out of bounds
otherwise i is within bounds

#include <iostream>
#include <vector>
int jump_array(const std::vector<int>& vector)
{
int index = 0;
int old_index = 0;
int size = vector.size();
int count = 0;
do
{
old_index = index;
index += vector[index];
if(count >= size)
{
return -1;
}
else
{
count++;
}
}
while(index < size);
return vector[old_index];
}
int main()
{
std::vector<int> vector;
vector.push_back(2);
vector.push_back(-1);
vector.push_back(4);
vector.push_back(1);
vector.push_back(4);
vector.push_back(2);
std::cout << jump_array(vector);
}

#include <algorithm>
void myfunction (int &i) { // function:
i=1000001;
}
int arrayJmp ( const vector<int> &A ) {
int N=A.size();
vector<int> indexes;
for_each (indexes.begin(), indexes.end(), myfunction);
int index=0;
while(std::find(indexes.begin(), indexes.end(), index) == indexes.end()){
indexes.push_back(index);
index+=A[index];
if (index==(N-1)&&A[index]>0) return indexes.size()+1;
}
return -1;
}

Related

Find the length of longest consecutive sub-sequence of a sequence in O(n) time where all elements are less than 10^6

I have to find the the length of largest increasing sub-sequence of an array such that difference between any two consecutive elements of sub-sequence is 1
For example: {5,4,2,1,6,2,3,4,5}
length of largest consecutive increasing sub-sequence : 5 {1,2,3,4,5}
SO far I have tried this:
#include <iostream>
using namespace std;
int a[1000001];
int m[1000001]={0};
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
m[a[i]]=i;
}
int maxm=0;
for(int i=1;i<=n;i++)
{
if(m[a[i]-1]==0 || m[a[i]]<=m[a[i]-1])
{
int k=a[i];
int prev = m[k];
k++;
int c=1;
while(m[k]>prev)
{
c++;
prev=m[k];
k++;
}
maxm=max(maxm,c);
}
}
cout<<maxm;
return 0;
}
But this is giving wrong answer for cases like{2,2,1,2,3,1,2,3,4,3,5}
Any help would be appreciated.
Let's discuss the algorithm here rather than jumping to the answer/code.
Associate a value with each element. The value with any element X will be how many elements from X-1 till 1 have I seen before I encountered X and add 1 to the value because now we have encountered X also.
So since an element of an array is strictly between 1 <= A[i] <= 106 we are in luck.
We make an array for each of the elements, whether they appear in the array or not. This kind of approach is similar to Hash Table
but since all our elements are integers, we are using an array as a simple hash table where key is the index of the array and value is the hash_table[index] i.e.. the value stores in the index.
Now lets dry run our approach for one of our sample inputs :
5 1 5 6 2 3 8 7 4
Initiall the hash-table looks like this :
hash_table = {0,0,0,0,0,0,0,0,0}; // Not showing indices > 8 because they won't be affected.
Now we encounter 5 :
We look up the value of hash_table[4] and add 1 and put it as the value of 5 i.e. hash_table[5] = hash_table[4] + 1
So hash table looks like this now :
hash_table = {0,0,0,0,0,1,0,0,0};
Then we encounter 1 : we do the same thing :
hash_table = {0,1,0,0,0,1,0,0,0};
Like that after taking in all the numbers hash_table looks like this :
hash_table = {0,1,2,3,4,1,2,3,1}
Our answer is the maximum value of the hash_table, which is 4.
Talk is cheap show me the code :
#include <stdio.h>
#define MAX (int)1e6
int h[MAX];
int main ()
{
int N,i,max=0,temp;
scanf ("%d",&N);
for (i=0;i<N;i++)
{
scanf ("%d",&temp);
h[temp] = h[temp - 1] + 1;
if (h[temp] > max)
max = h[temp];
}
printf ("%d\n",max);
return 0;
}
So what if you can't upvote. You can still accept this answer if you found it useful !
You are thinking a bit too complicated. You just have to iterate through the array once and count the lenght of sequences and remeber the longest one :
int main() {
int size;
int input[100000];
/* ... get your input with size elements ... */
int current = 1;
int biggest = 1;
for (int i=1;i<size;i++) {
if (input[i] == input[i-1] + 1) { current++; }
else {
if (current > biggest) { biggest = current; }
current = 1;
}
}
}

Modified binary search [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have been trying to solve a problem where i have an array lets say A[]={5,9,11,15}; and 2 variables with values lets say 2 and 10 .i need to find whether any element of the array belong to (2,10] i.e it has value between 2 (excluded) to 10 (inclusive). i could simply turn a loop and search whether the >if(2=A[i])< but this wont work at large value of array size lets say 10^5. and also i tried using modified binary search which returns value of index less than or equal to value to key provide but failed .can anyone provide me with a fast algo for doing this?
EDIT: pos here are the number of elements breaaking is the array FLOOR is the function(modified binary)
int Floor(int A[], int l, int r, int key)
{
int m;
while( r - l > 1 )
{
m = l + (r - l)/2;
if( A[m] <= key )
l = m;
else
r = m;
}
return l;
}
int Floor(int A[], int size, int key)
{
// Error checking
if( key < A[0] )
return -1; // There is no floor value
return Floor(A, 0, size, key);
}
//
int ret=Floor(breaaking,pos,mini);
printf("%d\n",ret);
printf("mini is %d and maxi is %d",mini,maxi);
if(pos==0)
{
printf("There is no breaking point in the array :) (pos==0)\n");
printf("Yes\n");
}
else if(ret==-1)
{
printf("Mini is smaller than smallest element of breaking\n");
if(breaaking[0]<maxi)
{
printf("but maxi is greater than smallest element hece it lies between so:\n");
printf("No\n");
}
else {
printf("even maxi is less than smallest element hence:\n");
printf("Yes\n");
}
}
else if(ret==pos-1)
{
printf("mini is either equal to last element of breaker set or greater than it\n");
if(mini==breaaking[pos-1])
{
printf("mini is equal to the last element hence\n");
printf("No\n");}
else
{
printf("mini is greater than the last element hence:");
printf("Yes\n");
}
}
else
{
printf("returned a valid index which is less than or equal to mini which is btw %d\n",ret);
if(breaaking[ret]==mini)
{
printf("mini was equal to one of the element of array hence\n");
printf("No\n");
}
else
{ printf("mini is smaller than this element but greater than next element\n");
if(breaaking[ret+1]<maxi)
{
printf("next element lies between mini and maxi hence:\n") ;
printf("No\n");
}
else
{ printf("even maxi is smaller than next element hence\n");
printf("Yes\n");
}
}
`}
You can simply use std::lower_bound to return you a range that contains all values. The range will be empty, if there are none.
#include <iostream>
#include <algorithm>
#include <tuple>
template<typename ForwardIterator>
std::pair<ForwardIterator, ForwardIterator>
range_inside(ForwardIterator b, ForwardIterator end,
int lower, int upper) {
auto it = std::lower_bound(b, end, lower);
auto it2 = std::upper_bound(it, end, upper);
return std::make_pair(it, it2);
}
int main()
{
int arr[] = { 2, 5, 9, 10, 11, 15};
int *r, *e;
std::tie(r, e) = range_inside(std::begin(arr), std::end(arr), 2, 10);
std::for_each(r, e, [] (int& x) { std::cout << x << " "; });
// output: 2 5 9
return 0;
}

Converting integer into array of digits [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I need to convert two integers into two arrays of digits, so for example 544 would become arr[0] = 5, arr[1] = 4, arr[2] = 4.
I have found some algorithms doing this, but they create new array, and return this. I would have to allocate this memory for two arrays, so I wanna pass two integers by reference and do this on them directly.
I guess I can do this, because these integers are in fact template types, so they should be changeable. That's why I added C++ tag here.
Just using something like this:
int n = 544; // your number (this value will Change so you might want a copy)
int i = 0; // the array index
char a[256]; // the array
while (n) { // loop till there's nothing left
a[i++] = n % 10; // assign the last digit
n /= 10; // "right shift" the number
}
Note that this will result in returning the numbers in reverse order. This can easily be changed by modifying the initial value of i as well as the increment/decrement based on how you'd like to determine to length of the value.
(Brett Hale) I hope the poster doesn't mind, but I thought I'd add a code snippet I use for this case, since it's not easy to correctly determine the number of decimal digits prior to conversion:
{
char *df = a, *dr = a + i - 1;
int j = i >> 1;
while (j--)
{
char di = *df, dj = *dr;
*df++ = dj, *dr-- = di; /* (exchange) */
}
}
A simple solution is:
int i = 12312278;
std::vector<int> digits;
while (i)
{
digits.push_back(i % 10);
i /= 10;
}
std::reverse(digits.begin(), digits.end());
or, string based ( i >= 0 )
for (auto x : to_string(i))
digits.push_back(x-'0');
Call the integer a.
To get the units digit of a, a % 10
To shift a down so the tens is the units digit, a / 10
To know when you're done, a == 0
To know how large your array needs to be in the first place, min(ceil(log(a+1, 10)), 1) (to convince yourself this works, try the logarithm part of it in a calculator. if you don't have multiple argument log, use the identity log(x,y) == log(x)/log(y))
You can do something like this if you are using C++:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int a=544;
stringstream str;
str << a;
string arr;
str>>arr;
for(int i=0; i<arr.length(); i++)
{
cout << arr[i];
}
system("pause");
return 0;
}

For which cases does this algorithm fail? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
What does it do - element at index 'i' is the product of all input elements except for the input element at 'i'.
As an example, if arr = { 1, 2, 3, 4 }, then
output = { 2*3*4, 1*3*4, 1*2*4, 1*2*3 }.
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
int n;
long long int arr[1000]={0},prod=1;
cin>>n;
for(int i=0;i<n;i++){
cin>>arr[i];
prod*=arr[i];
}
if(prod!=0)
for(int i=0;i<n;i++){
cout<<(prod/arr[i])<<endl;
}
else
for(int i=0;i<n;i++){
cout<<"0"<<endl;
}
return 0;
}
The simplest case for which it fails is 2 0 1. The correct result would be 1 0, your result is 0 0.
More generally, it fails if there is exactly one zero and at least one non-zero in the input set.
As was already noted, the problem is when one of the inputs is zero, and you try to divide by zero. To properly compute the products, an algorithm which only performs multiplications and no divisions is needed, such as the following one.
#include <stdio.h>
#include <stddef.h>
// Input: an array of 2^(exp) doubles
// Output: an array of 2^(exp) doubles, where each one is the
// product of all the numbers at different indices in
// the input
// Return value: the product of all inputs
double products (unsigned int exp, const double *in, double *out)
{
if (exp == 0) {
out[0] = 1;
return in[0];
}
size_t half_n = (size_t)1 << (exp - 1);
double prod_left = products(exp - 1, in, out);
double prod_right = products(exp - 1, in + half_n, out + half_n);
for (size_t i = 0; i < half_n; i++) {
out[i] *= prod_right;
out[half_n + i] *= prod_left;
}
return prod_left * prod_right;
}
int main ()
{
double in[8] = {1, 2, 3, 4, 5, 6, 7, 8};
double out[8];
products(3, in, out);
for (size_t i = 0; i < 8; i++) {
printf("%f ", out[i]);
}
printf("\n");
return 0;
}
This requires O(n*log(n)) time and no extra space, except for the O(log(n)) space used by the recursion. While it's nice and easy to understand, it is not optimal; see this question.

How I do fibonaci sequence under 1000? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
#include <iostream>
using namespace std;
void main()
{
int i = 0;
while (i < 1000)
{
int TEMP = i * 2;
cout << i << endl;
TEMP = i;
i = i +1;
// ???
}
return;
}
I'm so confused?? :(
The Fibonacci sequence F is F(n) = F(n - 1) + F(n - 2), F(0) = 0, F(1) = 1.
Here's some psuedo-code:
Start Counter1 at 0
Start Counter2 at 1.
For i = 0 to 1000
New value = Counter1 + Counter2
Print new value
Counter2 = Counter1
Counter1 = New Value
End For
This doesn't print out 0 or 1; it starts at F(2). You can easily fix this by just printing out 0 and 1 first. Also, this code prints the first 1000 numbers. If you change this to: While Counter1 < 1000, you'll stop when you reach or pass 1000.
It's up to you to implement it, and make sure you understand how it works.
First you should check that you understand the definition of the Fibonacci numbers.
By definition, the first two Fibonacci numbers are 0 and 1, and each remaining number is the sum of the previous two. Some sources omit the initial 0, instead beginning the sequence with two 1s.
You need two variables to remember the state, not just one as you were trying to do. And you don't multiply by two, you just add the two variables.
#include <iostream>
using namespace std;
int main()
{
int i = 0;
int j = 1;
while (i < 1000)
{
/* Print a number. */
cout << i << endl;
/* Set j to the sum of i and j, and i to the old value of j. */
int TEMP = j;
j += i;
i = TEMP;
}
return 0;
}
If you would like just a hint, Google "recursion".
If you would like the answer, Google "recursion fibonacci C++", but PLEASE try to work it out with the hint above :) It's worth it.