I want to separate negative numbers and positive numbers in an array.
For example, if my array has 10 values and they are {-8,7,3,-1,0,2,-2,4,-6,7}, I want the new modified array to be {-6,-2,-1,-8,7,3,0,2,4,7}.
I want to do this in O(n^2) and I have written a code as well. But I am not getting the right outputs. Where is my code wrong?
import java.util.Random;
public class Apples {
public static void main(String[] args) {
Random randomInteger=new Random();
int[] a=new int[100];
for(int i=0;i<a.length;i++)
{
a[i]=randomInteger.nextInt((int)System.currentTimeMillis())%20 - 10;
}
for(int i=0;i<a.length;i++)
{
if(a[i]<0)
{
int temp=a[i];
for(int j=i;j>0;j--)
{
a[j]=a[j-1];
j--;
}
a[0]=temp;
}
}
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+" ");
}
}
}
You have two j-- while you need only one, so remove either one of them.
for(int j=i;j>0;j--)
{
a[j]=a[j-1];
// remove j--; from here
}
You might consider the following as an alternative way of doing the partition of negatives/positives. This is based on K&R's quicksort, but only makes one pass on the array:
import java.util.Random;
public class Sweeper {
public static void printArray(int[] a) {
for (int elt : a) {
System.out.print(elt + " ");
}
System.out.println();
}
public static void swap(int[] a, int i, int j) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
public static void partition(int[] a, int target) {
int last = 0;
for (int i = 0; i < a.length; ++i) {
if (a[i] < target && i != last) swap(a, i, last++);
}
}
public static void main(String[] args) {
Random rng = new Random();
int[] a = new int[20];
for (int i = 0; i < a.length; i++) {
a[i] = rng.nextInt(20) - 10;
}
printArray(a);
partition(a, 0);
printArray(a);
}
}
Related
I am trying to iterate over an array and add the sum of the array except the number 13 and the number after it.
Example
[1,1,1,1,13,2] = [1,1,1,1,0,0] = 4
this is what I have so far the main things I need to know is how do I check if the array has a number 13 in it and how do I change it to a 0
public static int sum13(int[] nums) {
for(int i=0; i < nums.length; i++) {
if(nums.indexOf(i) == 13) {
}
}
}
public static void main(String[] args) {
//this is the main method
int[] a = {1,2,3,13,4};
sum13(a);
}
}
You can try this , to skip adding all number when you get 13 in your array :
public static int sum13(int[] nums) {
int sum = 0;
for(int i=0; i < nums.length; i++) {
if(nums[i] == 13) {
break;
}
sum += nums[i];
}
return sum;
}
public static void main(String[] args) {
//this is the main method
int[] a = {1,2,3,13,4};
System.out.println(sum13(a));
}
Try this.
public static int sum13(int[] nums) {
return IntStream.of(nums).takeWhile(i -> i != 13).sum();
}
I have tried this
public static void main(String[] args) throws ArrayIndexOutOfBoundsException
{
int arr1[]= {1,2,3,4,5};
int arr2[]= {6,7};
for(int i=0;i<arr1.length;i++)
{
System.out.println(arr1[i]);
System.out.println(arr2[i]);
}
}
but this does not work.
You have to find the max length and then care about what you can output to not hit the boundaries.
public static void main(String[] args) throws ArrayIndexOutOfBoundsException
{
int arr1[]= {1,2,3,4,5};
int arr2[]= {6,7};
int max = arr1.length;
if (max < arr2.length){
max = arr2.length;
}
for(int i=0;i<max;i++)
{
if (i < arr1.length){
System.out.println(arr1[i]);
}
if (i < arr2.length){
System.out.println(arr2[i]);
}
}
}
Iterate the loop to the maximum length of two arrays, and Print only those array element whose index is valid.
public static void main(String[] args) throws ArrayIndexOutOfBoundsException
{
int arr1[]= {1,2,3,4,5};
int arr2[]= {6,7};
for(int i=0;i<Math.max(arr1.length, arr2.length);i++)
{
if(i<arr1.length)
System.out.println(arr1[i]);
if(i<arr2.length)
System.out.println(arr2[i]);
}
}
Try this, this will work I think
int arr1[]= {1,2,3,4,5};
int arr2[]= {6,7};
int a1 = arr1.length();
int a2 = arr2.length();
int n = a1 > a2 ? a1 : a2;
for(int i = 0; i < n; i++) {
if(a1 > i)
System.out.println(arr1[i]);
if(a2 > i)
System.out.println(arr2[i]);
}
Best of luck
Or, for printing them one after the other:
int arr1[]= {1,2,3,4,5};
int arr2[]= {6,7};
for (int i=0; i < arr1.length + arr2.length; ++i)
{
if (i < arr1.length)
System.out.println (arr1 [i]);
else
System.out.println (arr2 [i - arr1.length]);
}
You can use below code. You can still meet a condition where both arrays of same length.This will work only if arrays are of different lengths.
public static void main(String[] args) throws ArrayIndexOutOfBoundsException
{
int arr1[]= {1,2,3,4,5};
int arr2[]= {6,7};
int arr1Length = arr1.length;
int arr2Length = arr2.length;
if(arr1.length>arr2.length)
{
for(int i=0;i<arr1.length;i++)
{
System.out.println(arr1[i]);
if(i<arr2.length)
{
System.out.println(arr2[i]);
}
else continue;
}
else{
for(int i=0;i<arr2.length;i++)
{
if(i<arr1.length)
{
System.out.println(arr1[i]);
}
else continue;
System.out.println(arr2[i]);
}
}
}
This is my java code for array exercises at my school
and I was wondering why even though everything here works when I try to use the ImprovisedBubbleSort method my program stops functioning
public static void ImprovedBubbleSort(int [] array){
boolean swap;
int temp;
int max;
do {
swap=false;
max=1;
for (int i=0;i<array.length-max;i++){
if (array[i]>array[i+1]){
temp=array[i];
array[i]=array[i+1];
array[i+1]=temp;
swap=true;
}
max++;
}
}while (swap=true);
System.out.println("The numbers you entered in order are: ");
for (int j=0;j<10;j++){
System.out.println(array[j]);
}
}
}
It's important to realize that if you're using a single loop like in your example with that if statement you can find an instance of position 0 and 1 where it is sorted but the rest of the array may not be sorted. This will cause the if statement to not activate.
You can alleviate this problem by doing something like this:
import java.util.Arrays;
public class MyClass {
public static void main(String args[]) {
int test[] = {7,1,9,1,5,6};
bubbleSort(test);
System.out.println(Arrays.toString(test));
}
static void bubbleSort(int[] arr) {
int n = arr.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(arr[j-1] > arr[j]){
//swap elements
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
}
See this example.
Okay, so i need to find all the negative numbers of array and return them.I found the negative number, but how do i return them all? P.S yes i am a beginner.
public static void main(String[] args) {
int [] array = {5,-1,6,3,-20,10,20,-5,2};
System.out.println(findNumber(array));
}
public static int findNumber(int[] sum) {
int num = 0;
for (int i = 0; i < sum.length ; i++) {
if(sum[i] < num) {
num = sum[i];
}
}
return num;
}
Java 8 based solution. You can use stream to filter out numbers greater than or equal to zero
public static int[] findNumber(int[] sum)
{
return Arrays.stream(sum).filter(i -> i < 0).toArray();
}
There are multiple ways of doing this, if you just want to output all of the negative numbers easily you could do this:
public static void main(String[] args) {
int [] array = {5,-1,6,3,-20,10,20,-5,2};
ArrayList<Integer> negativeNumbers = findNumber(sum);
for(Integer negNum : negativeNumbers) {
System.out.println(negNum);
}
}
public static ArrayList<Integer> findNumber(int[] sum) {
ArrayList<Integer> negativeNumbers = new ArrayList<>();
for (int i = 0; i < sum.length ; i++) {
if(sum[i] < 0) {
negativeNumber.add(sum[i]);
}
}
return negativeNumbers;
}
As you told you are beginner, i'm giving code in using arrays only.
Whenever you come across a negative number, just add it to the array and increment it's index number and after checking all the numbers, return the array and print it.
public static void main(String[] args)
{
int [] array = {5,-1,6,3,-20,10,20,-5,2};
int[] neg = findNumber(array);
for(int i = 0 ; i<neg.length; i++)
{
System.out.println(neg[i]);
}
}
public static int[] findNumber(int[] a)
{
int j=0;
int[] n = new int[a.length];
for(int i = 0; i<a.length ; i++)
{
if(a[i] <0)
{
n[j] = a[i];
j++;
}
}
int[] neg = new int[j];
for( int k = 0 ; k < j ; k++)
{
neg[k] = n[k];
}
return neg;
}
I hope it helps.
You can modify your method to iterate through the array of numbers, and add every negative number you encounter, to a List.
public static List<Integers> findNegativeNumbers(int[] num) {
List<Integers> negativeNumbers = new ArrayList<>();
for (int i = 0; i < num.length; i++) {
if(num[i] < 0) {
negativeNumbers.add(num[i]);
}
}
return negativeNumbers;
}
You could then print out the list of negative numbers from this method itself, or return the list with return to be printed in main.
You code is returning the sum of elements, but I understood that you wanted every negative number.
So, I assumed you want something like this:
public static void main(String[] args) {
int [] array = {5,-1,6,3,-20,10,20,-5,2};
Integer [] result = findNumbers( array );
for( int i : result )
{
System.out.println( i );
}
}
public static Integer[] findNumbers(int[] v) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < v.length ; i++) {
if(v[i] < 0) {
list.add(v[i]);
}
}
return list.toArray( new Integer[0] );
}
Is it?
Best regards.
public static int[] findNum(int[] array)
{
int negativeIntCount = 0;
int[] negativeNumbers = new int[array.length];
for(int i = 0; i < array.length; i++)
{
if(array[i] < 0)
{
negativeIntCount++;
negativeNumbers[i] = array[i];
}
}
System.out.println("Total negative numbers in given arrays is " + negativeIntCount);
return negativeNumbers;
}
To display as an array in output :
System.out.println(Arrays.toString(findNum(array)));
To display output as space gaped integers :
for(int x : findNum(array))
{
System.out.print(" " + x)
}
Say I have the nested array
gridInterFT=[[1,2],[2,3],[3,4],[1,3],[1,4],[2,4]]
How would I write a function that takes this array as input and returns a 3d nested array containing all possible permutations of the array?
I.e. the returned array should look something like:
[[[1,2],[2,3],[3,4],[1,3],[1,4],[2,4]], [[1,3],[2,3],[3,4],[1,2],[1,4],[2,4]],
[[2,3],[1,2],[3,4],[1,3],[1,4],[2,4]]...]
Are there any libraries which contain functions that do this directly?
static <T> void swap(T[] a, int i, int j) {
T t = a[i];
a[i] = a[j];
a[j] = t;
}
static void permutation(int[][] perm, int n, List<int[][]> result) {
int size = perm.length;
if (n >= size)
result.add(Arrays.copyOf(perm, size));
else
for (int i = n; i < size; i++ ) {
swap(perm, n, i);
permutation(perm, n + 1, result);
swap(perm, n, i);
}
}
public static int[][][] permutation(int[][] perm) {
List<int[][]> result = new ArrayList<>();
permutation(perm, 0, result);
return result.toArray(new int[0][][]);
}
public static void main(String[] args) {
int[][] gridInterFT={{1,2},{2,3},{3,4},{1,3},{1,4},{2,4}};
int[][][] r = permutation(gridInterFT);
for (int[][] a : r) {
for (int i = 0; i < a.length; ++i)
System.out.print(Arrays.toString(a[i]));
System.out.println();
}
}