Arrays 如何在一个很大的数组中获得最大数量的连续整数(分布在多台机器上)

Arrays 如何在一个很大的数组中获得最大数量的连续整数(分布在多台机器上),arrays,distributed-computing,distributed-system,system-design,Arrays,Distributed Computing,Distributed System,System Design,我在一次采访中被问到这个问题。第一部分相当简单,我必须编写一个代码来获得数组中最大数量的连续整数。以下是我编写的代码: int count = 0, max = 0; for(int i = 1; i < array.length; i++) { if((array[i - 1] + 1) == array[i])) //curr is consecutive to prev count++; else count = 0; //re

我在一次采访中被问到这个问题。第一部分相当简单,我必须编写一个代码来获得数组中最大数量的连续整数。以下是我编写的代码:

int count = 0, max = 0;
for(int i = 1; i < array.length; i++) {
    if((array[i - 1] + 1) == array[i])) //curr is consecutive to prev
          count++;
    else
          count = 0; //reset the counter as sequence is broken

    //Keep track of maximum
    if(count > max)
         max = count;
}

System.out.println(max); //print the length of largest consecutive integers
int count=0,max=0;
for(int i=1;i最大值)
最大值=计数;
}
系统输出打印项次(最大值)//打印最大连续整数的长度
第二部分是跟进问题:


如何修改此逻辑以适用于存储在多台计算机中的阵列?

您可以使用

Python中的示例(很抱歉命名错误):


您可以使用

Python中的示例(很抱歉命名错误):


假设我们按顺序从左到右将整个数组分配给每台机器。例如,对于只有两台机器(
machine1
machine2
),我们将分发数组
0。。。。i
机器1
i+1…n
机器2
。从每台机器上,我们可以返回几个附加信息以及本地最大值

class result {
    public int machineId;
    public int maxSoFar; // the max value as your code
    public int leftElement; // the leftmost element
    public int leftLength; // number of times the leftElement appears consecutively in left
    public int rightElement; // the rightmost element
    public int rightLength;  // number of times the rightElement appears consecutively in right
};
在合并两台机器的结果时,对于
machineId
连续的任何两台机器(例如3和4),我们可以这样最大化-

return Math.max(((machine1.rightElement == machine2.leftElement) ? machine1.rightLength + machine2.leftLength : 0), 
                  Math.max(machine1.maxSoFar, machine2.maxSoFar));

假设我们按顺序从左到右将整个数组分配给每台机器。例如,对于只有两台机器(
machine1
machine2
),我们将分发数组
0。。。。i
机器1
i+1…n
机器2
。从每台机器上,我们可以返回几个附加信息以及本地最大值

class result {
    public int machineId;
    public int maxSoFar; // the max value as your code
    public int leftElement; // the leftmost element
    public int leftLength; // number of times the leftElement appears consecutively in left
    public int rightElement; // the rightmost element
    public int rightLength;  // number of times the rightElement appears consecutively in right
};
在合并两台机器的结果时,对于
machineId
连续的任何两台机器(例如3和4),我们可以这样最大化-

return Math.max(((machine1.rightElement == machine2.leftElement) ? machine1.rightLength + machine2.leftLength : 0), 
                  Math.max(machine1.maxSoFar, machine2.maxSoFar));