Question: Amex , Recent Online Assessment Questions (SVNIT Surat) | N buckets | Small One-Way Bridge | 3rd August 2023
0
Entering edit mode

ADD COMMENTlink 15 months ago PoGo 2.4k
0
Entering edit mode

question 2 soltuion

int solution(int U,vector<int> &weight){
  int n=weight.size();
  int i=0,j=1;//two pointers to check sum of weight of the two car passing bridge
  //base case if there is only one car

  if(n==1){
    if(weight[0]<=U){
      return 0;
    }
    return 1;
  }

  bool f=true;
  //base case if all cars weight greater than U return n
  for(int i=0;i<n;i++){
    if(weight[i]<=U){
      f=false;
      break;
    }
  }

  if(f){
    return n;
  }

  int ans=0;
  while(j<n){
    //as at a time only 2 vehicle can go from bridge
    int tot=weight[i]+weight[j];
    //check if sum of weight of cars passing is less than or equal to U
    if(tot<=U){
      i=j;
      j++;
    }else{
  //if tot is more than U than remove the car which has maximum weight out of the two car passing
      int v=max(weight[i],weight[j]);
      if(v==weight[j]){
        j++;
      }else{
        i=j;
        j++;
      }
  //increament ans by 1
      ans++;
    }
  }
  return ans;
}

 

ADD COMMENTlink 15 months ago suryansh jaiswal • 370
0
Entering edit mode

question 01 solution

 

#include<bits/stdc++.h>

using  namespace std;

 

void solve(){

    int n;

    cin>>n;

    char s[n];

    for(int i=0; i<n; i++){

        cin>>s[i];

    }

 

    int k = 0;

    for(int i=0; i<n; i++){

        if(s[i] == 'B'){

            k++;

        }

    }

    if((2*k) - 1 > n){

        cout<<"Not possible"<<endl;

        return;

    }

    int ans = 0;

    int cnt = 0;

    int i=0, j=0;

    while(j<n){

        if(j%2 == 0 && s[j] == 'B') cnt++;

        if(j-i+1 < 2*k){

            j++;

        }

        else if(j-i+1 == 2*k){

            ans = max(ans, cnt);

            j = j+2;

            if(s[i] == 'B') cnt--;

            i = i+2;

        }

    }

    cnt = 0;

    while(j<n){

        if(j%2 != 0 && s[j] == 'B') cnt++;

        if(j-i+1 < 2*k){

            j++;

        }

        else if(j-i+1 == 2*k){

            ans = max(ans, cnt);

            j = j+2;

            if(s[i] == 'B') cnt--;

            i = i+2;

        }

    }

 

    cout<<"the answer is "<<(k-ans)<<endl;

}

 

int main(){

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

 

    int t;

    t=1;

    for(int i=0; i<t; i++){

        solve();

    }

}

ADD COMMENTlink 3 months ago Gullu • 0
Entering edit mode
0

Has anyone verified the above code , it is showing wrong answers ig, can someone provide correct code

 

ADD REPLYlink 15 months ago
JAADU
• 0
0
Entering edit mode
//question 01 solution in cpp #includeusing namespace std; void solve(){ int n; cin>>n; char s[n]; for(int i=0; i>s[i]; } int k = 0; for(int i=0; in){ cout<<"Not possible"<
ADD COMMENTlink 15 months ago Gullu • 0
0
Entering edit mode

question 01 solution in cpp 

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

void solve(){
    int n;
    cin>>n;
    char s[n];
    for(int i=0; i<n; i++){
        cin>>s[i];
    }

    int k = 0;
    for(int i=0; i<n; i++){
        if(s[i] == 'B'){
            k++;
        }
    }
    if((2*k) - 1 > n){
        cout<<"Not possible"<<endl;
        return;
    }
    int ans = 0;
    int cnt = 0;
    int i=0, j=0;
    while(j<n){
        if(j%2 == 0 && s[j] == 'B') cnt++;
        if(j-i+1 < 2*k){
            j++;
        }
        else if(j-i+1 == 2*k){
            ans = max(ans, cnt);
            j = j+2;
            if(s[i] == 'B') cnt--;
            i = i+2;
        }
    }
    cnt = 0;
    while(j<n){
        if(j%2 != 0 && s[j] == 'B') cnt++;
        if(j-i+1 < 2*k){
            j++;
        }
        else if(j-i+1 == 2*k){
            ans = max(ans, cnt);
            j = j+2;
            if(s[i] == 'B') cnt--;
            i = i+2;
        }
    }

    cout<<"the answer is "<<(k-ans)<<endl;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t; 
    t=1;
    for(int i=0; i<t; i++){
        solve();
    }
}

 

ADD COMMENTlink 15 months ago Gullu • 0
0
Entering edit mode
//question 01 soln #includeusing namespace std; void solve(){ string s; cin>>s; int n = s.size(); int k = 0; for(int i=0; in) cout<<-1<evenCnt){ if(n/2 >= k) cout<
ADD COMMENTlink 3 months ago Gullu • 0

Login before adding your answer.

Similar Posts
Loading Similar Posts