- import java.util.* ;
- public class Main {
- public static void main (String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter size : ");
- int size = sc.nextInt();
- // initializing array
- int[] arr = new int[size] ;
- // taking array inputs from the user
- for(int i = 0 ;i
- if(sc.hasNextInt()){
- arr[i] = sc.nextInt() ;
- }
- }
- // calling the function
- System.out.println(doSomething(arr));
- }
- // creating the function
- public static int doSomething(int[] arr){
- int Ones = 0 ;
- int Zeros = 0 ;
- // counting numner of zeros and Ones
- for(int i = 0 ; i < arr.length ; i++){
- if(arr[i] == 0){
- Zeros ++ ;
- }else{
- Ones++ ;
- }
- }
- // logic
- if(Ones < Zeros){
- return Ones * 2 ;
- }else{
- return Zeros * 2 ;
- }
- }
- }
No comments:
Post a Comment