Skip to content

Instantly share code, notes, and snippets.

@thmain
Created July 23, 2017 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/af9c0870ed342ea8524f1d282818cedb to your computer and use it in GitHub Desktop.
Save thmain/af9c0870ed342ea8524f1d282818cedb to your computer and use it in GitHub Desktop.
public class TwoRepeatingByFormula {
public static void twoElements(int [] A, int n){
int a,b;
int X =0;
int Y =1;
int S = n*(n+1)/2;
int fact = factorial(n);
for (int i = 0; i <A.length ; i++) {
X += A[i];
Y *= A[i];
}
int sum = X - S;
int product = Y/fact;
int subtract = (int)Math.sqrt(sum*sum - 4*product);
a = (sum + subtract)/2;
b = sum - a;
System.out.println("Two Repeating Elements are: " + a + " and " + b);
}
static int factorial(int n){
if(n==0)
return 1;
else
return n*factorial(n-1);
}
public static void main(String[] args) {
int [] A = {1,4,5,6,3,2,5,2};
int n = 6;
twoElements(A, n);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment