This question already has answers here:
What is an easy way to break an NSArray with 4000+ objects in it into multiple arrays with 30 objects each?
(3 answers)
What's the Best Way to Shuffle an NSMutableArray?
(12 answers)
Closed 5 years ago.
I have an array of string elements like this [A,B,C,D,E,F,G,H,I,J,K,L,M,N,O]
I want to split this into n arrays, where n is chosen randomly, and the elements in each new array are also chosen randomly.
For example if n=4, and the original array is [A,B,C,D,E,F,G,H,I,J,K,L,M,N,O], I might get
[C,E,G,A]
[I,B,O,L]
[H,F,J,N]
[D,K,M]
How can I do this?
Related
This question already has answers here:
How to find sum of elements from given index interval (i, j) in constant time?
(5 answers)
Closed 4 years ago.
I have large array with random number stored in it. I want to create a function which returns the sum of numbers from given starting and end position in O(1) time.
Is there another method of storing number to achieve O(1) ?
It’s not possible to make a O(1) code, to get the sum of numbers.
This question already has answers here:
find an element in a sorted matrix [duplicate]
(9 answers)
Search a sorted 2D matrix [duplicate]
(4 answers)
Most efficient way to search a sorted matrix?
(9 answers)
Find number in sorted matrix (Rows n Columns) in O(log n) [duplicate]
(5 answers)
Closed 5 years ago.
In sorted array, we can search in O(logn) by binary search.
But I thought that in n*n array, how can we apply this algorithm(or other) to the array to search faster?
n*n list is sorted each row and column like below.
1 3 7 13 19
2 5 12 14 20
4 9 15 16 22
8 10 18 23 25
11 17 21 24 27
Obviously the naive solution is to perform binary search on each row (or column) which will result in a runtime complexity of O (n log n).
The main problem I see in directly adapting binary search to the whole matrix is that there is no complete ordering. This makes it hard to define a "split element" where all elements to the "left" are smaller and all elements to the "right" are larger.
My approach for a direct adaptation would be closer to a spatial index like a quad-tree. The basic observation is the following: For each sub-matrix of the original matrix you can define bounds of the elements by looking at the top-left (lower bound) and bottom-right (upper-bound) element.
Now you can basically perform depth first search, recursively splitting the matrix in 4 sub matrices, computing upper and lower bounds for each sub matrix and discarding or exploring it depending if the key is within its bounds.
This question already has answers here:
How to count possible combination for coin problem
(18 answers)
Closed 8 years ago.
I have given an array.And i want to find the all permutation of an array so it sum to a specific numbers.ExampleArray a =[2,3,5 ,1] Target = 8`Solution: [2,2,2,2] ,[5,3] ,[3,3,2] ,[5,2,1] and all possible combinationPlease provide me a approach to solve this the problem , the problem i am facing how to handle the repetition of the elements.Target is a large number of 10^6.
I think it is same asThis theory
You are facing a typical Subset Problem. The worst case complexity of this problem is exponential no matter how you put it. You might find good polynomial-time approximations that work wonders for average case though.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Code to calculate “median of five” in C#
Suppose I have five numbers in a list, say [3,2,1,4,5].
How do I find the median with 6 comparisons?
Take an optimal sorting network for N=5 which requires 9 comparisons and then prune the unneeded comparisons for outputs 0, 1, 3 and 4. There's an example of this which uses 6 comparisons in the answer to this question.
This question already has answers here:
Interview: Remove Loop in linked list - Java
(7 answers)
Closed 9 years ago.
A loop may occur in singly linked list (SLL).
To delete the loop in the list, first we need to detect the loop in the SLL and then delete the loop.
Can any one tell how to delete the loop in a SLL with pseudo code?
Can we do it using 3 pointers?
Is there any alternate to accomplish the task?
There are many solutions to what you ask. One of the easiest yet inefficient methods consists of reversing the list, while remembering the head node. If you get back to the head node, then you know that a loop exists.
Another way to check is by creating an array containing an int for each node in the list, each time you visit a node, increment its' corresponding value in the array. Then all you have to do is check to see if a value in the array has more than one, and then compare that to where the extra iterations start. This method detects full loops, and small loops. Hopefully this is helpful.