Advertisement
annie_02

Untitled

Jan 5th, 2022
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int longestDist(const int arr[], const int arrSize) {
  5. int first_index, second_index, difference, result = 0;
  6. if (arrSize < 2) {
  7. return -2;
  8. }
  9. else {
  10. for (int i = 0; i < arrSize - 1; ++i) {
  11. int temp = arr[i];
  12. first_index = i;
  13.  
  14. for (int j = i + 1; j < arrSize; ++j) {
  15.  
  16. if (temp == arr[j]) {
  17. second_index = j;
  18. difference = second_index - first_index;
  19.  
  20. if (difference > 0) {
  21. result = first_index;
  22. return result;
  23. }
  24. }
  25. }
  26. } return -1;
  27. }
  28. }
  29.  
  30. int main() {
  31. int sizeArray;
  32. cin >> sizeArray;
  33. int arr[100];
  34. for (int i = 0; i < sizeArray; ++i) {
  35. cin >> arr[i];
  36. }
  37. cout << longestDist(arr, sizeArray);
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement