## Linear pass over an array has me stumped

codingbat arrays

Hi I'm trying to do a single linear pass over two arrays, here is my problem: I have it working with the commented code but it's not correct. I've tried to solve this problem for a whole night before asking for help - here is what I've tried:

////////////////////////////// PROBLEM STATEMENT ////////////////////////////// // Given two arrays of ints sorted in increasing order, outer and inner, // // print true if all of the numbers in inner appear in outer. The best // // solution makes only a single "linear" pass of both arrays, taking // // advantage of the fact that both arrays are already in sorted order. // // {1, 2, 4, 6}, {2, 4} -> true // // {1, 2, 4, 6}, {2, 3, 4} -> false // // {1, 2, 4, 4, 6}, {2, 4} -> true // /////////////////////////////////////////////////////////////////////////////// // >>>>>> Your Java Code Fragment starts here <<<<<< Boolean prez = true; Scanner kb = new Scanner(System.in); int n = kb.nextInt(); int z = 0; int[] inner = new int[n]; for(int i = 0; i < inner.length; i++)inner[i] = kb.nextInt(); n = kb.nextInt(); int[] outer = new int[n]; for(int i = 0; i < outer.length; i++)outer[i] = kb.nextInt(); // if(outer.length == 0) // prez = true; // for(int i = 0; i < outer.length; i++){ // for(int o = 0; o < inner.length ; o++){ // if(outer[i] != inner[o]){ // prez = false; // } if(outer[i] == inner[o]){ // prez = true; // break; // } // } // if(i == 1 && outer[i] == 3){ // prez = false; // break; // } // } // for(z = 0; z < outer.length ; z++){ // if(inner[z] == outer[z]){ // z++; // prez = true; // }else if(outer[z] != inner[z]){ // prez = false; // } // } int i = 0; //ok lodo so we loop through the array for(int j = 0; j < inner.length; j++) { //while i is less than the outer length and out[i] is less than inner[j]??? while(i < outer.length && outer[i] < inner[j]) { //we increase i 1 i++; } // if "i" is the same or equal to outer size or the current outer doesnt equal inner make prez false if(i >= outer.length || outer[i] != inner[j]) { prez = false; } } //prez = true; //print the results System.out.print(prez);

Am i understanding correctly? here is the output

I am trying lodo's solution as about. Here is the output Do i need a break when i get a false?

You need one loop over the inner array, and an external index for the outer array:

boolean checkContained(int[] inner, int[] outer) { int i = 0; for(int j = 0; j < inner.length; j++) { while(i < outer.length && outer[i] < inner[j]) { i++; } if(i >= outer.length || outer[i] != inner[j]) { return false; } } return true; }

EDIT:

Also (as I see in your output examples), you have to read the outer array first, while in your code you read the inner array first:

int n = kb.nextInt(); int z = 0; int[] outer= new int[n]; for(int i = 0; i < inner.length; i++)inner[i] = kb.nextInt(); n = kb.nextInt(); int[] inner= new int[n]; for(int i = 0; i < outer.length; i++)outer[i] = kb.nextInt();

**In case you have some trouble,** To others seeing this, let me tell not to copy and paste the code untill after couldn't have gotten through Arrays and Loops III without your help Loan. I got stumped on fitting the if statement into the for loop - it wasn't intuitive what the I had let time pass before getting back into the lessons so by the time I hit the blank box� An array is a valid parameter to pass to useState(), as is shown here: In the next section I try various methods to add to the array that is currently in the React state — accessible via

You can manage 2 indexes or 2 iterators, one on each array. A solution with 2 indexes:

boolean ok = true; int outerIndex = 0; int innerIndex = 0; while (ok && outerIndex < outer.length && innerIndex < inner.length) { if (outer[outerIndex]==inner[innerIndex]) { outerIndex++; innerIndex++; } else if (outer[outerIndex] < inner[innerIndex]) { outerIndex++; } else { ok = false; } } // test if there are remaining values in inner array if (innerIndex < inner.length) { ok = false; } // here ok is true if all inner values have been found in outer

**Member array usage reduced � Issue #5056 � NREL/EnergyPlus ,** @Myoldmopar @lgentile Here is the quick member array reduction pass we discussed. As a bonus, if the FindItem* search is over the whole array the size argument can be omitted I want to label my PRs better but this one has me stumped. items in an O( log N ) lookup container and not be doing linear searches at all� Linear equality constraints, specified as a real vector. beq is an Me-element vector related to the Aeq matrix. If you pass beq as a row vector, solvers internally convert beq to the column vector beq(:). For large problems, pass beq as a sparse vector. beq encodes the Me linear equalities

How would you do this on paper?

You would walk along both array from the start, managing two independent indexes. For the first iteration, both indexes are zero.

Now, you have 3 possibilities:

- the number at
`0`

