-1

How can i get value value 11 and 15 because there is a missing values 12,13,14.I have got snippet from this SO post:

I could not use it for array with no fixed size.

       int binaryValue[] = null;
       ArrayList<Integer> al; 

        Vector father = new Vector();
        Vector child;
        int start = al.get(0);
          for(int i = 0; i < al.size(); i++) {
         if(al.get(i) != i + start) {
         child = new Vector();
         child.add(al.get(i-1));
         child.add(al.get(i));
         father.add(child);
    }
    }
Child should have 11 and 15 and so on ....
  • It is not clear what is your question. Could you describe please your problem in detail? – Vasiliy Vlasov May 01 '17 at 19:01
  • 2
    An array with no fixed size is a `List`. Are you having trouble [correlating the operations](http://stackoverflow.com/q/23730092/1079354) across? – Makoto May 01 '17 at 19:02
  • i have an ArrayList having some data value according to result by computation. Consider it as {2,3,4,5,7,8} there is amissing value 6. how can i get 5 and 7. – sumit gautam May 01 '17 at 19:04
  • So you're asking us to give you the algorithm on how to find the gaps in an array of consecutive values (in your example case, 11 and 15)? What have you tried? I don't see any work that you've done yourself to find a solution ... – AntonH May 01 '17 at 19:05
  • 1
    What's wrong with `hasGaps(new long[]{9, 10, 11, 15})`? – 9000 May 01 '17 at 19:06
  • @9000 I believe OP is looking on how to find the values of 11 and 15, i.e. the values bordering the gap (waiting for confirmation on this, however). – AntonH May 01 '17 at 19:07
  • @AntonH i modified question..my code snippet is there. – sumit gautam May 01 '17 at 19:18
  • @VasiliyVlasov please see above i modified question – sumit gautam May 01 '17 at 19:20
  • 1
    What is "array with no fixed size"? You mean an `ArrayList`? The only difference is using the methods `ArrayList` provides to access the array. Other than that this seems pretty much like a duplicate of the question you linked. – Bernhard Barker May 01 '17 at 19:45
  • 1
    I would also advise to stay with ONE concept instead of three: if you don't understand the workings of arrays versus lists, then for sure it will not help you to further confuse yourself with vectors. – GhostCat May 01 '17 at 19:47

1 Answers1

0

This is pure logic based issue, you just need to look for next element to find the gap, see below working sample:

public class GapFinder {
    private static int[] myIntArr = {9, 10, 11, 12, 13, 15};

    public static void main(String[] args) {
        int a = 0,b = 0;
        for (int i = 0; i < myIntArr.length; i++) {
            if(((i + 1) < myIntArr.length) && myIntArr[i] != myIntArr[i + 1] - 1) {
                a = myIntArr[i];
                b = myIntArr[i + 1];
                break;
            }
        }
        if(a == 0 && b == 0){
            System.out.println("There were no gaps.");
        } else{
            System.out.println("Gaps: " + a + " | " + b);
        }
    }
}

Please note:

  • This is not a concept which I could possibly explain, this is pure logic and I have said this in my opening statement, and logic is also straight forward which needs no explaination, its quiet implicit and not a complex algorithmic logic; OP just needed to loop for next element to identify the gap, until next/previous element is checked, a gap cannot be identified with getting expected values, this was just a small step OP was missing and OP got it
  • This is a working solution, there could be corner cases like what @9000 mentioned in his comment, it is basic assumption that I cannot provide answer handling all corner cases, I am just highlighting the logic which would help solve the problem.
hagrawal7777
  • 14,103
  • 5
  • 40
  • 70
  • 1
    I don't understand why people down vote without reasons! – hagrawal7777 May 01 '17 at 19:23
  • 3
    You've only got code there. No explanation as to what the OP's actual problem is. No guidance on why this works. Just *code*. If you can't explain the solution, you really shouldn't answer. – Makoto May 01 '17 at 19:24
  • Consider also input arrays of length 0 and 1. – 9000 May 01 '17 at 19:24
  • @Makoto Buddy read my opening statement - "*This is pure logic based issue, you just need to look for next element to find the gap*" .. This is pure logic, there is no concept which I could have possibly explained and I have explained what OP should have done. – hagrawal7777 May 01 '17 at 19:25
  • @9000 I am not giving a full fledged solution, this is no my problem, there type of minor things can be handled by OP. – hagrawal7777 May 01 '17 at 19:26
  • I *maintain*, if you're not willing to explain why this solution works and what the actual *is*, it becomes less practical for me to validate your solution. Further, you enable the OP and others to simply copy this code verbatim into their project without actually understanding what's going on. It may be my personal penchant, but I would genuinely appreciate it if you did a job of explaining your answer. – Makoto May 01 '17 at 19:29
  • `(i + 1) < myIntArr.length` can be the for-loop condition, no need to check that in the if statement as well as checking `i < myIntArr.length`. – Bernhard Barker May 01 '17 at 19:32
  • @hagrawal it worked thanks...but a minor suggestion it works for single gap.if we have more than 1 gaps it should be replaced "break" by "continue". – sumit gautam May 01 '17 at 19:32
  • @Makoto Let me start by saying that I agree in general with you that we should try to explain the concept/reasons/logic, but I don't know how to explain it better than OP just needed to look for next element, this is really a pure logic, and if someone is coding then I would assume he can understand this kind of basic logic, person may not get it on his/her own but when someone put a code it is not hard to understand a logic. I would personally never ever explain a simple logic like this. – hagrawal7777 May 01 '17 at 19:32
  • I agree with @Makoto. Answering with a coded solution is not beneficial to OP or the answerer. Nobody ends up learning in this situation. – Shiraaz.M May 01 '17 at 19:33
  • @sumitgautam Like I said there could be corner cases, I am sure you can handle that, I think you were just missing a step and you have got that now to get your solution working. – hagrawal7777 May 01 '17 at 19:34
  • 1
    @Dukeling If I don't do that I would end looking beyond array size because I am doing `myIntArr[i + 1]` and it will cause array out of bound exceptiion, to be precise this would occur when I am at last index of array. – hagrawal7777 May 01 '17 at 19:37
  • @hagrawal its better include that in your answer with some comments so that other can easily understand...any way thank you very much i spend more than 2hrs for this problem... – sumit gautam May 01 '17 at 19:38
  • @Shiraaz.M We all have opinions and I respect each ones, but my opinion is that I need to explain a simple logic like this to the OP, OP was just missing a step and he has got it, this was not a complex algorthmic logic which i needed to explain. – hagrawal7777 May 01 '17 at 19:39
  • @hagrawal I'm not saying you should remove it, I'm saying you should make it the for-loop condition. You check `i < myIntArr.length` and then you check `(i + 1) < myIntArr.length` right afterwards - the first check is pointless. – Bernhard Barker May 01 '17 at 19:41
  • @hagrawal No worries! It's okay to have different opinions on the same thing :) – Shiraaz.M May 01 '17 at 19:41
  • @Dukeling I am sure OP can handle these kind of minors, my intention was to highlight missing step and not full fledged solution, I have written this in my answer. – hagrawal7777 May 01 '17 at 19:44
  • 1
    Just to alter your `for loop` to make it simple: `for (int i = 0; i < myIntArr.length-1; i++) { if(myIntArr[i+1] - myIntArr[i]!=1) { a = myIntArr[i]; b = myIntArr[i + 1]; break; } }` – FSm May 01 '17 at 19:55