-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
39050f1
commit 066f151
Showing
4 changed files
with
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
//{ Driver Code Starts | ||
import java.util.*; | ||
import java.io.*; | ||
class PeakElement{ | ||
public static void main(String[] args)throws IOException{ | ||
BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); | ||
PrintWriter out=new PrintWriter(System.out); | ||
int t=Integer.parseInt(in.readLine().trim()); | ||
while(t-->0){ | ||
int n=Integer.parseInt(in.readLine().trim()); | ||
int[] a=new int[n]; | ||
int[] tmp=new int[n]; | ||
String s[]=in.readLine().trim().split(" "); | ||
for(int i=0;i<n;i++){ | ||
a[i]=Integer.parseInt(s[i]); | ||
tmp[i] = a[i]; | ||
} | ||
|
||
int f=0; | ||
int A=(new Solution()).peakElement(tmp,n); | ||
|
||
if(A<0 && A>=n) | ||
out.println(0); | ||
else | ||
{ | ||
if(n==1 && A==0) | ||
f=1; | ||
else if(A==0 && a[0]>=a[1]) | ||
f=1; | ||
else if(A==n-1 && a[n-1]>=a[n-2]) | ||
f=1; | ||
else if(A >= 0 && A < n && a[A] >=a[A+1] && a[A]>= a[A-1]) | ||
f=1; | ||
else | ||
f=0; | ||
out.println(f); | ||
} | ||
} | ||
out.close(); | ||
} | ||
} | ||
// } Driver Code Ends | ||
|
||
|
||
/*Complete the function below*/ | ||
|
||
class Solution | ||
{ | ||
// Function to find the peak element | ||
// arr[]: input array | ||
// n: size of array a[] | ||
public int peakElement(int[] arr,int n) | ||
{ | ||
//add code here. | ||
int start = 0, end = n-1; | ||
while (start < end) | ||
{ | ||
int mid = start+(end-start)/2; | ||
if (arr[mid] > arr[mid+1]) | ||
end = mid; | ||
else | ||
start = mid+1; | ||
} | ||
return start; | ||
} | ||
} |
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,2 @@ | ||
Time complexity - O(logn) | ||
Space complexity - O(1) |
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,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(1) |
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,9 @@ | ||
class Solution | ||
{ | ||
public String maximumOddBinaryNumber(String s) | ||
{ | ||
int zeros = (int) s.chars().filter(c -> c == '0').count(); | ||
int ones = s.length() - zeros; | ||
return "1".repeat(ones - 1) + "0".repeat(zeros) + "1"; | ||
} | ||
} |