We have an array with values [2,1,5,3,7] and we have to find number of ways to form a spiral array by deleting one element at a time. Lets say when we remove 2 we can have [1,5,3,7] and when we remove 1 then we have [2,5,3,7]. There is no more possibility. so we should return 2. Do you have any idea how can i solve this problem in Java?
Related
First of all, I apologize for the confusing title, the task which I'm trying to accomplish is itself still confusing to me, hence why I'm finding it hard to do it. I'll try to be as clear as I can from now on.
I have 100 500x500 arrays, the values inside range from 0 to 1. What I would like to do is write a code that gives me 10 arrays, these arrays will be a sort of composite of the minimum values between them.
The first array is made of the absolute minimum values, the second array with the 2nd order minimum values....and so on. So the 10 arrays will be a composite of sorted ascending values.
I managed to get the absolute minimum with np.minimum() but I have no clue on how to proceed to the next ones.
To reiterate, I don't want to sort the 100 arrays, but loop through them and create new arrays with the lowest values found in each position.
Sorting is the most efficient way.
np.sort([array0,array1,...], 0)
Will yield an array where the first element is an 100x100 array of the smallest element-wise entries of all your arrays, the second the second smallest, etc.
I've come across a problem that at first looks like the well known maximum sum subarray problem but there's a twist that makes things much more complex.
Suppose you have two arrays each containing the same amount of "1"s and "-1"s. Additionally, suppose each "1" in the first array has a corresponding or sibling "1" in the second array. Same for each "-1". The task is to find the optimal subarrays, one in the first array and one in the second, such that their combined sum is the largest (maximal) with the added constraint that an element in one subarray only counts towards the sum if the other subarray contains its sibling.
Anyone know what kind of problem this is? It looks like it could be a graph problem in disguise but I'm not sure which. If there's optimal substructure here I don't see that either. I know it can be solved by complete search but surely there's a faster way.
Below is an example of the setup to the problem.
Here the optimal solution is subarray [2..9] in the first array with subarray [4..9] in the second array for a sum of 8.
I need to populate a first array with 20 numbers then I have to populate a second array using the numbers of the first array but without duplicate.
My question is: which is the best way to do this?
The only way I've found is:
populate the first array
populate the second array using the same numbers
delete duplicates from the second array
but I suppose this is not the best solution. Any suggestion?
homework?
One approach would be to sort the array (search for qsort). Then duplicate elimination is as simple as copying the numbers from array 1 to array 2 skipping all equal numbers.
I encountered an interview question.
Given two sorted arrays.
Initially both have set set of elements but one element is removed from one array.
Find the element removed.
Constraint is we have to done it inplace in O(logn)
For ex:
arr1[]={1,2,3,8,12,16};
arr2[]={1,2,8,12,16};
element removed is 3
I am typing from mobile, so it is a pseudo code, but you will get it:
take arr1.len / 2. It is 3. Check arr1[3] and arr2[3]. If they equal then missing vsalue is in index greater than 3 else less than 3. Here we get 8 and 12. So missing is before. We take index 3/2=1. Compare arr1[1] and arr2[1]. They are equal, so missing is after index 1 and before 3. So it is arr1[2] = 3.
This is the idea. You are doing a binary search, deviding searvh area by half everytime. You take left or right part of the array depending on comparing. You just need to implement this and do some checks, but the idea is clear I think.
Hi I have problem with how to test my sort arrays. I have no problem in the coding of them however we are supposed to develop "smart" test cases. To test the sorting methods.
I dont get what the smart cases would be. I know if I was developing a calender a smart thest case would be the last day of the year etc. However I dont understand it for sorting.
The only thing I can think of would be the middle element the first and the last.
I dont want any code just some feedback on what you thing smart cases would be.
You could try to sort in increasing order:
a sorted array
an almost sorted array( only a few swaps would sort it )
a random array
an almost sorted array in decresing order
a sorted array in decreasing order
Afetr analising the performance on those, you could try:
random array with many duplicates
If you're checking for correctness, you may want to try:
a reversed order array: [5,4,3,2,1]
a array with repeated elements [1,1,3,3,2,2]
a array with only repeated elements [1,1,1,1,1]
arrays with odd and even number of elements
If you're going to use associative arrays, you may want to check for stability.
If you're checking for runtime complexity, you may also want to try longer arrays.