Qty no. 1s and 0s without comparison

There are only 1 and 0 in the array, as you would think no. from 1s and 0s without comparison, and then change it to an array of 3s and 5s.
My approach is to use a cumulative array, so that cumulative [i] = array [i] + cumulative [i-1]. No one = cumulative [n] number of zeros = n-cumulative [n]; Is this approach right? Or suggest a different approach? Can we convert it to an array of 3 and 5 without comparison?

+4
source share
6 answers

You fit right. I also do not think that it can be improved, because you need to iterate over all the elements of the array.

+2
source

, , , , 0 False , 1 True. : C, Python.

, , 0 1, , . 1 0.

Python

>>> original_list = [0, 1, 1, 1, 0, 1, 0]
>>> altered_list = [3 if item else 5 for item in original_list]
>>> print altered_list
[5, 3, 3, 3, 5, 3, 5]
+3

, , , , " " "". , , :

numOfOnes += item[i];
numOfZeroes += (1 - item[i]);

: - 5s 3s. , , - -, 3 5 . .

. - .

+1

: 2, b [2]. :

for(int i=0; i < n; i++) {
b[a[i]]++;
}

, b [0] , b [1] 1s.

+1
//To find the count of 0 & 1 we can do below 
public void findZeroOneCount(int [] arr){
        int len=arr.length;
        System.out.println(len);
        int sum=0;
        for(int i=0;i<len;i++){
            sum=sum+arr[i];
        }

        System.out.println("Count of Zero:"+(len-sum%len));
        System.out.println("Count of One:"+sum%len);
}
0

0- > 3 1- > 5

const int lookup[]={3,5};

forloop {
   array[i] = lookup[array[i]];
}
-1
source

All Articles