All Questions
Tagged with nested-loopsalgorithm
126 questions
25votes
10answers
70kviews
Find out which combinations of numbers in a set add up to a given total
I've been tasked with helping some accountants solve a common problem they have - given a list of transactions and a total deposit, which transactions are part of the deposit? For example, say I have ...
20votes
7answers
14kviews
Creating N nested for-loops
Is there a way to create for-loops of a form for(int i = 0; i < 9; ++i) { for(int j = 0; j < 9; ++i) { //... for(int k = 0; k < 9; ++k) { //N-th loop without knowing N at the ...
8votes
5answers
1kviews
Trying to optimize my code to either remove nested loop or make it more efficient
A friend of mine takes a sequence of numbers from 1 to n (where n > 0) Within that sequence, he chooses two numbers, a and b He says that the product of a and b should be equal to the sum of all ...
8votes
3answers
419views
efficient algorithm instead of looping
I have a data set where each samples has a structure similar to this X=[ [[],[],[],[]], [[],[]] , [[],[],[]] ,[[][]]] for example: X=np.array([ [ [1,2,3], [2,4,5] ,[2,3,4] ] , [ [5,6], [6,6] ] , [[2,...
6votes
3answers
4kviews
Time complexity for dependant nested for loop?
Can you explain me how to find time complexity for this? sum=0; for(k=1;k<=n;k*=2) for(j=1;j<=k;j++) sum++; So, i know the outer loop has time complexity of O(logn), but since the ...
5votes
4answers
13kviews
Iteration counter for double loops
I am trying to find the formula to count every iteration of this double for loop (in python for instance): for i in range(5): for j in range(5): count = MYSTERIOUS_FORMULA print ...
4votes
3answers
441views
Determining as a function of n how often the statement incrementing the variable count is performed
Ok so I'm new to analyzing algorithms and would really appreciate any helpful tips that can be shared on how to go about this. I am trying to determine how many times count is incremented as a ...
4votes
2answers
215views
Variable Number of Nested While Loops with Index Increment Limits Based on External Logic
In Javascript, I am attempting to iterate through an array of indices indexArray. This array can be any length, but for ease of explanation, I will use length 3 indexArray.length = 3. I have an ...
3votes
2answers
1kviews
Optimal solution for the maximum single sell profit algorithm
The input array is: A[0] = 23171 A[1] = 21015 A[2] = 21123 A[3] = 21366 A[4] = 21013 A[5] = 21367 Mission is to find maximum profit. E.g A[3] - A[2] = 243 and my code is: class Solution { ...
3votes
1answer
190views
Why are my nested for loops taking so long to compute?
I have a code that generates all of the possible combinations of 4 integers between 0 and 36. This will be 37^4 numbers = 1874161. My code is written in MATLAB: i=0; for a = 0:36 for b= 0:36 ...
3votes
2answers
147views
How do "inner" and "outer" work in this bubble sort code?
I am trying to learn the bubble sort algorithm in a book for C. I can't seem to understand in the code below how int outer and int inner link to which element of the nums array. Like how does inner ...
3votes
3answers
626views
How to determine how often a statement in a nested loop is executed?
I am working through a section of a text on determining complexity of nested loops using recurrence relations. In this particular example I am trying to determine how many times the count variable ...
3votes
1answer
287views
Determining the big-O of three nested for loops with if statment
What is the big-O for the following code : y=1; x=3; for(int i =1 ; i < =n ; i*=2) for(int j =1; j<= i * i; j++) if (i % j == 0) for(int k = 1; k<=j; k++) y=y*...
2votes
1answer
12kviews
How is Big-O of Depth-First-Search = O(V+E)? [duplicate]
I am trying to understand how/why complexity of DFS is O(V+E). Here is my attempt at analysing complexity of pseudo-code iterative DFS. DFS(G, t) { 1 stack S = new empty Stack of size G.|V| ... O(...
2votes
5answers
1kviews
big oh of algorithm with nested loop
find the big oh characterization input: n s<-0 for i<-1 to n^2 do for j<-1 to i do s<-s+i Both loops iterate n^2 times. I'm not sure if I should add or multiply the loops. ...