Pair of Socks problem in cpp, why output is not coming | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Pair of Socks problem in cpp, why output is not coming

#include<iostream> #include<algorithm> using namespace std; int sockspair(int a[],int n) { sort(a,a+n); int count=0; for(int i=0;i<n-1;i+2) { if(a[i]==a[i+1]) { count++; } } return count; } int main() { int n; cin>>n; int a[n]; for(int i=0;i<n;i++) { cin>>a[i]; } int result=sockspair(a,n); cout<<"Pairs of socks are"<<result; }

17th Aug 2021, 2:22 PM
Abhishek Kumar
Abhishek Kumar - avatar
2 Answers
+ 5
Try to use i += 2 instead of i+2
17th Aug 2021, 2:25 PM
Pariket Thakur
Pariket Thakur - avatar
0
Thanks bro
17th Aug 2021, 2:37 PM
Abhishek Kumar
Abhishek Kumar - avatar