Open In App

Modify a given array by replacing each element with the sum or product of their digits based on a given condition

Last Updated : 16 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] consisting of N integers, the task is to modify the array elements after performing only one of the following operations on each array elements:

Examples:

Input: arr[] = {113, 141, 214, 3186}
Output: 3 4 7 3186
Explanation:
Following are the operation performed on each array elements:

  1. For element arr[0](= 113): count of even and odd digits are 0 and 3. As count of even < count of odd digit, therefore update arr[0](= 113) to the product of each digit of the number 113 i.e., 1 * 1 * 3 = 3.
  2. For element arr[1](= 141): count of even and odd digits are 1 and 2. As count of even < count of odd digit, therefore update arr[1](= 141) to the product of each digit of the number 141 i.e., 1 * 4 * 1 = 4.
  3. For element arr[2]:(= 214) count of even and odd digits are 2 and 1. As count of even > count of odd digit, therefore update arr[2](= 214) to the sum of each digit of the number 214 i.e., 2 + 1 + 4 = 7.
  4. For element arr[3](= 3186): count of even and odd digits are 2 and 2. As count of even is the same as the count of odd digit, then no operation is performed. Therefore, arr[3](= 3186) remains the same.

After the above operations, the array modifies to {3, 4, 7, 3186}.

Input: arr[] = {2, 7, 12, 22, 110}
Output: 2 7 12 4 0

Approach: The given problem can be solved by performing the given operations for each array element and print the result accordingly. Follow the steps below to solve the problem:

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to modify the given array
// as per the given conditions
void evenOdd(int arr[], int N)
{
    // Traverse the given array arr[]
    for (int i = 0; i < N; i++) {
 
        // Initialize the count of even
        // and odd digits
        int even_digits = 0;
        int odd_digits = 0;
 
        // Initialize temp with the
        // current array element
        int temp = arr[i];
 
        // For count the number of
        // even digits
        while (temp) {
 
            // Increment the odd count
            if ((temp % 10) & 1)
                odd_digits++;
 
            // Otherwise
            else
                even_digits++;
 
            // Divide temp by 10
            temp /= 10;
        }
 
        // Performe addition
        if (even_digits > odd_digits) {
 
            int res = 0;
            while (arr[i]) {
 
                res += arr[i] % 10;
                arr[i] /= 10;
            }
            cout << res << " ";
        }
 
        // Performe multiplication
        else if (odd_digits > even_digits) {
 
            int res = 1;
            while (arr[i]) {
 
                res *= arr[i] % 10;
                arr[i] /= 10;
            }
            cout << res << " ";
        }
 
        // Otherwise
        else
            cout << arr[i] << " ";
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 113, 141, 214, 3186 };
    int N = sizeof(arr) / sizeof(arr[0]);
    evenOdd(arr, N);
 
    return 0;
}


Java




// Java program for the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to modify the given array
    // as per the given conditions
    static void evenOdd(int[] arr, int N)
    {
        // Traverse the given array arr[]
        for (int i = 0; i < N; i++) {
 
            // Initialize the count of even
            // and odd digits
            int even_digits = 0;
            int odd_digits = 0;
 
            // Initialize temp with the
            // current array element
            int temp = arr[i];
 
            // For count the number of
            // even digits
            while (temp > 0) {
 
                // Increment the odd count
                if ((temp % 10) % 2 != 0)
                    odd_digits++;
 
                // Otherwise
                else
                    even_digits++;
 
                // Divide temp by 10
                temp /= 10;
            }
 
            // Performe addition
            if (even_digits > odd_digits) {
 
                int res = 0;
                while (arr[i] > 0) {
 
                    res += arr[i] % 10;
                    arr[i] /= 10;
                }
                System.out.print(res + " ");
            }
 
            // Performe multiplication
            else if (odd_digits > even_digits) {
 
                int res = 1;
                while (arr[i] > 0) {
 
                    res *= arr[i] % 10;
                    arr[i] /= 10;
                }
                System.out.print(res + " ");
            }
 
            // Otherwise
            else
                System.out.print(arr[i] + " ");
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 113, 141, 214, 3186 };
        int N = arr.length;
        evenOdd(arr, N);
    }
}
 
// This code is contributed by rishavmahato348.


Python3




# Python program for the above approach
 
