public class RotationsNumbers {
public int main(String[] args) {
Solution ans= new Solution();
int[] arr= {5, 1,2, 3, 4};
System.out.println(ans.findKRotation(arr , 5));
return -1;
}
class Solution {
int findKRotation(int arr[], int n) {
// code here
int ans= findPivot(arr, 0 , n-1);
if( ans ==-1) return 0;
return ans 1;
}
int findPivot( int arr[], int start, int end){
int mid ;
while(start< end){
mid= start (end- start)/2;
if (mid < end && arr[mid]> arr[mid 1]){
return mid;
}
if ( mid > start && arr[mid ]< arr[mid-1]){
return mid-1;
}
if ( arr[ mid ] < arr[0]){
end= mid-1;
}else{
start= mid 1;
}
}
return -1;
}
}
}
Edit : Tried using static void main too .. still throwing error For the above code below error is getting thrown , what should i do?
Error: Main method must return a value of type void in class BinarySearch.SortedArray, please define the main method as: public static void main(String[] args)
Process finished with exit code 1
CodePudding user response:
some problems that I see
- like mentioned in the comment the main should be
public static void main
- return statement in main need to go
the below code should work
public static void main(String[] args) {
int[] arr= {5, 1,2, 3, 4};
System.out.println(findKRotation(arr , 5));
}
static int findKRotation(int arr[], int n) {
// code here
int ans= findPivot(arr, 0 , n-1);
if( ans ==-1) return 0;
return ans 1;
}
static int findPivot( int arr[], int start, int end){
int mid ;
while(start< end){
mid= start (end- start)/2;
if (mid < end && arr[mid]> arr[mid 1]){
return mid;
}
if ( mid > start && arr[mid ]< arr[mid-1]){
return mid-1;
}
if ( arr[ mid ] < arr[0]){
end= mid-1;
}else{
start= mid 1;
}
}
return -1;
}
CodePudding user response:
I assume there is a reason you went with the class construction that you've gone with so I won't make any changes beyond addressing your question / concern:
Unable to run the Java code
To run your code make these changes;
Your Solution:
public int main(String[] args) {
Solution ans= new Solution();
int[] arr= {5, 1,2, 3, 4};
System.out.println(ans.findKRotation(arr , 5));
return -1;
}
Changed to:
public static void main(String[] args)
Solution ans= new Solution();
int[] arr= {5, 1,2, 3, 4};
System.out.println(ans.findKRotation(arr , 5));
}
Your Solution:
class Solution {
Changed to:
static class Solution {
With these minimal changes, your code should run.