-1
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i,a,b) for(ll i=a;i<b;i++)


void calculation(ll n,ll x)
{
     
     ll i,arr[n],k,max=INT_MIN,j=0,min=INT_MAX;
     map<ll,ll> m1,m2;
        
     rep(i,0,n)
     {
          cin>>arr[i];
          m1[arr[i]]++;
          
          
     }
     if(x==0)
     {
          rep(i,0,n)
          {
                 if(m1[arr[i]]>max)
                 max=m1[arr[i]];
          }
          cout<<max<<" 0\n";
     }
     else{
     rep(i,0,n)
     {
          if(m1[arr[i]]+m2[arr[i]]>=max && m2[arr[i]]<min){
          max=m1[arr[i]]+m2[arr[i]];
          min=m2[arr[i]];}
     }
     
     cout<<max<<" "<<min<<"\n";
        }

}
int main ()
{
     ll t,n,x;
     cin>>t;
     while(t--)
     {
          cin>>n>>x;
          calculation(n,x);
     }
     return 0;
}

In the above code i used to map,one to store the intial frequency of numbers and second to store the frequency of numbers xor 'X'. This question is from codechef september long challenge and giving wrong answer.I am not asking answer ,but I want to clear my concepts of maps . Also I am providing the link of question if anyone want to try it. https://www.codechef.com/SEPT21C/problems/PALINT

if the key is same ,then if we increment in map 2,does it also get incremented in map 1.

pls help !. I got stuck here.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • 1
    Instances of maps do not share information. – Pepijn Kramer Sep 11 '21 at 09:49
  • 1
    What language is that? – A M Sep 11 '21 at 09:54
  • Thank you ,for your response,but I am not getting the point so can you please elaborate it @PKramer – Kalash Jain Sep 11 '21 at 09:57
  • _"...if we increment in map 2..."_ `m2` is never incremented. Also `m2` is never assigned to. Note `m2[arr[i]]` (and similar) in expressions will value initialise the map with the `key` and `0` for the value. – Richard Critten Sep 11 '21 at 10:07
  • You might want to read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h), [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) and [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). Many potential bug sources. Also the macros make your code hard to read. – Lukas-T Sep 11 '21 at 10:07
  • Do not tag C for C++ questions. – Eric Postpischil Sep 11 '21 at 10:12
  • 1
    @KalashJain What I mean is that each map m1 and m2 has its own state. They have their own unique set of key value pairs. m1 cannot access any of that information in m2 or the other way around. You can have the same keys, but you must add values to m1 and m2 seperatly. (Or create a struct containing both values, and then make one a map from key to struct) – Pepijn Kramer Sep 11 '21 at 10:18
  • @PKramer Thank you for your explanation .I understood the concept – Kalash Jain Sep 11 '21 at 10:25

1 Answers1

1

The answer is No. The two map is different from each other. They don't share memory. You should print address of value or key of the two map for more details.