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
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:
- 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.
- 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.
- 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.
- 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++
#include <bits/stdc++.h>
using namespace std;
void evenOdd( int arr[], int N)
{
for ( int i = 0; i < N; i++) {
int even_digits = 0;
int odd_digits = 0;
int temp = arr[i];
while (temp) {
if ((temp % 10) & 1)
odd_digits++;
else
even_digits++;
temp /= 10;
}
if (even_digits > odd_digits) {
int res = 0;
while (arr[i]) {
res += arr[i] % 10;
arr[i] /= 10;
}
cout << res << " " ;
}
else if (odd_digits > even_digits) {
int res = 1;
while (arr[i]) {
res *= arr[i] % 10;
arr[i] /= 10;
}
cout << res << " " ;
}
else
cout << arr[i] << " " ;
}
}
int main()
{
int arr[] = { 113, 141, 214, 3186 };
int N = sizeof (arr) / sizeof (arr[0]);
evenOdd(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void evenOdd( int [] arr, int N)
{
for ( int i = 0 ; i < N; i++) {
int even_digits = 0 ;
int odd_digits = 0 ;
int temp = arr[i];
while (temp > 0 ) {
if ((temp % 10 ) % 2 != 0 )
odd_digits++;
else
even_digits++;
temp /= 10 ;
}
if (even_digits > odd_digits) {
int res = 0 ;
while (arr[i] > 0 ) {
res += arr[i] % 10 ;
arr[i] /= 10 ;
}
System.out.print(res + " " );
}
else if (odd_digits > even_digits) {
int res = 1 ;
while (arr[i] > 0 ) {
res *= arr[i] % 10 ;
arr[i] /= 10 ;
}
System.out.print(res + " " );
}
else
System.out.print(arr[i] + " " );
}
}
public static void main(String[] args)
{
int [] arr = { 113 , 141 , 214 , 3186 };
int N = arr.length;
evenOdd(arr, N);
}
}
|
Python3
def evenOdd(arr,N):
for i in range (N):
even_digits = 0 ;
odd_digits = 0 ;
temp = arr[i];
while (temp):
if ((temp % 10 ) & 1 ):
odd_digits + = 1 ;
else :
even_digits + = 1 ;
temp = temp / / 10
if (even_digits > odd_digits):
res = 0 ;
while (arr[i]):
res + = arr[i] % 10 ;
arr[i] = arr[i] / / 10 ;
print (res, end = " " );
elif (odd_digits > even_digits):
res = 1 ;
while (arr[i]):
res * = arr[i] % 10 ;
arr[i] = arr[i] / / 10
print (res, end = " " );
else :
print (arr[i], end = " " );
arr = [ 113 , 141 , 214 , 3186 ];
N = len (arr);
evenOdd(arr, N);
|
C#
using System;
class GFG {
static void evenOdd( int [] arr, int N)
{
for ( int i = 0; i < N; i++) {
int even_digits = 0;
int odd_digits = 0;
int temp = arr[i];
while (temp > 0) {
if ((temp % 10) % 2 != 0)
odd_digits++;
else
even_digits++;
temp /= 10;
}
if (even_digits > odd_digits) {
int res = 0;
while (arr[i] > 0) {
res += arr[i] % 10;
arr[i] /= 10;
}
Console.Write(res + " " );
}
else if (odd_digits > even_digits) {
int res = 1;
while (arr[i] > 0) {
res *= arr[i] % 10;
arr[i] /= 10;
}
Console.Write(res + " " );
}
else
Console.Write(arr[i] + " " );
}
}
public static void Main()
{
int [] arr = { 113, 141, 214, 3186 };
int N = arr.Length;
evenOdd(arr, N);
}
}
|
Javascript
<script>
function evenOdd(arr,N)
{
for (let i = 0; i < N; i++) {
let even_digits = 0;
let odd_digits = 0;
let temp = arr[i];
while (temp) {
if ((temp % 10) & 1)
odd_digits++;
else
even_digits++;
temp = parseInt(temp/10)
}
if (even_digits > odd_digits) {
let res = 0;
while (arr[i]) {
res += arr[i] % 10;
arr[i] = parseInt(arr[i]/10);
}
document.write(res+ " " );
}
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+ " " );
}
else
document.write(arr[i]+ " " );
}
}
let arr = [113, 141, 214, 3186 ];
let N = arr.length;
evenOdd(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...