Move all negative numbers to beginning and positive to end (java),order is not important

Advertisements public class posbeginnegend { private static void Swap(int a, int b) { int temp=a; a=b; b=temp; } public static void main(String[] args) { int []arr={45,23,-89,-21,67,-34,7,-44}; int start=0; int end=arr.length-1; while(start<=end) { int mid=start+(end-start)/2; if(arr[mid]>0) { if(arr[end]>0) { end–; } else { Swap(arr[mid],arr[end]); end–; } } else { if(arr[start]<0) { start++; } else { Swap(arr[mid],arr[start]);… Read More Move all negative numbers to beginning and positive to end (java),order is not important

Arrays.binarySearch not finding the element

Advertisements I have this piece of code: class Solution { public int[] twoSum(int[] nums, int target) { for(int i = 0;i<nums.length;i++){ int n = target – nums[i]; int temp = nums[i]; nums[i] = Integer.MIN_VALUE; System.out.println("nums: "+ Arrays.toString(nums)); System.out.println("n: "+n); int result = Arrays.binarySearch(nums,n); System.out.println("result: "+result); if (result >= 0){ int[] out = new int[2]; out[0]… Read More Arrays.binarySearch not finding the element

Binary Search enters a infinite loop when I use a 'for' Loop but works fine 'while' while loop

Advertisements I am learning Binary Search and these are my attempts. So here are two pieces of code, both of them being same with the only difference being one is in while loop while the other is in for loop. int binarySearch(int *input, int n , int val) { int high = n-1, low =0;… Read More Binary Search enters a infinite loop when I use a 'for' Loop but works fine 'while' while loop

Why doesn't this code for a binary search function in c++ work?

Advertisements #include <iostream> int binary_search(int arr[], int size, int target) { int first = 0; int last = size – 1; int midpoint = (last + first) / 2; while(first <= last) { if(arr[midpoint] == target) { return midpoint; } else if(arr[midpoint] < target) { first = midpoint + 1; } else if(arr[midpoint] > target)… Read More Why doesn't this code for a binary search function in c++ work?

Binary search array showing -1 as result

Advertisements #include<stdio.h> int binary_search(int arr[], int size ,int element){ int low, mid, high; low=0; high=size-1; //start of search while(low<=high) { mid = (low + high)/2; if(arr[mid] == element){ return mid; } if(arr[mid]<element){ low= mid+1; } else{ high = mid-1; } } //end of search return -1; } int main(){ int arr[20]={1,20,31,44,54,68,70,85}; int size= sizeof(arr)/sizeof(int); int… Read More Binary search array showing -1 as result

Time complexity of finding range of target in sorted array – Is this solution O(N) in the worst case?

Advertisements I was going through LeetCode problem 34. Find First and Last Position of Element in Sorted Array, which says: Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value. If target is not found in the array, return [-1, -1]. You must write… Read More Time complexity of finding range of target in sorted array – Is this solution O(N) in the worst case?

Can't typecast to int in C; Error: Expected expression before int

Advertisements int arr5[8]={10,20,30,40,50,60,70,80}; len=8; int beg=0,mid,end=len-1,loc; mid=int((beg+end)/2); printf("%d",mid); I was trying to write a code for binary search but can’t typecast the value of (beg+end)/2 to mid. error: expected expression before ‘int’ mid=int((beg+end)/2); ^~~ >Solution : For starters using the type specifier int in this statement mid=int((beg+end)/2); does not make a sense. It is enough… Read More Can't typecast to int in C; Error: Expected expression before int