1 – Cakezoned Codechef Solution|Problem Code: BENDSP2

1 – Cakezoned Codechef Solution: Hello coders, today we are going to solve 1 – Cakezoned Codechef Solution.

Codechef solutions

Problem

Chefina loves cakes! N suitors numbered 1…N have arrived from across the seven seas to woo her.

Each of the suitors wishes to impress Chefina, so suitor i has prepared a cake of height Hi. To test the suitors, Chefina assigned them Q tasks. Each of the tasks is of one of the following types:

  • 11 L R X: Increase the heights of all cakes in the range L to R inclusive by X, i.e., increase each of HL, HL+1, HL+2…… HR by X.
  • 22: Find the sum of heights of all cakes prepared by odd-numbered suitors.
  • 33: Find the sum of heights of all cakes prepared by even-numbered suitors.

Can you help the suitors complete the tasks?

Input:

  • The first line contains an integer N, the number of cakes.
  • The next line contains N space-separated integers H1,H2,…,HN, denoting the heights of the cakes.
  • The third line contains an integer Q, the number of tasks.
  • The next Q lines describe the tasks assigned to the suitors by Chefina.

Output:

For each task of type 2 or 3, display the sum of heights of all cakes prepared by odd or even-numbered suitors, respectively, on a new line.

Constraints:

  • 1≤N≤1051≤N≤105
  • 1≤Q≤2⋅1051≤Q≤2⋅105
  • 1≤L≤R≤N1≤L≤R≤N
  • 1≤X,Hi≤108

Example

Sample Input 1 

6
2 9 5 2 1 8
5
2
3
1 1 3 2
3
2

Sample Output 1 

8
19
21
12

1 – Cakezoned – CodeChef Solution in JAVA

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    int[] H = new int[N];
    for (int i = 0; i < H.length; ++i) {
      H[i] = sc.nextInt();
    }
    int Q = sc.nextInt();
    sc.nextLine();
    String[] tasks = new String[Q];
    for (int i = 0; i < tasks.length; ++i) {
      tasks[i] = sc.nextLine();
    }
    System.out.println(solve(H, tasks));
    sc.close();
  }
  static String solve(int[] H, String[] tasks) {
    long oddSum =
        IntStream.range(0, H.length).filter(i -> i % 2 == 0).map(i -> H[i]).asLongStream().sum();
    long evenSum =
        IntStream.range(0, H.length).filter(i -> i % 2 != 0).map(i -> H[i]).asLongStream().sum();
    List<Long> result = new ArrayList<>();
    for (String task : tasks) {
      if (task.charAt(0) == '1') {
        String[] parts = task.split(" ");
        int L = Integer.parseInt(parts[1]);
        int R = Integer.parseInt(parts[2]);
        int X = Integer.parseInt(parts[3]);
        int minOdd = L + ((L % 2 == 0) ? 1 : 0);
        int maxOdd = R - ((R % 2 == 0) ? 1 : 0);
        oddSum += (maxOdd / 2L - minOdd / 2 + 1) * X;
        int minEven = L + ((L % 2 == 0) ? 0 : 1);
        int maxEven = R - ((R % 2 == 0) ? 0 : 1);
        evenSum += (maxEven / 2L - minEven / 2 + 1) * X;
      } else if (task.charAt(0) == '2') {
        result.add(oddSum);
      } else {
        result.add(evenSum);
      }
    }
    return result.stream().map(String::valueOf).collect(Collectors.joining("\n"));
  }
}

1 – Cakezoned – CodeChef Solution in CPP

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	long long n,q,l,r,x,odd_sum=0,even_sum=0,i;
	cin>>n;
	long long a[n];
	for(i=1;i<=n;i++)
	{
	    cin>>a[i];
	    if(i%2!=0)
	    odd_sum+=a[i];
	    else
	    even_sum+=a[i];
	}
	cin>>q;
	while(q--)
	{
	    int x;
	    cin>>x;
	    if(x==1)
	    {
	        cin>>l>>r>>x;
	        if(l%2!=0 && r%2!=0)
	        {
	            even_sum+=(r-l)/2*x;
	             odd_sum+=((r-l)/2+1)*x;
	        }
	        else if(l%2==0 && r%2==0)
	        {
	            odd_sum+=(r-l)/2*x;
	             even_sum+=((r-l)/2+1)*x;
	        }
	        else
	        {
	         odd_sum+=((r-l)/2+1)*x;
	             even_sum+=((r-l)/2+1)*x;
	        }
	    }
	    else if(x==2)
	    {
	        cout<<odd_sum<<endl;
	    }
	    else
	    {
	       cout<<even_sum<<endl;
	    }
	}
	return 0;
}

1 – Cakezoned – CodeChef Solution in PYTHON

import sys
class Cakes:
    cakes_count = 0
    cakes_heights_odd_even = [0, 0]
    def __init__(self, cakes_count):
        self.cakes_count = cakes_count
    def get_increments(self, span, first):
        if span % 2 == 0:
            return [int(span / 2), int(span / 2)]
        else:
            if first % 2 == 0:
                return [int(span / 2), int(span / 2) + 1]
            else:
                return [int(span / 2) + 1, int(span / 2)]
    def process(self):
        cakes_heights = sys.stdin.readline().rstrip('\n').split(' ')
        i = 0
        for height in cakes_heights:
            self.cakes_heights_odd_even[i] += int(height)
            i = (i + 1) % 2
        tasks_number = int(sys.stdin.readline())
        for task in range(tasks_number):
            task_row = sys.stdin.readline().rstrip('\n').split(' ')
            task_id = int(task_row[0])
            if task_id > 1:
                print(self.cakes_heights_odd_even[task_id - 2])
                continue
            first = int(task_row[2])
            span = first - int(task_row[1]) + 1
            height_increase = int(task_row[3])
            cakes_increments = self.get_increments(span, first)
            self.cakes_heights_odd_even[0] += cakes_increments[0] * height_increase
            self.cakes_heights_odd_even[1] += cakes_increments[1] * height_increase
if __name__ == '__main__':
    cakes = Cakes(int(sys.stdin.readline()))
    cakes.process()

Disclaimer: The above Problem (1 – Cakezoned ) is generated by CodeChef but the solution is provided by  Chase2learn.This tutorial is only for Educational and Learning purpose.

Leave a Comment