chore(CPlusPlus): add special index in an array (#1048)

Co-authored-by: Arsenic <54987647+Arsenic-ATG@users.noreply.github.com>
pull/1079/head
nandinisahu407 2022-10-30 21:33:10 +05:30 committed by GitHub
parent 978a119d9a
commit 07d7d4aeb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 0 deletions

View File

@ -0,0 +1,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)

View File

@ -32,6 +32,7 @@
- [Maximum Minimum Average of numbers](Arrays/max-min-avg.cpp)
- [Sparse Matrix](Arrays/sparse_matrix.cpp)
- [Balanced Parenthesis](Arrays/balanced-parenthesis.cpp)
- [Find special index](Arrays/specialindex2.cpp)
## Dynamic-Programming