1

Suppose I have an array like this : [ 1, 2, 3, 4, 5, 6, 1]

I want to get an output like 1 ==> 6 ( For element 1(0th index the match is found at index 6)

This means for 0th element the next match is found at 6th index.

If the input is [4,6,4,6] the output is 4 ==> 2 and 6 ==> 3

Since first match for 4 is found at index 2( which is four) and first match for six(2nd element) is found at 3rd index

If the time complexity is O(n2) the solution is pretty straight forward. I have tried to find the next greatest element with this code in O(n) using stack. But I couldn't find a way to do the same for next equal element.

import java.util.Scanner;
import java.util.Stack;

public class NextGreatestElement {
    public static void main(String[] args) {
        int[] inp = {1,4,6,2,39};
        Stack<Integer> stack = new Stack<>();
        stack.push(inp[0]);
        for (int i = 1; i < inp.length; i++) {
            int element = inp[i];
            if (element > stack.peek()) {
                System.out.println(stack.peek() + " ==> " + element);
                stack.pop();
                while (!stack.isEmpty()) {
                    System.out.println(stack.pop() + " ==> " + element);
                }
                stack.push(element);
            } else if (element < stack.peek()) {
                stack.push(element);
            }
        }
        while (!stack.isEmpty()) System.out.println(stack.pop() + " ==> " + (-1));
    }
}

Even Algorithm is enough. I don't need the code.

Bharat
  • 1,044
  • 15
  • 34

3 Answers3

2

It is not possible to implement it with a stack in O(n). You can take a HashMap and iterate from right to left saving the last occurrence of a number. It will give you O(n) average-case time complexity:

int[] inp = {1, 2, 3, 1, 2, 1, 3};
Map<Integer, Integer> h = new HashMap<>();
List<Integer> result = new ArrayList<>();

for (int i = inp.length - 1; i >= 0; --i) {
    int cur = inp[i];
    int next = -1;
    if (h.containsKey(cur)) {
        next = h.get(cur);
    }   
    h.put(cur, i);
    result.add(next);
}

for (int i = 0; i < inp.length; ++i) {
    System.out.println("Number " + inp[i] + " next occurence at index " + result.get(inp.length - i - 1));
}

Runnable version: http://ideone.com/i6Cx09

DAle
  • 8,990
  • 2
  • 26
  • 45
  • @bharath, take a look at this question https://stackoverflow.com/questions/4553624/hashmap-get-put-complexity – DAle Aug 10 '17 at 10:54
1

if i'm not mistaken if you want the element or get the element that occurs in array more than onece. You can do this.

This solution will be nlogn

  1. Sort the array (ascending)
  2. loop array then compare
  3. if a[i] == a[i+1] then list.add(a[i]);

something like this enter image description here

-1

Use a hashmap or hashtable

traverse from right to left, for the first occurrence input the value into the collection

collection_object.put(inp[i], ""+i)

when you encounter next time just change the value like

value = collection_object.get(inp[i])
v = value.split("==>")
value = v[0] + "==>" + i
collection_object.put(inp[i], value)
  • I can't understand your answer – Bharat Aug 10 '17 at 09:18
  • HashMap collection_object = new HashMap (); collection_object is a hashmap, where while iterating through the input array , when you encounter an element for the first time then just input the element with its index as key, value respectively, for the next time when you encounter the same element . use the second code – Dileep Kumar Aug 10 '17 at 09:19
  • omg finally i understood your question after reading all the comments, please be specify when you ask the question. – Dileep Kumar Aug 10 '17 at 09:25
  • Its not my fault... Someone edited my question and I thought its grammar correction but it has changed entire meaning – Bharat Aug 10 '17 at 09:28
  • @bharath, Do you understand my answer? – DAle Aug 10 '17 at 09:40
  • Dale your answer is clear than this but I think h.containsKey might produce O(n) resulting in O(n^2) over all – Bharat Aug 10 '17 at 09:49
  • @bharath, HashMap has `O(1)` find-key operation in average-case. The worst-case time could be worse, but that's not the case: we have integers as keys and one of the most tested implementations of a hash table. – DAle Aug 10 '17 at 10:47