[英]What is the most efficient algorithm to find repeated elements in an array in time complexity O(n) and space complexity O(1)?
[英]Time complexity for storing array elements
這是我的程序,我可以部分地獲得時間復雜度,任何人都可以幫助我:
我可以得到其中的一些,但是任何人都可以驗證我的方法是否正確
package sd;
public class Max {
public static void main(String args[]) {
int i;
int large[] = new int[5];
int array[] = { 33, 55, 13, 46, 87, 42, 10, 34, 43, 56 };
int max = 0, index = 0;
// O(5)
for (int j = 0; j < 5; j++) {
max = array[0]; // Assuming max to be first element
// Comparing 1st element with max O(n)
for (i = 1; i < array.length; i++) {
if (max < array[i]) {
max = array[i]; // Replace if greater
index = i;
}
}
large[j] = max;
array[index] = Integer.MIN_VALUE; // Find max and replace with least
// possible value to avoid
// duplicate max
System.out.println("Largest 5 amoung 10 : " + large[j]); // Time
// complexity:
// O(5)
// *
// O(n)
// =
// O(n)
}
}
}
這里的復雜度是O(5N)
。
有關查找復雜性的更多信息,請檢查此問題 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.