Skip to content

BitonicArraySearch.java added || DistinctElements.java added || Readme.md file updated #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions BitonicArraySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//Program to search in a Bitonic Array
import java.util.Scanner;
public class BitonicArraySearch {
static int ascendingBS(int a[], int k, int range) {
int l=0,h=range,m=0;
while(l<=h) {
m=(l+h)/2;
if(a[m]==k) {
return m;
}else if(k<a[m]){
h=m-1;
}else {
l=m+1;
}
}
return -1;
}
static int descendingBS(int a[], int k, int range) {
int l=range,h=a.length-1,m=0;
while(l<=h) {
m=(l+h)/2;
if(a[m]==k) {
return m;
}else if(k<a[m]){
l=m+1;
}else {
h=m-1;
}
}
return -1;
}
static int bitonicpoint(int a[]) {
int l=0,r=a.length-1,m=0;
while(l<=r) {
m=(l+r)/2;
if(a[m]>a[m-1] && a[m]>a[m+1]) {
return m;
}else if(a[m]>a[m-1] && a[m]<a[m+1]){
l=m;
}else {
r=m+1;
}
}
return -1;
}
static int searchbitonic(int a[],int k,int bindex) {
if(k==a[bindex]) {
return bindex;
}
if(k>a[bindex]) {
return -1;
}
int res1=ascendingBS(a, k, bindex-1);
int res2=descendingBS(a, k, bindex+1);
if (res1!=-1) {
return res1;
}
else if(res2!=-1) {
return res2;
}
return -1;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.print("Enter Number:");
int k=sc.nextInt();
int a[]= {1,3,6,7,9,12,8,5,2};
int bindex=bitonicpoint(a);
int b=searchbitonic(a, k, bindex);
if (b == -1) {
System.out.println("Element Not Found In Sequence");
}
else {
System.out.println("Element Found at index: "+ b);
}
}
}
26 changes: 26 additions & 0 deletions DistinctElements.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//Program to find distinct elements
public class DistinctElements {
static int dupliremoveint(int a[]) {
int rd=0;
for(int i=1;i<a.length;i++) {
if(a[rd]!=a[i]) {
rd++;
a[rd]=a[i];
}
}
return rd+1;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[]= {1,1,2,2,4,5,5,5,6,6,7};
for(int i=0;i<a.length;i++) {
System.out.print(a[i]+" ");
}
System.out.println("\n Distinct Elements:");
int b=dupliremoveint(a);
for(int i=0;i<b;i++) {
System.out.print(a[i]+" ");
}
}

}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ It is very easy to contribute, you may follow these steps -
99.[RotateLinkedList](https://github.com/PrajaktaSathe/Java/blob/main/Programs/RotateLinkedList.java)-Program to demo rotating a linked list
100. [ReverseString](https://github.com/PrajaktaSathe/Java/blob/main/ReverseString.java) -Program to reverse a String using the java method substring.
101.[Overriding](https://github.com/PrajaktaSathe/Java/blob/main/Programs/Overriding.java)-Program to demo overriding in java

102.[BitonicArraySearch](https://github.com/PrajaktaSathe/Java/blob/main/BitonicArraySearch.java)-Program to search in a Bitonic Array
103.[Distinct Elements](https://github.com/PrajaktaSathe/Java/blob/main/DistinctElements.java)-program to obtain distinct elements of an array
# Contributors -
## A big thank you to all our contributors!!!

Expand Down