Skip to content

Commit

Permalink
Merge pull request #47 from yanurag1414/cycle_sort
Browse files Browse the repository at this point in the history
adding cycle sort
  • Loading branch information
lilmistake authored Oct 20, 2023
2 parents e4148dd + 7f10d04 commit 6e3b403
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions DSA_Codesheet/Java/cycle_sort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public class cycle_sort {
public static void main(String[] args) {
long[] arr = {1,2,4,5,1};
dupmis(arr, arr.length);

}
static void dupmis(long[] arr,long n){
long sum = (n*(n+1))/2;
long squaresum = (n*(n+1)*((2*n+1)))/6;
long s1 = 0; //sum of array elements
long s2 = 0; //sum of square of array elements
for (int i = 0; i < n; i++) {
s1 += arr[i];
s2 += (arr[i]*arr[i]);
}
long val1 = s1-sum; //x-y
long val2 = s2-squaresum;
val2 = val2/val1;
long x = (val1+val2)/2;
long y = x-val1;
System.out.print(x+" ");
System.out.print(y);
}
}

0 comments on commit 6e3b403

Please sign in to comment.