MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / findTwoElement

Method findTwoElement

MissingAndRepeating.java:3–30  ·  view source on GitHub ↗
(int arr[], int n)

Source from the content-addressed store, hash-verified

1class Solve
2{
3 int[] findTwoElement(int arr[], int n)
4 {
5 // code here
6 int result[] = new int[2];
7
8 int sum=0;
9
10 int origSum=0;
11
12 for(int i=0;i<n;i++)
13 {
14 int index = Math.abs(arr[i])-1;
15
16 if(arr[index]<0)
17 {
18 result[0] = Math.abs(arr[i]);
19 }
20
21 arr[index] = -arr[index];
22
23 sum+=Math.abs(arr[i]);
24
25 origSum+=(i+1);
26 }
27 result[1]=origSum-(sum-result[0]);
28
29 return result;
30 }
31}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected