Skip to content

Commit

Permalink
Added codes for 1 March
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Mar 1, 2024
1 parent 39050f1 commit 066f151
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
66 changes: 66 additions & 0 deletions GeeksForGeeks/March/01-3-24/GFG.java
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;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/March/01-3-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(logn)
Space complexity - O(1)
2 changes: 2 additions & 0 deletions LeetCode/March/01-3-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(1)
9 changes: 9 additions & 0 deletions LeetCode/March/01-3-24/Solution.java
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";
}
}

0 comments on commit 066f151

Please sign in to comment.