GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Data-Structure_Algorithms_Coding/balarray.cpp at main · Sdccoding/Data-Structure_Algorithms_Coding Expand file tree/ Copy pathbalarray.cppMore file actions 58 lines (49 loc) · 1.3 KB / Copy pathbalarray.cppFile metadata and controls58 lines (49 loc) · 1.3 KB 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 // Given an integer array A of size N. You need to count the number of special elements in the given array. // A element is special if removal of that element make the array balanced. // Array will be balanced if sum of even index element equal to sum of odd index element. int Solution::solve(vector<int> &A) { int n=A.size(); int od[n]={0}; int ev[n]={0}; od[0]=A[0]; if(n>=2) od[1]=od[0]; for(int i=2;i<n;i=i+2) { od[i]=od[i-2]+A[i]; if(i+1<n) od[i+1]=od[i]; } ev[0]=0; if(n>=2) ev[1]=A[1]; if(n>=3) ev[2]=ev[1]; for(int i=3;i<n;i=i+2) { ev[i]=ev[i-2]+A[i]; if(i+1<n) ev[i+1]=ev[i]; } // for(int i=0;i<n;i++) // cout<<od[i]<<" "; // cout<<endl; // for(int i=0;i<n;i++) // cout<<ev[i]<<" "; // cout<<endl; int cp=0; for(int i=0;i<n;i++) { int odd=0,even=0; if(i%2==0) { odd=od[i]-A[i]; odd+=ev[n-1]-ev[i]; even=ev[i]+od[n-1]-od[i]; } else { even=ev[i]-A[i]; even+=od[n-1]-od[i]; odd=od[i]+ev[n-1]-ev[i]; } if(odd==even) cp++; } return cp; } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |