Customized metadata of a table and its variables, specified as a CustomProperties object. The CustomProperties object is a container for customized metadata that you can add to a table. By default, CustomProperties has zero properties. Each property you add to CustomProperties can contain either table metadata or variable metadata. If a property contains variable metadata, then its value must be an array, and the number of elements in the array must equal the number of table variables.
Note: You can add or remove only properties for customized metadata using addprop and rmprop. You cannot add or remove properties of the tableName.Properties object. ExampleCreate a table. load patients T = table(Gender,Age,Height,Weight,Smoker,Systolic,Diastolic); Add properties that can hold customized metadata about the table and its variables. In this example, the metadata are names of instruments, true and false values indicating whether variables are to be plotted, and the name of an output file. To add properties, use the addprop function. T = addprop(T,{'Instrument','ToPlot','OutputFile'},{'variable','variable','table'}); T.Properties ans = TableProperties with properties: Description: '' UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {1x7 cell} VariableDescriptions: {} VariableUnits: {} VariableContinuity: [] RowNames: {} Custom Properties (access using t.Properties.CustomProperties.<name>): OutputFile: [] Instrument: [] ToPlot: [] Assign values to the customized metadata using dot syntax. When you assign an array of text values to customized metadata, the best practice is to use a string array, not a cell array of character vectors. If a property of CustomProperties is a cell array of character vectors, then there is no mechanism to prevent you from later assigning nontext values as elements of the cell array. T.Properties.CustomProperties.Instrument = ["","","height rod","scale","","blood pressure cuff","blood pressure cuff"]; T.Properties.CustomProperties.ToPlot = [false false true true false true true]; T.Properties.CustomProperties.OutputFile = 'patientData.csv'; T.Properties ans = TableProperties with properties: Description: '' UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {1x7 cell} VariableDescriptions: {} VariableUnits: {} VariableContinuity: [] RowNames: {} Custom Properties (access using t.Properties.CustomProperties.<name>): OutputFile: 'patientData.csv' Instrument: ["" "" "height rod" "scale" ... ] ToPlot: [0 0 1 1 0 1 1] Remove the OutputFile property from T. T = rmprop(T,'OutputFile'); T.Properties ans = TableProperties with properties: Description: '' UserData: [] DimensionNames: {'Row' 'Variables'} VariableNames: {1x7 cell} VariableDescriptions: {} VariableUnits: {} VariableContinuity: [] RowNames: {} Custom Properties (access using t.Properties.CustomProperties.<name>): Instrument: ["" "" "height rod" "scale" ... ] ToPlot: [0 0 1 1 0 1 1] Problem Description: Given an array of n integers and given a number K, determines whether there is a pair of elements in the array that sums to exactly K. For example : Input : A[] = [-5, 1, -40, 20, 6, 8, 7 ], K=15 Output: true ( 7, 8 and -5, 20 are the pairs with sum 15) Input : A[] = [-5, 4, -2, 16, 8, 9], K=15 Output: false (There is no pair of elements whose sum is equal to 15) Possible follow-up questions to ask the interviewer :
We are discussing four ways to solve this problem :
Use two loops and check A[i] + A[j] == K for each pair (i, j) in A[]. If there exists a pair with sum equals to K then return true. By end of both loops, If you didn’t find such a pair then return false. Pseudo Codeint find_sumPair (A[], n, K) { for( i = 0 to n-1 ) { for(j = i+1 to n-1) { if(A[i]+A[j] == K) return 1 } } return -1 } Complexity Analysis The total no. of comparison in worst case = Total no. of possible pairs = nC2 = n(n-1)/2 = O(n²) Time Complexity = O(n²) and Space Complexity = O(1). Can we improve the time complexity? Let's try to think about it. The idea of binary search works perfectly in the case of the sorted array. We can sort the A[] and for each value A[i], search whether there is another value K-A[i] present in the array or not. Binary search performs searching in O(logn) which could help us to improve the time complexity. Solution Steps
Pseudo Codeint find_sumPair (A[], n, K) { Sort ( A, n) for( i = 0 to n-1 ) { x = binarySearch ( A, 0, n-1, K-A[i] ) if ( x ) return 1 } return -1 } Complexity Analysis Suppose we are using heap sort/merge sort and iterative binary search for the implementation. Time Complexity = Time complexity of sorting + n. Time complexity of Binary Search = O(nlogn) + n. O(logn) = O(nlogn) Space Complexity = Space Complexity of sorting + Space complexity of the binary search (Iterative Implementation) If merge sort ,Space Complexity = O(n) + O(1) = O(n) If Heap Sort, Space Complexity = O(1) + O(1) = O(1) Critical Ideas to think!
Sort the array A[] then walk two pointers inward from the ends of the array, at each point looking at their sum. If it is exactly k, then we are done. If it exceeds k, then any sum using the larger element is too large, so we walk that pointer inwards. If it is less than k, then any sum using the lower element is too small, so we walk that pointer inwards. Solution Steps
3) Run a loop while i< j.
4) If didn’t find such a pair in the whole array - return false. Solution Visualization Pseudo Codeint find_sumPair( A[], n, K) { Sort(A, n) i = 0 j = n -1 while (i < j) { if(A[i] + A[j] == K) return 1 else if(A[i] + A[j] < K) i = i+1 else j = j-1 } return -1 } Complexity Analysis Suppose we are using Heap Sort or merge sort. Time Complexity = Time complexity of sorting + Time complexity of two pointer approach (while loop) = O (n log n) + O(n) = O (n log n) If merge sort, Space Complexity = O(n) If Heap Sort, Space Complexity = O(1) Critical Ideas to think!
Idea is to use the Hash Table to improve the time complexity because it performs searching and insertion efficiently in O(1) average. Solution Steps
3. If you didn’t find such a pair by end of the loop then return false. Pseudo Codebool checkPair(int A[], int n, int K) { Take Hash Table H of size O(n) for (i = 0 to n-1) { int x = K - A[i] if (H.search(x) is true) return 1 H.insert(A[i]) } return -1 } Complexity Analysis In worst case scenario, we scan the whole array and didn't find any such pair. Time Complexity = Time complexity of inserting n elements in hash table + Time complexity of searching (K-A[i]) n times in hash table = n. O(1) + n . O(1) = O(n) Space Complexity = Extra space of Hash Table = O(n) Critical Ideas to think!
Please write comments if you find any error or bug. Happy Coding! Enjoy Algorithms! |