Find index of an extra element present in one sorted array | C ++ code for above approach ; function return sum of array elements ; function return index of given element ; Function to find Index ; Calculating extra element ; returns index of extra element ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum ( int arr [ ] , int n ) { int summ = 0 ; for ( int i = 0 ; i < n ; i ++ ) { summ += arr [ i ] ; } return summ ; } int indexOf ( int arr [ ] , int element , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == element ) { return i ; } } return -1 ; } int find_extra_element_index ( int arrA [ ] , int arrB [ ] , int n , int m ) { int extra_element = sum ( arrA , n ) - sum ( arrB , m ) ; return indexOf ( arrA , extra_element , n ) ; } int main ( ) { int arrA [ ] = { 2 , 4 , 6 , 8 , 10 , 12 , 13 } ; int arrB [ ] = { 2 , 4 , 6 , 8 , 10 , 12 } ; int n = sizeof ( arrA ) / sizeof ( arrA [ 0 ] ) ; int m = sizeof ( arrB ) / sizeof ( arrB [ 0 ] ) ; cout << find_extra_element_index ( arrA , arrB , n , m ) ; }