Consecutive Adding CodeChef Solution

Problem -Consecutive Adding CodeChef Solution

This website is dedicated for CodeChef solution where we will publish right solution of all your favourite CodeChef problems along with detailed explanatory of different competitive programming concepts and languages.

Consecutive Adding CodeChef Solution in C++17

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ll long long
#define vll vector<ll>
#define ld long double
#define pll pair<ll,ll>
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define oset tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define osetll tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define ook order_of_key
#define fbo find_by_order
const int MOD=1000000007; //998244353
long long int inverse(long long int i){
    if(i==1) return 1;
    return (MOD - ((MOD/i)*inverse(MOD%i))%MOD+MOD)%MOD;
}
ll POW(ll a,ll b)
{
    if(b==0) return 1;
    if(b==1) return a%MOD;
    ll temp=POW(a,b/2);
    if(b%2==0) return (temp*temp)%MOD;
    else return (((temp*temp)%MOD)*a)%MOD;
}
 
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        ll r,c,x;
        cin>>r>>c>>x;
        ll a[r][c],b[r][c];
        ll a1[x][x];
        for(int i=0;i<x;i++)
        {
            for(int j=0;j<x;j++)
            {
                a1[i][j]=0;
            }
        }
        for(int i=0;i<r;i++)
        {
            for(int j=0;j<c;j++)
            {
                cin>>a[i][j];
            }
        }
        for(int i=0;i<r;i++)
        {
            for(int j=0;j<c;j++)
            {
                cin>>b[i][j];
            }
        }
        for(int i=0;i<r;i++)
        {
            for(int j=0;j<c;j++)
            {
                a1[i%x][j%x]+=a[i][j];
                a1[i%x][j%x]-=b[i][j];
            }
        }
        ll flag=0;
        for(int i=1;i<x;i++)
        {
            ll temp=a1[i][0]-a1[0][0];
            for(int j=1;j<x;j++)
            {
                if(temp!=a1[i][j]-a1[0][j])
                {
                    flag=1;
                }
            }
        }
        if(flag==0) cout<<"Yes";
        else cout<<"No";
        cout<<"\n";
    }
}

Consecutive Adding CodeChef Solution in C++14

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ll long long
#define vll vector<ll>
#define ld long double
#define pll pair<ll,ll>
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define oset tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define osetll tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define ook order_of_key
#define fbo find_by_order
const int MOD=1000000007; //998244353
long long int inverse(long long int i){
    if(i==1) return 1;
    return (MOD - ((MOD/i)*inverse(MOD%i))%MOD+MOD)%MOD;
}
ll POW(ll a,ll b)
{
    if(b==0) return 1;
    if(b==1) return a%MOD;
    ll temp=POW(a,b/2);
    if(b%2==0) return (temp*temp)%MOD;
    else return (((temp*temp)%MOD)*a)%MOD;
}
 
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        ll r,c,x;
        cin>>r>>c>>x;
        ll a[r][c],b[r][c];
        ll a1[x][x];
        for(int i=0;i<x;i++)
        {
            for(int j=0;j<x;j++)
            {
                a1[i][j]=0;
            }
        }
        for(int i=0;i<r;i++)
        {
            for(int j=0;j<c;j++)
            {
                cin>>a[i][j];
            }
        }
        for(int i=0;i<r;i++)
        {
            for(int j=0;j<c;j++)
            {
                cin>>b[i][j];
            }
        }
        for(int i=0;i<r;i++)
        {
            for(int j=0;j<c;j++)
            {
                a1[i%x][j%x]+=a[i][j];
                a1[i%x][j%x]-=b[i][j];
            }
        }
        ll flag=0;
        for(int i=1;i<x;i++)
        {
            ll temp=a1[i][0]-a1[0][0];
            for(int j=1;j<x;j++)
            {
                if(temp!=a1[i][j]-a1[0][j])
                {
                    flag=1;
                }
            }
        }
        if(flag==0) cout<<"Yes";
        else cout<<"No";
        cout<<"\n";
    }
}

