Skip to content

Instantly share code, notes, and snippets.

@0001vrn
Created July 30, 2016 10:16
Show Gist options
  • Save 0001vrn/26e07f0855549042f16437433ae871e8 to your computer and use it in GitHub Desktop.
Save 0001vrn/26e07f0855549042f16437433ae871e8 to your computer and use it in GitHub Desktop.
Given N and N elements Find the number of distinct sums
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
int []a= new int[n];
for(int i=0;i<n;i++)
a[i]=Integer.parseInt(sc.nextLine());
HashSet<Integer> hs=new HashSet<>();
for(int i=0;i<n;i++)
hs.add(a[i]);
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(!hs.contains(a[i]+a[j]))
hs.add(a[i]+a[j]);
System.out.println(hs.size());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment