Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int longestDist(const int arr[], const int arrSize) {
- int first_index, second_index, difference, result = 0;
- if (arrSize < 2) {
- return -2;
- }
- else {
- for (int i = 0; i < arrSize - 1; ++i) {
- int temp = arr[i];
- first_index = i;
- for (int j = i + 1; j < arrSize; ++j) {
- if (temp == arr[j]) {
- second_index = j;
- difference = second_index - first_index;
- if (difference > 0) {
- result = first_index;
- return result;
- }
- }
- }
- } return -1;
- }
- }
- int main() {
- int sizeArray;
- cin >> sizeArray;
- int arr[100];
- for (int i = 0; i < sizeArray; ++i) {
- cin >> arr[i];
- }
- cout << longestDist(arr, sizeArray);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement