File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+     bool doesValidArrayExist(vector<int>& derived) {
4+         int n=derived.size();
5+         if(n==1 && derived[0]==0){
6+             return true;
7+         }
8+         else if(n==1 && derived[0]==1){
9+             return false;
10+         }
11+         vector<int> helper(n);
12+         if(derived[n-1]==0){
13+             helper[0]=1;
14+             helper[n-1]=1;
15+             for(int i=0;i<n-2;i++){
16+                 if(derived[i]==0){
17+                     helper[i+1]=helper[i];
18+                 }
19+                 else{
20+                     helper[i+1]=1-helper[i];
21+                 }
22+             }
23+             if(helper[n-2]^helper[n-1]==derived[n-2]){
24+                 return true;
25+             }
26+         }
27+         else{
28+             helper[0]=0;
29+             helper[n-1]=1;
30+             for(int i=0;i<n-2;i++){
31+                 if(derived[i]==0){
32+                     helper[i+1]=helper[i];
33+                 }
34+                 else{
35+                     helper[i+1]=1-helper[i];
36+                 }
37+             }
38+             if(helper[n-2]^helper[n-1]==derived[n-2]){
39+                 return true;
40+             }
41+             
42+         }
43+         return false;
44+     }
45+ };
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments