I am trying to get the count of number of times all the integers
in an array
is divisible by 2 considering only one integer
in each step.
For example, initially if I have the array
: [2,4,2]
and count = 0
Step 1
[1,4,2] , count=1
Step 2
[1,2,2] , count=2
Step 3
[1,1,2] , count=3
Step 4
[1,1,1] , count=4
My approach to the problem is given below :
Code
public static void main(String[] args) {
int[] ar={2,4,2};
int[] p=new int[ar.length];
int count=0;
for (int i=0;i<ar.length ;i++ ) {
if(ar[i]>=1){
ar[i]=ar[i]/2;
count++;
}
}
for (int x:ar) {
System.out.println(x);
}
System.out.println("Count:"+count);
}
Output
1
2
1
Count:3
The problem in the code given above is that the array
is scanned only one time and I want to scan the array until all the integers are no more divisible by 2