291A - Spyke Talks - CodeForces Solution


*special problem implementation sortings *800

Please click on ads to support us..

Python Code:

n = int(input())
spyke = list(map(int, input().split()))
d = {}
for x in spyke:
    if x in d:
        d[x] += 1
    elif x != 0:
        d[x] = 1

count = 0
flag = True
for x in d.values():
    if x == 2:
        count += 1
    elif x >= 3:
        print(-1)
        flag = False
        break

if flag:
    print(count)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n,cnt=0,sum=1,m=0;
	cin>>n;
	int a[n];
	for(int i=0; i<n; i++){
		cin>>a[i];
		if(a[i]==0){
			cnt++;
		}
	}
	sort(a,a+n);
	int b[n-cnt];
	for(int i=cnt; i<n; i++){
		b[i-cnt]=a[i];
	}
	for(int i=1; i<n-cnt; i++){
		if(b[i]!=b[i-1]){
			sum++;
		}
	}
	int c[sum];
	c[0]=1;
	for(int i=1; i<sum; i++){
		c[i]=0;
	}
	for(int i=1; i<n-cnt; i++){
		if(b[i]!=b[i-1]){
			m++;
		}
		c[m]++;
	}
	int we=0;
	for(int i=0; i<sum; i++){
		if(c[i]>2){
			cout<<-1;
			return 0;
		}
		else if(c[i]==2){
			we++;
		}
	}
	cout<<we;
}


Comments

Submit
0 Comments
More Questions

1418C - Mortal Kombat Tower
1382B - Sequential Nim
1272C - Yet Another Broken Keyboard
808A - Lucky Year
1245A - Good ol' Numbers Coloring
58B - Coins
1041C - Coffee Break
507A - Amr and Music
1041D - Glider
1486A - Shifting Stacks
1389B - Array Walk
71B - Progress Bar
701A - Cards
545A - Toy Cars
1538E - Funny Substrings
234A - Lefthanders and Righthanders
1611D - Weights Assignment For Tree Edges
197A - Plate Game
1474A - Puzzle From the Future
6B - President's Office
1405B - Array Cancellation
431C - k-Tree
101A - Homework
1642C - Great Sequence
1523B - Lord of the Values
1406C - Link Cut Centroids
2409. Count Days Spent Together
2410. Maximum Matching of Players With Trainers
1604C - Di-visible Confusion
997A - Convert to Ones