Posts

Search

Bridges

QUESTION DESCRIPTION

Johnny has a pool in his garden. There are several islands in the pool. Some islands are connected by bridges. Any bridge can be removed.

Every day Johnny removes some bridges so that there is only one way from any island to any other. In the evening he returns removed bridges to their places. Also he has some favourite bridges which he never removes. Johnny will be happy if he is able to make a configuration of bridges on the given day which he has never made before.

You have to count the amount of days he will be happy. Of course, if the favourite bridges themselves don't satisfy the happiness condition Johnny will not be happy for even single day. Now display all the locations at which required element is found and also the number of times it occur in the file

TEST CASE 1

INPUT
5
23
4
12
4
4
4
OUTPUT
4 is present at location 2
4 is present at location 4
4 is present at location 5
4 is present 3 times in array

TEST CASE 2

INPUT
5
3
2
3
3
3
3
OUTPUT
3 is present at location 1
3 is present at location 3
3 is present at location 4
3 is present at location 5
3 is present 4 times in array

Code :

#include <iostream>
using namespace std;
int main() {
  int a,b,j=0;
  int ar[a];
  cin>>a;
  for(int i=0;i<a;i++)
  {
    cin>>ar[i];
  }
  cin>>b;
    for(int i=0;i<a+1;i++)
  {
    if(ar[i]==b)
    {
      cout<<b<<" is present at location "<<i+1<<"\n";
      j=j+1;
    }
    }
  if(j==0)
  {
    cout<<b<<" is not present in array ";
  }
   else
   {
 cout<<b<<" is present "<<j<<" times in array";
   }
 return 0;
   
}