Consecutive Adding CodeChef Solution in PYTH 3

# cook your dish here
# cook your dish here

for _ in range(int(input())):
    r,c,x=map(int,input().split())
    a,b=[],[]
    for i in range(r):
        a.append(list(map(int,input().split())))
    for i in range(r):
        b.append(list(map(int,input().split())))
    for i in range(r):
        for j in range(c):
            a[i][j]-=b[i][j]
    for i in range(x-1):
        k=i+x
        while(k<r):
            for j in range(c):
                a[i][j]+=a[k][j]
            k+=x
    k=x-1
    while(k<r):
        for i in range(x-1):
            for j in range(c):
                a[i][j]-=a[k][j]
        k+=x
    for j in range(x-1):
        k=j+x
        while(k<c):
            for i in range(x-1):
                a[i][j]+=a[i][k]
            k+=x
    k=x-1
    while(k<c):
        for j in range(x-1):
            for i in range(x-1):
                a[i][j]-=a[i][k]
        k+=x
    ans='Yes'
    for i in range(x-1):
        for j in range(x-1):
            if a[i][j]!=0:
                ans='No'
                break
        if ans=='No':
            break
    print(ans)
    
                
    

Consecutive Adding CodeChef Solution in C

#include <stdio.h>

int main(void) {
	int t;
	scanf("%d",&t);
	
	while(t--){
		long long int r,c,x;
		scanf("%lld %lld %lld",&r,&c,&x);
		
		long long int a[r+2][c+2],b[r+2][c+2];
		long long int i,j;
		for(i=0;i<r;i++){
			for(j=0;j<c;j++){
				scanf("%lld",&a[i][j]);
			}
		}
		
		for(i=0;i<r;i++){
			for(j=0;j<c;j++){
				scanf("%lld",&b[i][j]);
			}
		}
		for(i=0;i<r;i++){
			for(j=0;j<c;j++){
				a[i][j] = a[i][j] - b[i][j];
			}
		}
		
		
		
		long long int diff;
		for(i=0;i<r;i++){
			diff =0;
			for(j=0;j<c-x+1;j++){
				if(j>= x)
				diff += a[i][j-x];
				a[i][j] += diff;
				if(a[i][j]!=0){
					diff += -a[i][j];
					
				}
			}
			
			for(;j<c;j++){
				if(j>= x){
				diff += a[i][j-x];}
			a[i][j] += diff;}
		}
		
		for(j=c-x+1;j<c;j++){
			diff = 0;
			for(i=0;i<r-x+1;i++){
					if(i >= x)
					diff += a[i-x][j];
					a[i][j] += diff;
				if(a[i][j]!=0){
					diff += -a[i][j];
					
				}
			}
				for(;i<r;i++){
					
					if(i >= x){
					diff += a[i-x][j];}
					a[i][j] += diff;}
		}
		
		int flag = 0;
		for(i=r-x+1;i<r;i++){
			for(j=c-x+1;j<c;j++){
				if(a[i][j] != 0){
					flag=1;break;
				}
			}
		}
		
		if(flag == 1)
		printf("No\n");
		else
		printf("Yes\n");
	}
	return 0;
}

