Skip to content

Commit

Permalink
Solve special array and next letters
Browse files Browse the repository at this point in the history
  • Loading branch information
sangaryousmane committed Jun 8, 2023
1 parent b1ec17c commit 0f0baed
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/intermediate/Searching.java
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,29 @@ public static char nextGreatestLetter(char[] letters, char target){
start = middle + 1;
}
}
return letters[start % n];
return letters[start % n]; // Get the index of start and length modulus
}

// https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x/description/
public static int specialArray(int [] nums){
int end = nums.length;
int start = 0;

while (start <= end){
int middle = start + (end - start) / 2;
int count = 0;

for (int i = 0; i< nums.length; i++){
if (nums[i] >= middle)
count++;
}
if (count == middle)
return middle;
else if (count < middle)
end = middle-1;
else
start = middle + 1;
}
return -1;
}
}

0 comments on commit 0f0baed

Please sign in to comment.