This blog is intended to discuss many things of which algorithmic problems are a part.
So lets begin with a fairly simple problem Create Collections on spoj:
The problem can easily be solved by hashing as the constraints are really poor. By hashing, we can find out for each number whether a corresponding double number exists or not. Rest is simple :) Below is the accepted code for the problem:
So lets begin with a fairly simple problem Create Collections on spoj:
The problem can easily be solved by hashing as the constraints are really poor. By hashing, we can find out for each number whether a corresponding double number exists or not. Rest is simple :) Below is the accepted code for the problem:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<cstdio> | |
#include<algorithm> | |
#include<cstdlib> | |
#include<cstring> | |
using namespace std; | |
int hash[10000000]; | |
int main() | |
{ | |
int t, n, i, a[103], count; | |
scanf("%d", &t); | |
while(t--) { | |
count=0; | |
memset(hash, 0, sizeof(hash)); | |
scanf("%d", &n); | |
for(i=0; i<n; i++) { | |
scanf("%d", &a[i]); | |
hash[a[i]]+=1; | |
} | |
sort(a, a+n); | |
for(i=0; i<n; i++) { | |
if(hash[a[i]]>0 && hash[2*a[i]]>0) { | |
hash[a[i]]--; | |
hash[2*a[i]]--; | |
count++; | |
} | |
} | |
printf("%d\n", count); | |
} | |
return 0; | |
} |