For better understanding refer YouTube Tutorial Video
LOGIC
- Take array size (N)
- Input array (A)
- Sort the array.
- Set sock pair counter to 0 (C=0) and sock frequency counter to 1 (T=0)
- Check for match and increment frequrncy.
- Check the no of pairs for each frequency.
- Print the final pair count.
CODE
/***SOLUTION BY CODING DEMONS***/
#include <bits/stdc++.h>
using namespace std;
int main () {
int n; //array size
cin>>n;
int a[n];
for (int i=0;i<n;i++){
cin>>a[i];
//input array
}
//sorting the array
int m=sizeof(a)/sizeof(a[0]);
sort(a,a+m);
int c=0; //set counter to 0
int t=1; //frequency count set to 1
int i=0;
while (i<n){
for (int j=i+1;j<n;j++){
int x=a[i];
int y=a[j];
if (x==y){ //frequency count
t++;
}
}
c=c+t/2; //pair check
i=i+t; //set i to the next sock
t=1; //set t=1 to count
//next sock pairs
}
cout<<c; //print the total pairs
}
/********END********/
Our other Code Solutions
- Cats and a Mouse
- For More Solutions Click Here
!! SUBSCRIBE FOR MORE CODES & VIDEOS !!
0 Comments