time-complexity

Complexity of Array Operations

ぐ巨炮叔叔 提交于 2020-01-06 15:07:48
问题 So one of the topics in my comp sci class is concerning time complexity and using arrays and linked lists as a good way to compare certain operations and what container is better at doing so, so you can choose the appropiate data structure. I understand the reasoning behind most of the operations but I'm unsure about one and that is inserting and appending in an array. The worst case scenario for both of these is O(n). I believe I understand why inserting is O(n) because worst case, you

Generalizing O(log log n) time complexity

为君一笑 提交于 2020-01-06 08:05:24
问题 I want to generalise how we get O(log log n)` time complexity. Yesterday I asked this question, in which I came to know, this: for(i=1 ; i<n ; i*=2) { ... } leads to O(log n) time complexity. By multiplying by 2 in each iteration, we are essentially taking next power of 2: For O(log n) i=i*2 ============ 1*2 = 2^1 = 2 2*2 = 2^2 = 4 4*2 = 2^3 = 8 and so on So to get O(log log n) time complexity, I need to take next double power (I coined this term "double power" for sake of convenience),

Fetch all the duplicate records from a list without quadratic time complexity?

馋奶兔 提交于 2020-01-06 05:44:13
问题 Below is my list which contains the column names: country, gender & age. scala> funList res1: List[(String, String, String)] = List((india,M,15), (usa,F,25), (australia,M,35), (kenya,M,55), (russia,M,75), (china,T,95), (england,F,65), (germany,F,25), (finland,M,45), (australia,F,35)) My goal is to find the duplicate records with the combination of (country,age). Please note that I want to only fetch all the duplicate records and ignore others. And list should also contain other column values

Time complexity and integer inputs

折月煮酒 提交于 2020-01-06 03:29:06
问题 I came across a question asking to describe the computational complexity in Big O of the following code: i = 1; while(i < N) { i = i * 2; } I found this Stack Overflow question asking for the answer, with the most voted answer saying it is Log2(N). On first thought that answer looks correct, however I remember learning about psuedo polynomial runtimes, and how computational complexity measures difficulty with respect to the length of the input, rather than the value. So for integer inputs,

Is is possible to move from (a,b) to (c,d)

。_饼干妹妹 提交于 2020-01-05 04:21:06
问题 The problem was to output whether it is possible to move from a given point (a,b) to target (c,d) We are restricted to positive co-ordinates only The following two moves are possible (a,b) -> (a+b,b) (a,b) -> (a,b+a) For instance, (1,1) to (5,4) is True You can do the following: Using 2nd move 3 times, (1,1) -> (1,2) -> (1,3) -> (1,4) Using 1st move 1 time (1,4) -> (5,4) I came up with the following recursive method def move(a,b,c,d): if a==c and b==d: return True elif a>c or b>d: return

Time Complexity : Continuously summing the digits of a number until a single digit result

南楼画角 提交于 2020-01-04 09:28:22
问题 I am given a number N. Keep summing the digits until a single digit result. e.g 35252 ==> 17 ==> 8 I have written the following code : int digitSum(int n) { int sum = 0; int digit; while(n) { digit = n%10; n = n/10; sum += digit; } if(sum > 9) return digitSum(sum); else return sum; } Coming to the time complexity : I am given the range for N as 0 <= N <= 10^9. In worst case, with all 9s, the recursion will go max twice and I will get O(loglogn) In best case, with single digit N, complexity

HashMap vs. ArrayList insertion performance confusion

只谈情不闲聊 提交于 2020-01-04 09:14:11
问题 From my understanding a hashmap insertion is O(1) and for an arraylist the insertion is O(n) since for the hashmap the hashfunction computes the hashcode and index and inserts the entry and an array list does a comparison every time it enters a new element. 回答1: Firstly, an operation of complexity O(1) does not always take lesser time than an operation of complexity O(n). O(1) only means that the operation takes a constant time (which could be any value), regardless of the size of the input.

Solving a recurrence: T(n)=3T(n/2)+n

混江龙づ霸主 提交于 2020-01-04 05:52:27
问题 I need to Find the solution of the recurrence for n, a power of two if T(n)=3T(n/2)+n for n>1 and T(n)=1 otherwise. using substitution of n=2^m,S(m)=T(2^(m-1)) I can get down to: S(m)=2^m+3*2^(m-1)+3^2*2^(m-2)+⋯+3^(m-1) 2^1+3^m But I have no idea how to simply that. 回答1: These types of recurrences are most easily solved by Master Theorem for analysis of algorithms which is explained as follows: Let a be an integer greater than or equal to 1, b be a real number greater than 1, and c be a

Big-O of These Nested Loops

跟風遠走 提交于 2020-01-04 03:52:27
问题 I'm pretty new to the Big-O field, so bear with me here. I have been searching about it as much as I could but I still need a lot of work to fully understand it. I came across these nested for loops in a practicing exercises and there wasn't any solutions and they seem complicated. So, any help would be appreciated. 1) int sum=0; for(int i=0; i < n^2; i++) { // n+1 for(int j = n-1; j >= n-1-i; j–-) { // n(n+1)/2 ? sum = i+j; // n(n+1)/2 ? System.out.println(sum); // n(n+1)/2 ? } } Big-O = ? 2

Big-O of These Nested Loops

只愿长相守 提交于 2020-01-04 03:52:16
问题 I'm pretty new to the Big-O field, so bear with me here. I have been searching about it as much as I could but I still need a lot of work to fully understand it. I came across these nested for loops in a practicing exercises and there wasn't any solutions and they seem complicated. So, any help would be appreciated. 1) int sum=0; for(int i=0; i < n^2; i++) { // n+1 for(int j = n-1; j >= n-1-i; j–-) { // n(n+1)/2 ? sum = i+j; // n(n+1)/2 ? System.out.println(sum); // n(n+1)/2 ? } } Big-O = ? 2