sub-array

find minimum-length subarray that has all numbers

自古美人都是妖i 提交于 2019-12-03 15:39:21
File input.txt consists of two lines: first has integer number N space then integer number K (1 ≤ N,K ≤ 250000). Second has N space-delimeted integers, where each integer is less than or equal to K. It is guaranteed that each integer from 1 to K is in the array. The task is to find subarray of minimum length, that contains all integers. And print its start and end. Note, that indexing starts from 1. Examples: Input Output 5 3 2 4 1 2 1 3 2 6 4 2 6 2 4 2 3 3 1 I had this task in a recent programming competition. It is over, and I am not cheating. I've implemented it using python 3: with open(

Numpy unique 2D sub-array [duplicate]

佐手、 提交于 2019-12-01 17:29:45
This question already has an answer here: Find unique rows in numpy.array 20 answers I have 3D numpy array and I want only unique 2D-sub-arrays. Input: [[[ 1 2] [ 3 4]] [[ 5 6] [ 7 8]] [[ 9 10] [11 12]] [[ 5 6] [ 7 8]]] Output: [[[ 1 2] [ 3 4]] [[ 5 6] [ 7 8]] [[ 9 10] [11 12]]] I tried convert sub-arrays to string (tostring() method) and then use np.unique, but after transform to numpy array, it deleted last bytes of \x00, so I can't transform it back with np.fromstring(). Example: import numpy as np a = np.array([[[1,2],[3,4]],[[5,6],[7,8]],[[9,10],[11,12]],[[5,6],[7,8]]]) b = [x.tostring()

Two dimensional arrays and pointers

半城伤御伤魂 提交于 2019-12-01 16:41:15
I have the following code snippet: char board[3][3] = { {'1','2','3'}, {'4','5','6'}, {'7','8','9'} }; printf("address of board : %p\n", &board); printf("address of board[0] : %p\n", &board[0]); Both printf() statements all print the same value: 0x0013ff67 As per my knowledge, board (i.e) array name represents the address of the first subarray (i.e) board[0] and board[0] represents the address of first element in the first array (i.e) board[0][0] Why am I getting the same address in all my printf() statements? I expect different addresses for both statements. I am pretty new to this stuff and

Get the sub array in a bidimensional array having a particular key/value pair

天涯浪子 提交于 2019-11-30 22:53:23
I have a large PHP array, similar to: $list = array( array( 'id' = '3243' 'link' = 'fruits' 'lev' = '1' ), array( 'id' = '6546' 'link' = 'apple' 'lev' = '2' ), array( 'id' = '9348' 'link' = 'orange' 'lev' = '2' ) ) I want to get the sub-array which contains a particular id . Currently I use the following code: $id = '3243' foreach ($list as $link) { if (in_array($id, $link)) { $result = $link; } } It works but I hope there is a better way of doing this. You can write $link['id']==$id instead of in_array($id, $link) whitch will be less expensive. add a break; instruction after $result = $link;

How to use MPI_Type_create_subarray?

帅比萌擦擦* 提交于 2019-11-30 17:28:05
It is clear that its arguments are: int MPI_Type_create_subarray( int ndims, int array_of_sizes[], int array_of_subsizes[], int array_of_starts[], int order, MPI_Datatype oldtype, MPI_Datatype *newtype ); However, I cannot understand how this method receives the original array which we want to split and where it returns the new subarray (as this method should return an integer). In other words, I simply would like to see a simple implementation of this method in C++, which I am not able to find on the Internet. Jonathan Dursi MPI_Type_create_subarray() neither takes an original array nor

How to get a subArray from Swift 2.0

时光怂恿深爱的人放手 提交于 2019-11-30 17:00:54
I keep trying to search for the proper way to get a sub array in Swift but I am missing something here. This code doesn't work because rowArray.append(row) throws an error that states. Cannot convert value of type 'ArraySlice<Int>' to specified type '[Int]' I can't figure out how to get an [Int] out of the main array or to convert ArraySlice<Int> to [Int]. I am guessing I am missing something simple but can't seem to find the answer from the docs. var rowArray = [[Int]]() var rangeStart = 0 let rangeLength = mapWidth for var index = 0; index < mapHeight; ++index{ rangeStart = tileIDs.count - (

How to use MPI_Type_create_subarray?

拈花ヽ惹草 提交于 2019-11-30 01:13:25
问题 It is clear that its arguments are: int MPI_Type_create_subarray( int ndims, int array_of_sizes[], int array_of_subsizes[], int array_of_starts[], int order, MPI_Datatype oldtype, MPI_Datatype *newtype ); However, I cannot understand how this method receives the original array which we want to split and where it returns the new subarray (as this method should return an integer). In other words, I simply would like to see a simple implementation of this method in C++, which I am not able to

How to get a subArray from Swift 2.0

…衆ロ難τιáo~ 提交于 2019-11-30 00:57:43
问题 I keep trying to search for the proper way to get a sub array in Swift but I am missing something here. This code doesn't work because rowArray.append(row) throws an error that states. Cannot convert value of type 'ArraySlice<Int>' to specified type '[Int]' I can't figure out how to get an [Int] out of the main array or to convert ArraySlice<Int> to [Int]. I am guessing I am missing something simple but can't seem to find the answer from the docs. var rowArray = [[Int]]() var rangeStart = 0

Frequency of each element of an array considering all contiguos subarrays

白昼怎懂夜的黑 提交于 2019-11-29 07:57:21
Consider an array A = [5,1,7,2,3] All contiguos subarrays = { [5], [1], [7], [2], [3], [5,1], [1,7], [7,2], [2,3], [5,1,7], [1,7,2], [7,2,3], [5,1,7,2], [1,7,2,3], [5,1,7,2,3] } Replace all the arrays in the above set with maximum element in it: set will look like this: { [5], [1], [7], [2], [3], [5], [7], [7], [3], [7], [7], [7], [7], [7], [7] } Frequency info: [5] -> 2, [1] -> 1, [7] -> 9, [2] -> 1, [3] -> 2 My Goal is to find the above frequency info. My Approach: First make a list of pair (x,y). x is element in A and its index is y. LIST : [ (5,1), (1,2), (7,3), (2,4), (3,5) ] Sort the

Frequency of each element of an array considering all contiguos subarrays

╄→гoц情女王★ 提交于 2019-11-28 01:45:47
问题 Consider an array A = [5,1,7,2,3] All contiguos subarrays = { [5], [1], [7], [2], [3], [5,1], [1,7], [7,2], [2,3], [5,1,7], [1,7,2], [7,2,3], [5,1,7,2], [1,7,2,3], [5,1,7,2,3] } Replace all the arrays in the above set with maximum element in it: set will look like this: { [5], [1], [7], [2], [3], [5], [7], [7], [3], [7], [7], [7], [7], [7], [7] } Frequency info: [5] -> 2, [1] -> 1, [7] -> 9, [2] -> 1, [3] -> 2 My Goal is to find the above frequency info. My Approach: First make a list of pair