Consecutive Adding CodeChef Solution in JAVA

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
    public static boolean Solve(long[][] first, long[][] second, int r, int c, int x){
        if(r<x && c<x){
            return false;
        }
        if(c<x){ 
            for(int j=0;j<c;j++){
                for(int i=0;i<=r-x;i++){
                    if(first[i][j] != second[i][j]){
                        long diff = second[i][j] - first[i][j];
                        for(int k=i;k<i+x;k++){
                            first[k][j] += diff;
                        }
                    }
                    if(i == r-x){
                            for(int k = i+1;k<r;k++){
                                if(first[k][j] != second[k][j]){
                                    return false;
                                }
                            }
                    }
                }
            }
        }else if(r<x){
            for(int i=0;i<r;i++){
                for(int j=0;j<=c-x;j++){
                    if(first[i][j] != second[i][j]){
                        long diff = second[i][j] - first[i][j];
                        for(int k=j;k<x+j;k++){
                            first[i][k] += diff;
                        }
                    }
                    if(j == c-x){
                        for(int k=j+1;k<c;k++){
                            if(first[i][k] != second[i][k]){
                                return false;
                            }
                        }
                    }
                }
            }
        }else{
            for(int i=0;i<r;i++){
                for(int j=0;j<=c-x;j++){
                    if(first[i][j] != second[i][j]){
                        long diff = second[i][j] - first[i][j];
                        for(int k=j;k<j+x;k++){
                            first[i][k] += diff;
                        }
                    }
                }
            }
            for(int j = c-x; j<c;j++){
                for(int i=0;i<=r-x;i++){
                    if(first[i][j] != second[i][j]){
                        long diff = second[i][j] - first[i][j];
                        for(int k=i;k<i+x;k++){
                            first[k][j] += diff;
                        }
                    }
                    if(i == r-x){
                        for(int k=i+1;k<r;k++){
                            if(first[k][j] != second[k][j]){
                                return false;
                            }
                        }
                    }
                }
            }
        }
        return true;
    }
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		FastReader in = new FastReader();
		int t = in.nextInt();
		StringBuffer ans = new StringBuffer();
		while((t--)>0){
		    int r = in.nextInt();
		int c = in.nextInt();
		int x = in.nextInt();
		long[][] first = new long[r][c];
		long[][] second = new long[r][c];
		for(int i=0;i<r;i++){
		    for(int j=0;j<c;j++){
		        first[i][j] = in.nextLong();
		    }
		}
		for(int i=0;i<r;i++){
		    for(int j=0;j<c;j++){
		        second[i][j] = in.nextLong();
		    }
		}
		if(Solve(first, second, r, c, x) == true){
		    ans.append("Yes\n");
		}else{
		    ans.append("No\n");
		}
		}
		System.out.println(ans);
	}
	static class FastReader {
        BufferedReader br;
        StringTokenizer st;
 
        public FastReader()
        {
            br = new BufferedReader(
                new InputStreamReader(System.in));
        }
 
        String next()
        {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
 
        int nextInt() { return Integer.parseInt(next()); }
 
        long nextLong() { return Long.parseLong(next()); }
 
        double nextDouble()
        {
            return Double.parseDouble(next());
        }
 
        String nextLine()
        {
            String str = "";
            try {
                str = br.readLine();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}

Consecutive Adding CodeChef Solution in PYPY 3

def process(ar, x, n):
    curr = 0
    res = [0 for i in range(x - 1)]

    for i in range(n-x+1):
        to_add = -(curr + ar[i])
        curr = -ar[i]
        ind = x - 2 - (n-i) % x
        if ind > -1:
            res[ind] += to_add
    
    for i in range(x - 3, -1, -1):
        res[i] += res[i + 1]
    
    for i in range(n-x+1):
        ar[i] = 0

    for i in range(n-x+1, n):
        ar[i] += res[i - n + x -1]

t=int(input())
for _ in range(t):
    r, c, x = map(int, input().split())
    
    a = []
    for i in range(r):
        l = list(map(int, input().split()))
        a.append(l)
    
    b = []
    for i in range(r):
        l = list(map(int, input().split()))
        b.append(l)
    
    for i in range(r):
        for j in range(c):
            b[i][j] -= a[i][j]
    
    for i in range(r):
        process(b[i], x, c)
    
    processed = [[0 for i in range(r)] for j in range(x - 1)]
    
    for i in range(r):
        for j in range(c-1, c-x, -1):
            processed[c-1-j][i] = b[i][j]
    
    for i in range(x-1):
        process(processed[i], x, r)
    
    ans = True
    
    for i in range(x - 1):
        for j in range(r):
            if processed[i][j] != 0:
                ans = 0
                break
    
    if ans == True:
        print('Yes')
    else:
        print('NO')

Consecutive Adding CodeChef Solution in C#

using System;
using System.Collections;
using System.Text;

namespace cs
{
	public class CONSADD {
		int r, c, x;
		long[, ] a, b;
		string rez;
		
		void afis() {
			Console.Error.WriteLine();
			for(int i = 1; i <= r; ++i) {
				for(int j = 1; j <= c; ++j)
					Console.Error.Write(a[i, j] + " ");
				Console.Error.WriteLine();
			}
			Console.Error.WriteLine();
		}
		
		void solve() {
			rez = "Yes\n";
			for(int i = 1; i <= r; ++i)
				for(int j = 1; j <= c - x + 1; ++j) {
					if(a[i, j] == b[i, j]) continue;
					long dif = b[i, j] - a[i, j];
					for(int k = 0; k < x; ++k)
						a[i, j + k] += dif;
				}
			//afis();
			for(int i = 1; i <= r - x + 1; ++i)
				for(int j = c - x + 2; j <= c; ++j) {
					if(a[i, j] == b[i, j]) continue;
					long dif = b[i, j] - a[i, j];
					for(int k = 0; k < x; ++k)
						a[i + k, j] += dif;
				}
			//afis();
			for(int i = r - x + 1; i <= r; ++i)
				for(int j = c - x + 1; j <= c; ++j)
					if(a[i, j] != b[i, j]) {
						rez = "No\n";
						return;
					}
		}
		
		public static void Main(string[] args) {
			CONSADD ob = new CONSADD();
			int t = int.Parse(Console.ReadLine());
			StringBuilder sb = new StringBuilder();
			while(t > 0) {
				--t;
				string[] vs = Console.ReadLine().Split();
				ob.r = int.Parse(vs[0]);
				ob.c = int.Parse(vs[1]);
				ob.x = int.Parse(vs[2]);
				ob.a = new long[ob.r + 1, ob.c + 1];
				ob.b = new long[ob.r + 1, ob.c + 1];
				for(int i = 1; i <= ob.r; ++i) {
					vs = Console.ReadLine().Split();
					for(int j = 0; j < ob.c; ++j)
						ob.a[i, j + 1] = long.Parse(vs[j]);
				}
				for(int i = 1; i <= ob.r; ++i) {
					vs = Console.ReadLine().Split();
					for(int j = 0; j < ob.c; ++j)
						ob.b[i, j + 1] = long.Parse(vs[j]);
				}
				ob.solve();
				sb.Append(ob.rez);
			}
			Console.Write(sb);
		}
	}
}

Consecutive Adding CodeChef Solution in GO

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)

	tc := readNum(reader)

	var buf bytes.Buffer

	for tc > 0 {
		tc--
		m, n, x := readThreeNums(reader)
		A := make([][]int, m)
		for i := 0; i < m; i++ {
			A[i] = readNNums(reader, n)
		}
		B := make([][]int, m)
		for i := 0; i < m; i++ {
			B[i] = readNNums(reader, n)
		}
		res := solve(x, A, B)

		if res {
			buf.WriteString("Yes\n")
		} else {
			buf.WriteString("No\n")
		}
	}
	fmt.Print(buf.String())
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(X int, A, B [][]int) bool {
	C := make([][]int, X)
	for i := 0; i < X; i++ {
		C[i] = make([]int, X)
	}
	for i := 0; i < len(A); i++ {
		for j := 0; j < len(A[i]); j++ {
			C[i%X][j%X] += A[i][j]
			C[i%X][j%X] -= B[i][j]
		}
	}

	for i := 1; i < X; i++ {
		delta := C[i][0] - C[0][0]
		for j := 1; j < X; j++ {
			if C[i][j]-C[0][j] != delta {
				return false
			}
		}
	}
	return true
}
Consecutive Adding CodeChef Solution Review:

In our experience, we suggest you solve this Consecutive Adding CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Consecutive Adding CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Consecutive Adding CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

Cognitive Class Answer

CodeChef Solution

Microsoft Learn

Leave a Reply

Your email address will not be published. Required fields are marked *