fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // Naive function to find minimum index of repeating element
  5. int findMinIndex(int arr[], int n)
  6. {
  7. // for each element arr[i], search it in sub-array arr[i+1..n-1]
  8. // and return as soon as first repeated occurrence is found
  9. for (int i = 0; i < n - 1; i++)
  10. {
  11. for (int j = i + 1; j < n; j++)
  12. {
  13. // if the element is seen before, return its index
  14. if (arr[j] == arr[i])
  15. return i;
  16. }
  17. }
  18.  
  19. // invalid input
  20. return n;
  21. }
  22.  
  23. // main function
  24. int main()
  25. {
  26. int arr[] = { 5, 6, 3, 4, 3, 6, 4 };
  27. // int arr[] = { 1, 2, 3, 4, 5, 6 };
  28.  
  29. int n = sizeof(arr) / sizeof(arr[0]);
  30.  
  31. int minIndex = findMinIndex(arr, n);
  32.  
  33. if (minIndex != n)
  34. cout << "Minimum index of repeating element is " << minIndex;
  35. else
  36. cout << "Invalid Input";
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0s 15224KB
stdin
Standard input is empty
stdout
Minimum index of repeating element is 1