Question: IBM, Recently Asked Online Assessment Questions (IIIT Pune | 6M + FTE) | Evaluating Match Outcome | String twins | 2023
2
Entering edit mode

ADD COMMENTlink 14 months ago Delta 2.9k
0
Entering edit mode

Question 01 soln:

#include <bits/stdc++.h>
using namespace std;

void solve(){
  int n;
  cin>>n;
  
  vector<int> arr(n);
  for(int i=0; i<n; i++){
    cin>>arr[i];
  }
  
  bool flag = 0;
  int i=0, j=n-1;
  bool turn = 0;
  int s1 = 0, s2 = 0;
  
  while(i<=j){
    if(flag == 0){
      flag = (arr[i]%2 == 0) ? 1-flag : flag;
      if(turn == 0) s1 += arr[i];
      else s2 += arr[i];
      turn = 1-turn;
      i++;
    }else{
      flag = (arr[j]%2 == 0) ? 1-flag : flag;
      if(turn == 0) s1 += arr[j];
      else s2 += arr[j];
      turn = 1-turn;
      j--;
    }
  }
  
  cout<<s1<<" "<<s2<<endl;
}

int main() 
{
    int t;
    // cin>>t;
    t=1;
    while(t--){
      solve();
    }
}

 

ADD COMMENTlink 11 weeks ago Gullu • 0

Login before adding your answer.

Similar Posts
Loading Similar Posts