-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #268 from Ishaan-10/master
array Intersection program
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
Program's_Contributed_By_Contributors/Java_Programs/arrayIntersection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
public class arrayIntersection{ | ||
|
||
public static void intersect(int[] a,int[] b){ | ||
|
||
int minSize = (a.length>=b.length) ? b.length:a.length; | ||
int c[] = new int[minSize]; | ||
int z=0; | ||
for(int i=0;i<a.length;i++){ | ||
|
||
for(int j=0;j<b.length;j++){ | ||
|
||
if(a[i]==b[j]){ | ||
|
||
if(search(c, a[i])==-1){ | ||
c[z]=a[i]; | ||
z++; | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
public static int search(int arr[],int a){ | ||
|
||
int start=0,end=arr.length-1; | ||
|
||
while(start<=end){ | ||
|
||
int mid=(start+end)/2; | ||
if(arr[mid]>a){ | ||
end=mid-1; | ||
}else if(arr[mid]<a){ | ||
start = mid+1; | ||
}else{ | ||
return mid; | ||
} | ||
|
||
} | ||
|
||
return -1; | ||
} | ||
|
||
public static void main(String[] args) { | ||
|
||
int arr1[] = { 1,2,3,4,5,6,7,8,9,10}; | ||
int arr2[] = { 5,6,7,8,9,10,11,12,13,14,15,16}; | ||
intersect(arr1,arr2); | ||
} | ||
} |