How does each element in the output array of a DFT correspond to a specific frequency?

I have a basic understanding of the Fourier Transform, though I’m trying to use it in a program and I’m confused on the specifics. Based on source code I can find online, the DFT takes a set of samples/numbers, performs a summation for each term, and returns a set of these summations which is the same size as the input set. Suppose I have a periodic function. As I understand it, the output array should contain the amplitudes/weights of each frequency which sum to that function. What I can’t figure out is how each frequency is encoded in the array as just an index. In each example I read, we just assign a summation at each iteration of the inner loop to the next consecutive index in the output array. How are these indices indicative of which frequency they correspond to?

I’m attaching the source code I’m referencing to the bottom of this in case the website I linked to ever goes down.

import cmath def compute_dft_complex(input):     n = len(input)     output = []     for k in range(n):  # For each output element         s = complex(0)         for t in range(n):  # For each input element             angle = 2j * cmath.pi * t * k / n             s += input[t] * cmath.exp(-angle)         output.append(s)     return output 

Interview q: Small possible length of stick from an array of stick lengths

I was asked this question in a phone interview recently and I bombed it completely. Zero clue how to approach it. I wasn’t able to find any similar patterns on google-ing. Thought maybe folks here might be able to help?

Statement: Given m sticks with different lengths. Combine these sticks to form longer sticks with the same length. What’s the smallest possible length of these newly unified sticks?


  • Must use all sticks
  • m < 50
  • max length of single stick less than 20


Input: 5 2 1 5 2 1 5 2 1 Output: 6 (Process: 1+5, 1+5, 1+5, 2+2+2) 

Input: 3 3 3 2 2 5 Output: 9 (Process: 3+3+3, 2+2+5) 

Input: 1 2 3 4 5 Output: 5 (Process: 2+3, 1+4, 5) 

Input: 1 3 4 5 Output: 13 (Process: 1+3+4+5) 

simplyfing common data values (parts of a string) in an array – by merging same attributes (data values)

I have arrays comprised of string elements like the following 2 examples. Each of the following lines is a string element of the array.


aaa bbb cc1 dd1  aaa bbb cc1 dd2  aaa bbb cc2 dd1  aaa bbb cc2 dd2  aaa bbb cc3 dd1  aaa bbb cc3 dd2  


bbb rrr nnn ttt ooo eee ddd fff contr  bbb sss nnn ttt ppp eee contr  bbb sss nnn aaa ooo eee ddd fff contr  bbb rrr nnn ttt yyy eee ddd fff contr  

I want to simplify and remove "redundant" lines by merging in a single line duplicate attributes. So the results should be:


aaa bbb cc1 dd1/dd2 aaa bbb cc2 dd1/dd2 aaa bbb cc3 dd1/dd2 


bbb rrr nnn ttt ooo/yyy eee ddd fff contr  bbb sss nnn ttt ppp eee contr  bbb sss nnn aaa ooo eee ddd fff contr  

(the results are an array as well).

My current approach goes like this: remove the first column then compare all elements. if equal lines are found, merge them. remove second column, compare. It becomes though somewhat complicated, as not all lines have the same number of data values (separated by spaces).

I’m stuck here. Any help would be welcome.

Reconstructing an Array via Time-Intensive Subset Queries

I am trying to design an algorithm for a problem, and the following is an auxiliary problem for which a good solution would imply a faster algorithm for the original problem.

I am given access to an array of numbers. However, I am only allowed to query it by specifying an arbitrary subset of indices, in response to which I am then given the sum of the elements at those positions. These queries are quite costly, specifically they run in time $ \tilde{O}(n^2)$ time (where $ \tilde{O}(\cdot)$ hides polylogarithmic factors). I want to determine the element at each index in the array (i.e. reconstruct the array) using as little time as possible.

Of course, it is possible to do this by querying each element on its own. This algorithm does $ n$ queries and hence has total running time $ \tilde{O}(n^3)$ . I am wondering if there is a faster way. Adaptivity does not help with this problem, so any algorithm would have two steps: First, it executes a fixed sequence of queries, and then reconstructs all elements using the query answers. Ideally, both steps run in time $ o(n^3)$ . So far, any set of $ o(n)$ queries that I looked at makes recovery impossible. This might be the case for any such set of queries (and my intuition screams that this is probably the case), but I cannot see a proof for this.

