how to find max in array then remove that max element and insert that max element by divided by 2 (floor division) in java

huangapple go评论71阅读模式
英文:

how to find max in array then remove that max element and insert that max element by divided by 2 (floor division) in java

问题

我有一个Java程序,在其中给定一个数组,比如{20, 5, 7, 9},我们需要执行以下操作:
(索引在这里不重要)

  1. 在这个数组中找到最大的元素并删除它。
  2. 然后将最大元素再次添加到数组中,除以2(向下取整除法)。
    我们可以在任何地方添加最大元素除以2的结果。
英文:

I have a java program in which we are given an array lets say {20 ,5 ,7 ,9} in this array we have to perform following operations:
(index do not matter here)

  1. find max element in this array and delete that element in array.
  2. then add the max element again by divided by 2(floor division).
    we can add max element divided by 2 where ever we want.

答案1

得分: 1

Java中的数组是静态的,因此一旦实例化,数组的大小就不能更改。因此,我们不能删除一个元素并减小数组的大小。

int max = -Integer.MIN_VALUE;
int maxIndex = -1;

现在遍历数组,如果元素大于maxValue,则更新maxValuemaxIndex

for (int i = 0; i < array.length; i++) {
    if (array[i] > max) {
        max = array[i];
        maxIndex = i;
    }
}

现在将maxIndex处的值替换为MaxValue/2以解决此问题。由于位置无关紧要,并且删除操作必须在最大索引处执行,您也可以使用ArrayList来删除特定索引处的元素,然后使用ArrayList.add()方法。祝好!

英文:

arrays in Java are static so the size of the arrays cannot change once they are instantiated. Thus we cannot delete an element and reduce the array size.

int max=-Integer.MIN_VALUE;
int maxIndex=-1;

Now iterate over the array and if element is greater than maxValue update maxValue and maxIndex.

for(int i=0;i&lt;array.length;i++)

      {
        if(array[i]&gt;max)
        {
                max= array[i];
                maxIndex=i;
        }
    }

Now replace value at maxIndex with MaxValue/2 to solve this.Since position doesnt matter and delete operation has to be performed at the max index might as well place it there or you could use arrayLists to remove element at that index specifically and then using the ArrayList.add() method. Cheers!

答案2

得分: 1

使用ArrayList而不是数组,在Java中,数组是静态的,因此一旦实例化数组,数组的大小就无法更改。还不能删除元素并减小数组大小。

int max = -Integer.MIN_VALUE;
int maxIndex = -1;

遍历数组并将每个元素插入ArrayList中

ArrayList<Integer> list = new ArrayList<>();
for(int i : arr){
   list.add(i);
 }

现在数组被转换为ArrayList在这里我们将找到最大值然后将最大值存储在临时变量中一旦从列表中删除最大值我们将使用temp使用Math.floorDiv(a,b)函数进行除法取整

for(int i = 0; i< list.size(); i++){
 max = temp;
 list.remove(i);
 list.add(Math.floorDiv(temp,2));  
   }
}

完整的代码与输出如下

import java.util.*;
public class Main{

这是一个从数组中找到最大数然后执行floorDiv()除以2并将其插入回数组的函数

static ArrayList<Integer> solve(ArrayList<Integer> list){
  int max = -Integer.MIN_VALUE;
  int maxIndex = -1;
     for(int i = 0; i< list.size(); i++){
        if(list.get(i) > max){
            int temp = list.get(i);
            max = temp;
            list.remove(i);
            list.add(Math.floorDiv(temp,2));
             }
        }
    return list;
}
public static void main(String[] args) {
    int [] arr = new int[]{20 ,5 ,7 ,9};
    ArrayList<Integer> list1= new ArrayList<>();
    for(int a : arr){
        list1.add(a);
    }
    Main obj = new Main();
    System.out.println(obj.solve(list1));
  }
}

输出结果为 [5, 7, 9, 10]
英文:

Use ArrayList instead of arrays as in Java arrays are static so the size of the arrays cannot change once they are instantiated.Also cannot delete an element and reduce the array size.

int max = -Integer.MIN_VALUE;
int maxIndex = -1;

iterate over the array and and insert every element to the arraylist

ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();
for(int i : arr){
list.add(i);
}

Now the array is transformed to an ArrayList.Here we will find the max. Then store the max in the temp variable . Once we delete the max from the list we will use the temp to do floor division by 2 using Math.floorDiv(a,b) function.

for(int i = 0; i&lt; list.size; i++){
max = temp;
list.remove(i);
list.add(Math.floorDiv(temp,2));  
}
}

The complete code with the output is given below :

import java.util.*;
public class Main{

This is the function which will find the max number from the array and then do the floorDiv() by 2 and insert it back.

static ArrayList&lt;Integer&gt;solve(ArrayList&lt;Integer&gt;list){
int max = -Integer.MIN_VALUE;
int maxIndex = -1;
for(int i = 0; i&lt; list.size(); i++){
if(list.get(i) &gt; max){
int temp = list.get(i);
max = temp;
list.remove(i);
list.add(Math.floorDiv(temp,2));
}
}
return list;
}
public static void main(String[] args) {
int [] arr = new int[]{20 ,5 ,7 ,9};
ArrayList&lt;Integer&gt; list1= new ArrayList&lt;&gt;();
for(int a : arr){
list1.add(a);
}
Main obj = new Main();
System.out.println(obj.solve(list1));
}
}

The output is : [5, 7, 9, 10]

huangapple
  • 本文由 发表于 2020年8月12日 17:17:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63373489.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定