-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
specialindex2.cpp
65 lines (58 loc) · 1.45 KB
/
specialindex2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
@author: nandinisahu407
special index-> if after deleting element from index i , sum of even index=sum of odd index
approach->
after deleting ,previous element at odd index will be now at even index and vice versa
s_odd= odd[0 to i]+ even[i+1 to len]
s_even=even[0 to i]+odd[i+1 to len]
*/
#include<iostream>
using namespace std;
int main(){
int num;
cout<<"enter length"<<endl;
cin>>num;
vector <int> arr (num);
for(int i=0;i<num;i++){
cin>>arr[i];
}
int count=0;
int s_even,s_odd;
for(int i=0;i<num;i++){ //checking whether special index or not
s_even=0,s_odd=0;
for(int j=0;j<i;j++){ // from index[0,i]
if(j%2==0){
s_even+=arr[j];
}
else{
s_odd+=arr[j];
}
}
for(int j=i+1;j<num;j++){ //from index[i+1,len]
if(j%2==0){
s_odd+=arr[j];
}
else{
s_even+=arr[j];
}
}
if(s_even==s_odd){ //checking whether sum of even index=sum of odd index
cout<<"\n special index found at: "<<i;
count++;
}
else{
continue;
}
}
cout<<"\n total special index: "<<count; //displaying
return 0;
}
/* ----INPUT-----
enter length 6
4 3 2 7 6 -2
----- OUTPUT------
special index found at: 0
special index found at: 2
total special index:2
*/
//time complexity:o(n^2)