# Function to modify the given array
# as per the given conditions
def evenOdd(arr,N):
   
    # Traverse the given array arr[]
    for i in range(N):
 
        # Initialize the count of even
        # and odd digits
        even_digits = 0;
        odd_digits = 0;
 
        # Initialize temp with the
        # current array element
        temp = arr[i];
 
        # For count the number of
        # even digits
        while (temp):
 
            # Increment the odd count
            if ((temp % 10) & 1):
                odd_digits += 1;
 
            # Otherwise
            else:
                even_digits += 1;
 
            # Divide temp by 10
            temp = temp//10
 
        # Performe addition
        if (even_digits > odd_digits):
 
            res = 0;
            while (arr[i]):
 
                res += arr[i] % 10;
                arr[i] = arr[i]//10;
     
            print(res, end=" ");
 
        # Performe multiplication
        elif (odd_digits > even_digits):
 
            res = 1;
            while (arr[i]):
 
                res *= arr[i] % 10;
                arr[i] = arr[i]//10
             
            print(res, end=" ");
         
        # Otherwise
        else:
            print(arr[i], end=" ");
     
# Driver Code
arr = [113, 141, 214, 3186 ];
N = len(arr);
evenOdd(arr, N);
 
    
# This code is contributed by _saurabh_jaiswal


C#




// C# program for the above approach
 
using System;
 
class GFG {
 
    // Function to modify the given array
    // as per the given conditions
    static void evenOdd(int[] arr, int N)
    {
        // Traverse the given array arr[]
        for (int i = 0; i < N; i++) {
 
            // Initialize the count of even
            // and odd digits
            int even_digits = 0;
            int odd_digits = 0;
 
            // Initialize temp with the
            // current array element
            int temp = arr[i];
 
            // For count the number of
            // even digits
            while (temp > 0) {
 
                // Increment the odd count
                if ((temp % 10) % 2 != 0)
                    odd_digits++;
 
                // Otherwise
                else
                    even_digits++;
 
                // Divide temp by 10
                temp /= 10;
            }
 
            // Performe addition
            if (even_digits > odd_digits) {
 
                int res = 0;
                while (arr[i] > 0) {
 
                    res += arr[i] % 10;
                    arr[i] /= 10;
                }
                Console.Write(res + " ");
            }
 
            // Performe multiplication
            else if (odd_digits > even_digits) {
 
                int res = 1;
                while (arr[i] > 0) {
 
                    res *= arr[i] % 10;
                    arr[i] /= 10;
                }
                Console.Write(res + " ");
            }
 
            // Otherwise
            else
                Console.Write(arr[i] + " ");
        }
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 113, 141, 214, 3186 };
        int N = arr.Length;
        evenOdd(arr, N);
    }
}
 
// This code is contributed by subham348.


Javascript




<script>
  
 // JavaScript program for the above approach
 
// Function to modify the given array
// as per the given conditions
function evenOdd(arr,N)
{
    // Traverse the given array arr[]
    for (let i = 0; i < N; i++) {
 
        // Initialize the count of even
        // and odd digits
        let even_digits = 0;
        let odd_digits = 0;
 
        // Initialize temp with the
        // current array element
        let temp = arr[i];
 
        // For count the number of
        // even digits
        while (temp) {
 
            // Increment the odd count
            if ((temp % 10) & 1)
                odd_digits++;
 
            // Otherwise
            else
                even_digits++;
 
            // Divide temp by 10
            temp = parseInt(temp/10)
        }
 
        // Performe addition
        if (even_digits > odd_digits) {
 
            let res = 0;
            while (arr[i]) {
 
                res += arr[i] % 10;
                arr[i] = parseInt(arr[i]/10);
            }
            document.write(res+" ");
        }
 
        // Performe multiplication
        else if (odd_digits > even_digits) {
 
            let res = 1;
            while (arr[i]) {
 
                res *= arr[i] % 10;
                arr[i] = parseInt(arr[i]/10)
            }
            document.write(res+" ");
        }
 
        // Otherwise
        else
        document.write(arr[i]+" ");
    }
}
 
// Driver Code
 
    let  arr = [113, 141, 214, 3186 ];
    let N = arr.length;
    evenOdd(arr, N);
 
    
    // This code is contributed by Potta Lokesh
   
</script>


Output: 

3 4 7 3186

 

Time Complexity: O(N)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads