top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Longest subarray whose elements form a continuous sequence?

+3 votes
126 views
package com.stackoverflow;

import java.util.Arrays;
import java.util.Comparator;

public class ContSubArray {
    public static void main(String[] args) {

        int[] array = 
                    //{10, 5, 3, 1, 4, 2, 8, 7};
                    //{10, 5, 24, 3, 1, 4, 2, 8, 7};
                    //{10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1};
            {4, 5, 1, 5, 7, 6, 8, 4, 1};

        E[] ext = new E[array.length];
        for (int i = 0; i < array.length; i++) {
            E e = new E();
            ext[i] = e;
            e.e = array[i];
            e.idx = i;
        }

        System.out.println("Input: " + Arrays.toString(array));
        System.out.println("Length: " + find(ext));
    }

    static int find(E[] array) {
        // sort input (can be done in O(n) for integers)
        Arrays.sort(array, new Comparator<E>() {
            @Override
            public int compare(E o1, E o2) {
                return o1.e - o2.e;
            }
        });

        System.out.println(Arrays.toString(array));

        int maxN = 1; 
        int sum = 0;
        int groupMin = Integer.MAX_VALUE;
        int curN = 0;
        int prev = -1; // none
        for (int i = 0; i < array.length; i++) {
            if (curN == 0) {
                sum = array[i].idx;
                groupMin = array[i].idx;
                prev = array[i].e;
                curN++;
            } else {
                if (array[i].e != prev + 1) {
                    curN = 1;
                    prev = array[i].e;
                    sum = array[i].idx;
                    groupMin = array[i].idx;
                } else { // continuous
                    groupMin = Math.min(groupMin, array[i].idx);
                    curN++;
                    sum += array[i].idx;
                    int estSum = estSum(groupMin, curN);
                    prev = array[i].e;
                    if (estSum == sum) {
                        maxN = Math.max(curN, maxN);
                    }
                }
            }
        }

        return maxN;
    }

    static int estSum(int a, int n) {
        return a * n + (n * (n - 1)) / 2;
    }

}

class E {
    int e;
    int idx;

    public String toString() {
        return "[" + e + ", " + idx + "]";
    }
}
posted Apr 30, 2016 by anonymous

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button LinkedIn Share Button

Similar Questions
+3 votes

Input: arr[] = {5, 5, 10, -10, -20, 40, 50, 35, -20, -50}
Output: 125 (40, 50, 35)

0 votes

How to find longest consecutive path in a binary tree using C/C++/Java?

0 votes

Given a string S and set of strings U. Find the length of longest prefix of S that can be formed using strings in U.

For example you have following -

S= “absolute”
U={“abs”,”xyz”,”ol”,”te”}

Answer: 5 i.e "absol"

Now the task is to have the algorithm/program for the above problem, please help me with this?

...