CSES - Distinct Numbers
Author: Andrew Wang
Time Complexity:
Sort the array of numbers. Loop through the array and increment the answer for every distinct number. Distinct numbers can be found if the current number isn't equal to the previous number in the array.
C++
#include <bits/stdc++.h>using namespace std;int main() {int N; cin >> N;int arr[N];for (int i = 0; i < N; i++)cin >> arr[i];sort(arr, arr+N);int ans = 1;
Java
import java.io.*;import java.util.*;public class DistinctNumbers {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));int n = Integer.parseInt(br.readLine());StringTokenizer st = new StringTokenizer(br.readLine());int[] arr = new int[n];for (int i = 0; i < n; i++) {
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!