Print the sum of array after doing k queries on the array
Last Updated :
10 Feb, 2023
Given an array, arr[] of size n. Calculate the sum of arr[] by performing k queries on arr[], such that we have to add an element given in array y[], on the basis of the boolean value given in array check[].
For every index i (0<= i <= n – 1):
- If check[i] = true, Add y[i] to all the odd numbers present in the array.
- If check[i] = false, Add y[i] to all the even numbers present in the array.
Examples:
Input: n = 3, k = 3, arr[] = {1, 2, 4}, check[] = {false, true, false}, y[] = {2, 3, 5}
Output: 29
Explanation:
- for, k = 1
check[0] = false, y[0] = 2
So, as per operation add to the even number
arr = [1, 2+2, 4+2] = {1, 4, 6}
- for, k = 2
check[1] = true, y[1] = 3
so, add to the odd number
arr = [1+3, 4, 6] = {4, 4, 6}
- for, k = 3
check[2] = false, y[2] = 5
so, add to the even number
arr = [4+5, 4+5, 6+5] = {9, 9, 11}
So, finally, print the sum of the array = 9 + 9 + 11 = 29
Input: n = 2, k = 1, arr[] = {1, 2}, check[] = {false}, y[] = {0}
Output: 3
Explanation: As check[0] = false, we have to y[0] to all the even elements present in the array. Therefore, the final sum is 1 + 2 =3.
Approach: The basic idea to solve the above problem is:
Iterate through whole the array arr[] for k times, check whether the element present is odd or even, and add an element accordingly.
Time complexity: O(k*n), where k = no of queries , n = length of array
Auxiliary Space: O(1)
Efficient Approach: The above idea can be optimized as:
Iterate over the arr[], count the number of even and odd elements present. Now, Run a loop for k queries along by keep a check on array check[]. If check[i] == true, Add the total sum by count odd * y[i], otherwise if it’s false, add count even * y[i]. Also, change the count of even and odd according to y[i], if it’s odd.
Steps involved in the implementation of the code:
Step 1: First calculate the total sum of the array.
Step 2: count of number of odd and even elements in the array.
Step 3: Run the loop for the k queries.
Step 4: If (check[first loop index] = true) then,
- Update the total sum => total sum += (count odd * y[first loop index])
- If the number which is added y[first loop index] is odd then,
- Update even count => count even += odd count, and odd count = 0
Step 5: If(check[first loop index] = false) then,
- Update the total sum => total sum += (count even* y[first loop index])
- If the number which is added y[first loop index] is odd then,
- Update odd count => odd count += even count, and even count = 0
Step 6: Finally print the total sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long sum( int n, int k, vector< int > arr,
vector< bool > check, vector< int > y)
{
long long totalSum = 0;
int countOdd = 0, countEven = 0;
for ( int i = 0; i < n; i++) {
totalSum = totalSum + arr[i];
if (arr[i] & 1)
countOdd++;
else
countEven++;
}
for ( int q = 0; q < k; q++) {
if (check[q]) {
totalSum = totalSum + (countOdd * 1LL * y[q]);
if (y[q] & 1) {
countEven = countEven + countOdd;
countOdd = 0;
}
}
else {
totalSum = totalSum + (countEven * 1LL * y[q]);
if (y[q] & 1) {
countOdd = countOdd + countEven;
countEven = 0;
}
}
}
return totalSum;
}
int main()
{
int n1 = 3, k1 = 3;
vector< int > arr1 = { 1, 2, 4 };
vector< bool > check1 = { false , true , false };
vector< int > y1 = { 2, 3, 5 };
cout << sum(n1, k1, arr1, check1, y1) << endl;
int n2 = 6, k2 = 7;
vector< int > arr2 = { 1, 3, 2, 4, 10, 48 };
vector< bool > check2
= { true , false , false , false , true , false , false };
vector< int > y2 = { 6, 5, 4, 5, 3, 12, 1 };
cout << sum(n2, k2, arr2, check2, y2) << endl;
return 0;
}
|
Java
import java.util.*;
class Main {
static long sum( int n, int k, int [] arr,
boolean [] check, int [] y)
{
long totalSum = 0 ;
int countOdd = 0 , countEven = 0 ;
for ( int i = 0 ; i < n; i++) {
totalSum = totalSum + arr[i];
if ((arr[i] & 1 ) == 1 )
countOdd++;
else
countEven++;
}
for ( int q = 0 ; q < k; q++) {
if (check[q]) {
totalSum
= totalSum + (countOdd * 1L * y[q]);
if ((y[q] & 1 ) == 1 ) {
countEven = countEven + countOdd;
countOdd = 0 ;
}
}
else {
totalSum
= totalSum + (countEven * 1L * y[q]);
if ((y[q] & 1 ) == 1 ) {
countOdd = countOdd + countEven;
countEven = 0 ;
}
}
}
return totalSum;
}
public static void main(String[] args)
{
int n1 = 3 , k1 = 3 ;
int [] arr1 = { 1 , 2 , 4 };
boolean [] check1 = { false , true , false };
int [] y1 = { 2 , 3 , 5 };
System.out.println(sum(n1, k1, arr1, check1, y1));
int n2 = 6 , k2 = 7 ;
int [] arr2 = { 1 , 3 , 2 , 4 , 10 , 48 };
boolean [] check2 = { true , false , false , false ,
true , false , false };
int [] y2 = { 6 , 5 , 4 , 5 , 3 , 12 , 1 };
System.out.println(sum(n2, k2, arr2, check2, y2));
}
}
|
Python3
def sum (n, k, arr, check, y):
total_sum = 0
count_odd, count_even = 0 , 0
for i in range (n):
total_sum + = arr[i]
if arr[i] % 2 ! = 0 :
count_odd + = 1
else :
count_even + = 1
for q in range (k):
if check[q]:
total_sum + = count_odd * y[q]
if y[q] % 2 ! = 0 :
count_even + = count_odd
count_odd = 0
else :
total_sum + = count_even * y[q]
if y[q] % 2 ! = 0 :
count_odd + = count_even
count_even = 0
return total_sum
n1, k1 = 3 , 3
arr1 = [ 1 , 2 , 4 ]
check1 = [ False , True , False ]
y1 = [ 2 , 3 , 5 ]
print ( sum (n1, k1, arr1, check1, y1))
n2, k2 = 6 , 7
arr2 = [ 1 , 3 , 2 , 4 , 10 , 48 ]
check2 = [ True , False , False , False , True , False , False ]
y2 = [ 6 , 5 , 4 , 5 , 3 , 12 , 1 ]
print ( sum (n2, k2, arr2, check2, y2))
|
C#
using System;
using System.Collections.Generic;
class Program
{
static long sum( int n, int k, List< int > arr,
List< bool > check, List< int > y)
{
long totalSum = 0;
int countOdd = 0, countEven = 0;
for ( int i = 0; i < n; i++) {
totalSum = totalSum + arr[i];
if ((arr[i] & 1) != 0)
countOdd++;
else
countEven++;
}
for ( int q = 0; q < k; q++) {
if (check[q]) {
totalSum
= totalSum + (countOdd * ( long )y[q]);
if ((y[q] & 1) != 0) {
countEven = countEven + countOdd;
countOdd = 0;
}
}
else {
totalSum
= totalSum + (countEven * ( long )y[q]);
if ((y[q] & 1) != 0) {
countOdd = countOdd + countEven;
countEven = 0;
}
}
}
return totalSum;
}
static void Main( string [] args)
{
int n1 = 3, k1 = 3;
List< int > arr1 = new List< int >{ 1, 2, 4 };
List< bool > check1
= new List< bool >{ false , true , false };
List< int > y1 = new List< int >{ 2, 3, 5 };
Console.WriteLine(sum(n1, k1, arr1, check1, y1));
int n2 = 6, k2 = 7;
List< int > arr2
= new List< int >{ 1, 3, 2, 4, 10, 48 };
List< bool > check2
= new List< bool >{ true , false , false , false ,
true , false , false };
List< int > y2
= new List< int >{ 6, 5, 4, 5, 3, 12, 1 };
Console.WriteLine(sum(n2, k2, arr2, check2, y2));
}
}
|
Javascript
function sum(n, k, arr, check, y) {
var totalSum = 0;
var countOdd = 0, countEven = 0;
for ( var i = 0; i < n; i++) {
totalSum = totalSum + arr[i];
if (arr[i] & 1) {
countOdd++;
}
else {
countEven++;
}
}
for ( var q = 0; q < k; q++) {
if (check[q]) {
totalSum = totalSum + countOdd * y[q];
if (y[q] & 1) {
countEven = countEven + countOdd;
countOdd = 0;
}
}
else {
totalSum = totalSum + countEven * y[q];
if (y[q] & 1) {
countOdd = countOdd + countEven;
countEven = 0;
}
}
}
return totalSum;
}
var n1 = 3, k1 = 3;
var arr1 = [1, 2, 4];
var check1 = [ false , true , false ];
var y1 = [2, 3, 5];
console.log(sum(n1, k1, arr1, check1, y1));
var n2 = 6, k2 = 7;
var arr2 = [1, 3, 2, 4, 10, 48];
var check2 = [ true , false , false , false , true , false , false ];
var y2 = [6, 5, 4, 5, 3, 12, 1];
console.log(sum(n2, k2, arr2, check2, y2));
|
Time Complexity: O(max(n, k)), where n = size of array, k = no of queries
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...