I’d love an answer that either shows a faster algorithm or proves that $ o(n)$ queries are impossible, but answers with partial insights would also be great.

approximate line segments from array of unsorted points

Sample Scenario

The polygon above is actually a collection of a lot of black points closely packed together. I want to approximate these black points as straight line segments. The black points are not sorted in any order.

What I’m doing right now is sort these points by selecting any random black point and recursively finding the next closest point until no more points are left, the problem with this method is it might produce inaccurate results, if there is a small gap of points somewhere on the shape or dense lump of points somewhere on the shape. After sorting the points by this algorithm I run Douglas–Peucker algorithm to obtain the line segments. Am I doing it right? How can I approach this problem better?

Help understanding range function and array indexing [closed]

I’m working on understanding the below solution of rotating an array of integers in Python:

  1. Does the range function start counting at 0?
  2. Do we assume array indexing starts goes 0,1,2… or 1,2,3…?
  3. Why do we need to transfer the rotated values from the new array a to the old array nums (last line)?


class Solution:     def rotate(self, nums: List[int], k: int) -> None:         n = len(nums)         a = [0] * n         for i in range(n):             a[(i + k) % n] = nums[i]                      nums[:] = a 

It is supposed to work as follows: Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4]

Construct a array using following rules

Consider there is a array $ a$ of length $ n$ , $ a=[a_i ,0 \leq i \leq n]$ and all $ a_i>0$ and I need to construct a array $ b$ of length $ n$ (if possible) , $ b=[b_i , 0\leq i \leq n]$ .Rules for making array b is all $ 1\leq b_i\leq 5$ .

Consider $ j=i+1$ ,if $ a_j>a_i$ then $ b_j>b_i$ ,if $ a_j<a_i$ then $ b_j<b_i$ and if $ a_i=a_j$ then $ b_i \neq b_j$ .

Example : $ a=[2,3,4]$ then $ b$ can be $ [1,2,3]$ ,any $ b$ is $ accepted$ .

My approach is to use a simple $ dynamic$ $ programming$ ,$ dp[i][j]$ where i will check if first $ i$ elements of $ b$ can be formed if $ b_i=j$ where $ 0<j<6$ if it is possible $ dp[i][j]=1$ else ,$ dp[i][j]=0$ .By this method i could find if answer exits but i could not find the array $ b$ ,I know array $ b$ can be formed by depth first search as we can see states are connected by i have no idea after that.Could anyone help me how to use dfs on this question.

Soure : link

Find original array from array with pairs of adjacent elements

Given an array composed of pairs, like this:


Each element in the array (call it pair) means that pair[0] and pair[1] are adjacent in the original array. Note, they can come in either order. For instance, for the sample array above, the original array would be:

4,1,5,3,2 (or the reversed version of this)

How can I do this quickly? I tried doing this as follows, which works, but it’s too slow:

Create a hashmap that maps adjacent elements. Map = {3: [5,2], 1: [5,4], 5: [1,3], 4: [1], 2:[3]}. My algorithm would then start with one of the keys that only has a corresponding value length of 1 (in this case either 4 or 2), and then add to an output array, and go through the hashmap. I.e. First I would add 4 to my output, and then go from key of 4 (with a corresponding value of 1), to the key of 1, with corresponding values of 5 and 4. I’d ignore 4 (since it’s already in output), and add 5, then go to the key of 5, and so on and so forth. This is too slow! Is there a better algorithm?

Does anyone know what this encoding format for passwords is? I think it is a decimal array but I can’t seem to convert it

During a penetration test, I ran across a server that was storing passwords in its database in what seems to be a binary array of sorts:

password_table  1,10,11,21,21,11,21,13,00,00,00,000 11,61,19,11,46,108,09,100 110,118,100,107,108,117,123,62,108,108,62,62 

(slightly edited for confidentiality)

The server in question is a Tomcat server and the application is running a Java program. I considered that this might be a array of sorts but I can’t seem to convert these arrays into anything readable or usable. Does anyone have any ideas?