in both arrays are equal - the number in the outer array is higher
- the number in the inner array is higher

So under `1.`

, we can simply increment both the indexes and compare the resulting two numbers.

Under `3.`

, as we know the outer array is sorted, we can increment the index of the outer array and compare the resulting two numbers. In the hopes if finding the value higher up.

Under `2`

, as we know the inner array is sorted, we know the outer array cannot contain the value and we can abort immediately.

From the logic we can see that we only need one loop, and we only ever increment the indexes, and we always increment at least one index (or exit). So the most we will ever do is travel over both arrays once - making this algorithm linear.

So the skeleton for the code would look like this:

public static boolean containsAll(int[] outer, int[] inner) { int outerIdx = 0; int innerIdx = 0; while (outerIdx < outer.length && innerIdx < inner.length) { final int o = outer[outerIdx]; final int n = inner[innerIdx]; if (o == n) { //elements are equal - situation 1. } else if (o > n) { //outer element greater than inner - situation 2. } else if (n > o) { //inner element greater than outer - situation 3. } } //do we need to check anything here? }

**Analysis of Algorithms,** Instead, we will analyze an algorithm by predicting how many steps it takes, and Assuming the worst case from now on, if the array contains 3 values, 51 instructions are executed. but we can state that once we pass some threshold for N (call it N0), the O(N) O(N), Linear, Linear Search (arrays or linked lists), 2T (N). Now, you have all the functionalities you need to implement linear regression. The fundamental data type of NumPy is the array type called numpy.ndarray. The rest of this article uses the term array to refer to instances of the type numpy.ndarray.

Here is my answer hope it explains everything with comments I put in

public boolean linearIn(int[] outer, int[] inner) { int p = 0;//pointer for inner array if(inner.length==0)return true;//no inner array for(int i=0;i<outer.length;i++){//iterating outer array if(inner[p] == outer[i]){//found a inner array element in outer p++;//increment pointer of inner array } if(p==inner.length){//if everyone in inner array is found return true; } } return false; }

**Javascript includes multidimensional array,** Typescript flatten nested array of objects Includes in MultiDimensional Array � javascript. input data (it can be 2 or 4 or Hello experts, I've got a problem that has me stumped. An array keeps track of multiple pieces of information in linear order, Passing PHP Arrays to JavaScript is very easy by using JavaScript Object� Counting sort is a sorting technique based on keys between a specific range.. It works by counting the number of objects having distinct key values (kind of hashing). Then doing some arithmetic to calculate the position of each object in the output sequence.

**Stumped: Array Validation - Just getting started,** Hello folks, Newbie here, and I've been reading through lots of posts to try and suss I get a request back for a PlaceID and it presents me with a bunch of arrays a PASS for photo_reference being present to then confirm it has content and� The above statement assigns the 5 th element in the array with a value of 50.0. All arrays have 0 as the index of their first element which is also called the base index and the last index of an array will be total size of the array minus 1. Shown below is the pictorial representation of the array we discussed above − Accessing Array Elements

**Repeating pattern along path,** Hiya all, I have been using FreeCAD for a while now and manage to hack out what I need albeit nothing fancy but this has me stumped. How do I create a repeating pad or cut along a non-linear path. You then only need to drop the Sketch over the Body, and attach it to its XY_Plane (let me know if you� The technique that I shared with Tom that day was to do a straight linear interpolation between the point values of one division to the next. This works well. I shared with him that it was also possible to implement EASING so the animation has a soft start and end. I did not have time to give him an example, but I hinted at how to do it.

**Using Arrays to Define a Model | OR-Tools,** In any MIP program, you start by importing the linear solver wrapper and declaring the MIP solver, The following code creates arrays containing the data for the example: the variable print('The problem does not have an optimal solution.'). (A second reason to use eachindex is that some arrays don't starting indexing at 1, but that's a topic for a separate blog post.) As we'll see below, these iterators have another purpose: independent of whether the underlying arrays have efficient linear indexing, multidimensional iteration can be a powerful ally when writing algorithms.

##### Comments

- It is spelled "linear". And by the way, since you have a
`for`

loop inside a`for`

loop, it is not going to be linear**even**if it does output the correct result. - It's spelled correctly in the question you have - is it that hard to spellcheck your question before posting?
- "I have it working with the commented code but it's not correct" - what do you mean by "working", if it's not correct? What do you mean by "not correct"? Please post sample input and expected/actual output... (Your commented out code isn't linear, mind you...)
- @JonSkeet I suppose the OP means that it's working with multiple passes - a sort of
`foreach(x -> y.contains(x))`

arrangement. - @BoristheSpider: Maybe. Hopefully they'll clarify.
- I tried this answer and it still gave me wrong output
- @Brett Look at my comment in the question.
- Thanks @lodo I am updating my code to show what i tried and the output i have
- Thanks @lodo looking and going through now
- I have posted new output and question