Empire Business CodeChef Solution

Problem -Empire Business 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.

Empire Business CodeChef Solution in C++17

#include <bits/stdc++.h>
#define int long long
using namespace std;

const long long MAXN = 2e6 + 2;
const long long MOD = 1e9 + 7;

int n;
int a[MAXN] = {0};

namespace solve {
	int pre[MAXN] = {0}, suf[MAXN] = {0};
	
	void fill_suffix() {
		int min_num = INT_MAX;
		for(int i = n; i >= 1; i--) {
			min_num = min(min_num, a[i]);
			suf[i] = min_num;
			min_num++;
		}
		return;
	}
	
	void fill_prefix() {
		int min_num = INT_MAX;
		for(int i = 1; i <= n; i++) {
			min_num = min(min_num, a[i]);
			pre[i] = min_num;
			min_num++;
		}
		return;
	}
	
	void programme() {
		// input:
		cin >> n;
		for(int i = 1; i <= n; i++) {
			cin >> a[i];
		}
		
		// solve:
		fill_suffix();
		fill_prefix();
		
		// check:
//		for(int i = 1; i <= n; i++) cout << pre[i] << " "; cout << endl; //
//		for(int i = 1; i <= n; i++) cout << suf[i] << " "; cout << endl; //
		
		// output:
		for(int i = 1; i <= n; i++) {
			cout << min(pre[i], suf[i]) << " ";
		}
		cout << "\n";
		return;
	}
}

signed main() {
//	freopen("INFLUNUM.INP", "r", stdin); //
//	freopen("INFLUNUM.OUT", "w", stdout); //
	
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	
	int test;
	cin >> test;
	
	while(test--) {
		solve::programme();
	}
	return 0;
}

Empire Business CodeChef Solution in C++14

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2e6+5;

int t, n, a[maxn], dp_tr[maxn], dp_ph[maxn];

signed main()
{
	cin>>t;
	while(t--)
	{
//		memset(a, 0, sizeof(a));
//		memset(dp_tr, 0, sizeof(dp_tr));
//		memset(dp_ph, 0, sizeof(dp_ph));
		cin>>n;
		for (int i=1; i<=n; i++) cin>>a[i];
		dp_tr[0]=1e9;
		dp_ph[n+1]=1e9;
		for (int j=1; j<=n; j++) dp_tr[j]=min(dp_tr[j-1]+1, a[j]);
		for (int i=n; i>=1; i--) dp_ph[i]=min(dp_ph[i+1]+1, a[i]);
		for (int i=1; i<=n; i++) cout<<min(dp_tr[i], dp_ph[i])<<" ";
		cout<<"\n";
	}
}

Empire Business CodeChef Solution in PYTH 3

# cook your dish here
# cook your dish here
t = int(input())

for i in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    r = [0] * n
    for j in range(n):
        r[j] = a[j]
    for k in range(n - 2, -1, -1):
        r[k] = min(r[k], r[k + 1] + 1)
    l = [0] * n
    for j in range(n):
        l[j] = a[j]
    for k in range(1, n):
        l[k] = min(l[k], l[k - 1] + 1)
    ans = []
    for x in range(n):
        ans.append(min(l[x], r[x]))
    print(*ans)

Empire Business CodeChef Solution in C

#include <stdio.h>

int main(void) {
	int t;
	scanf("%d",&t);
	while(t--)
	{
	    int n,lmin,rmin;
	    scanf("%d",&n);
	    int a[n];
	    int l[n];
	    int r[n];
	    for(int i=0;i<n;i++)
	    {
	        scanf("%d",&a[i]);
	    }
	    lmin=a[0];
	    rmin=a[n-1];
	    l[0]=a[0];
	    r[n-1]=a[n-1];
	    for(int i=1;i<n;i++)
	    {
	        lmin=lmin+1;
	        if(a[i]<lmin)
	        {
	            lmin=a[i];
	        }
	        l[i]=lmin;
	    }
	    for(int i=n-2;i>=0;i--)
	    {
	        rmin=rmin+1;
	        if(a[i]<rmin)
	        {
	            rmin=a[i];
	        }
	        r[i]=rmin;
	    }
	    for(int i=0;i<n;i++)
	    {
	        if(r[i]<l[i])
	        {
	            printf("%d ",r[i]);
	        }
	        else{
	            printf("%d ",l[i]);
	        }
	    }
	    printf("\n");
	}
	return 0;
}

Empire Business CodeChef Solution in JAVA

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

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;
import java.text.DecimalFormat;
	class Main
		{  
		static class Patient implements Comparable<Patient> {
	        int pos;
	        int illness;
	        Patient(int pos,int illness) {
	            this.pos=pos;
	            this.illness=illness;
	        }
	        @Override
	        public int compareTo(Patient p) {
	            return p.illness-this.illness;
	        }
	    }
		/*
		class Pair
		{
		  int f,s;
		  Pair( int f, int s)
		  {
		    this.f=f;
		    this.s=s;
		  }
		}*/
		 static class Pair {
		        char prefix;
		        int suffixId;
		        Pair(char prefix, int suffixId) {
		            this.prefix = prefix;
		            this.suffixId = suffixId;
		        }

		        @Override
		        public int hashCode() {
		            final int prime = 199;
		            int hash = 0;
		            hash = prime * prefix + hash;
		            hash = prime * suffixId + hash;
		            return hash;
		        }

		        @Override
		        public boolean equals(Object obj) {
		            if(obj instanceof Pair) {
		                Pair p2 = (Pair) obj;
		                return this.prefix == p2.prefix && this.suffixId == p2.suffixId;
		            }
		            return false;
		        }
		    }
		/*static class pair implements Comparable<pair>
		 {
		     long x;
		     int y;
		     pair(long x,int y)
		     {
		         this.x=x;
		         this.y=y;
		     }
		     public int compareTo(pair o)
		     {
		         return (int)(x-o.x);
		     }

		 }
	static	class Pair
		{
			long x,y;
			Pair(long x, long y)
			{
				this.x = x;
				this.y = y;
			}
		}
		static class PairComparator implements Comparator<Pair>
		{
			public long compare(Pair a, Pair b)
			{
				//if(a.x==b.x)
					return a.y-b.y;
			//	return a.x-b.x;
			}
		}
		static class Point {
			public double x, y;
		 
			private static final int MAX = (int) 1e6 + 1;
		 
			public Point(double x, double y) {
				this.x = x;
				this.y = y;
			}
		 
			public int hashCode() {
				return (int)x * MAX + (int)y;
			}
		 
			public boolean equals(Object ob) {
				if(ob instanceof Point) {
					Point other = (Point) ob;
					return other.x == x && other.y == y;
				}
				
				return false;
			}
		 
			public String toString() {
				return "(" + x + ", " + y + ")";
			}
		}
	/*	static int days4=0;
		static int all;
		static long phi[]=new long[1000001];
		static long ans[]=new long[1000001];
		//static int tree[],sum[];
		//public static long mod = 1000000007;	public static long [][] tempMatrix;
		public static long max = (long) Math.pow(10, 9) + 7;
		static StringBuilder res = new StringBuilder();
		//static Node tree[];
		//static int a[];
		static long mod = 998244353;
		 public static int rootColor=0;
		 static int MX = (1<<18);
		 static boolean primes[]=new boolean[10000001];
		 
		static double pi = 3.1415926535; 
		 private static final int MAXN = 5000;
		    private static final int MOD = 1000_000_007;
		 //   private static Modular modular_nCr = new Modular(MOD);
		  //  private static Modular modular = new Modular(MOD);
		    private static final long[][] nCr = new long[MAXN+5][MAXN+5];
		  //  static int[] maxval = new int[1000000];
		 //   static int[] minval = new int[1000000];
		  //  private static long bruteAns = 1;
		 //   static double eps = 1e-7;
		    static {
		    	nCr[0][0]=1;
				for(int i=0;i<=MAXN;i++)
					nCr[i][0]=1;
				for(int i=1;i<=MAXN;i++){
					for(int j=1;j<=i;j++){
						nCr[i][j]=(nCr[i-1][j-1]+nCr[i-1][j])%MOD;
					}
				}
			}
		/*
		    static { nCr[0][0] = 1;
		    for (int i = 1; i < MAXN; i++) {
		        nCr[i][0] = 1;
		        for (int j = 1; j <= i; j++) {
		            nCr[i][j] = modular_nCr.add(nCr[i - 1][j - 1], nCr[i - 1][j]);
		        } }
			}
			*/
		    static int final_sum=0;
		    static long mod=1000000007;
		    static ArrayList<Integer> graph[];
		    static boolean vis[];
		    static int seive[]=new int[1000001];
		    static int primes[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
		    static int[] calcPower() {
		        int[] arr = new int[26];
		        for (int i = 0; i < 26; i++) {
		            arr[i] = (int) Math.pow(2, i);
		        }
		        return arr;
		    }
			public static void main (String[] args) throws java.lang.Exception
			{  
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			PrintWriter pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		//	FastReader in = new FastReader(System.in);
			StringBuilder sb = new StringBuilder();
	        FastScanner in=new FastScanner();
		//	Scanner sc = new Scanner(System.in);
			PrintWriter out=new PrintWriter(System.out);
			
		//	HashMap<Integer, Integer> h = new HashMap<Integer, Integer>();
		//	TreeMap<Integer, Integer> h1 = new TreeMap<Integer, Integer>();
		//	HashMap<Integer, Integer> h2 = new HashMap<Integer, Integer>();
		//	HashSet<Point> s = new HashSet<Point>();
			
		//	HashSet<Double> s2 = new HashSet<Double>();
		//	HashSet<Double> s3 = new HashSet<Double>();
			//	HashSet<Character> h2 = new HashSet<Character>();
			//long t= in.nextLong();
			//	long t = in.nextLong();
			//DecimalFormat df = new DecimalFormat("#,###,##0.000000");
			
	/*	 boolean prime[]=new boolean[10000000];
			   int p[]=new int[10000000];
			    int k=1;
			    Arrays.fill(prime, true);
			    prime[0]=false;
			    prime[1]=false;for(int i=2;i<10000000;i++)
			    {
			        if(!prime[i])
			        {
			        	p[i]=p[i-1];
			            continue;
			        }
			        p[i]=k; k++;
	        for(long j=(long)i*i;j<10000000;j+=i)
            prime[(int)j]=false;
    }
		//	 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		    /*    int pri[]=new int[1000005];
		        int p=2;
		        List<Integer> list=new ArrayList<>();
		        while(p*p <=1000005)
		        {
		            if(pri[p]==0)
		            {
		                for(int i=p*2;i<=1000004;i=i+p)
		                {
		                    pri[i]=1;
		                }
		                list.add(p);
		            }
		            p++;
		        }*/
		        //System.out.println(list);
		
			int[] power = calcPower();
			   
		    int test = in.nextInt();
			while(test-->0)
			{  	
				int n = in.nextInt();
				int a[]=new int[n];
				for(int i=0;i<n;i++) {
					a[i] = in.nextInt();
				}
				int dp1[]=new int[n];
				dp1[0]=a[0];
				for(int i=1;i<n;i++) {
					dp1[i]=Math.min(a[i],dp1[i-1]+1);
				}
				int dp2[]=new int[n];
				dp2[n-1]=a[n-1];
				for(int i=n-2;i>-1;i--) {
					dp2[i]=Math.min(a[i],dp2[i+1]+1);
				}
				for(int i=0;i<n;i++) {
					sb.append(Math.min(dp1[i],dp2[i])+" ");
				}
				
				sb.append("\n");
			}
			out.println(sb);
			out.close();
			}
	
			
			
			
				/*
long mod = 1000000007;
//StringBuilder sb = new StringBuilder();
//	DecimalFormat df = new DecimalFormat("######0.00000000000");
//	int[] dp = new int[101];
String[] S1;

double[][] Comb=new double[1000+1][1000+1];
for(int i=0;i<1001;i++)
{
	Comb[i][0]=Comb[i][i]=1.0;
	for(int j=1;j<i;j++)
	Comb[i][j]=Comb[i-1][j]+Comb[i-1][j-1];
}
			*/
			public static long gcd(long a,long b,long n){
			    if(a==b){
			        return (power(a,n,mod)+power(b,n,mod))%mod;
			    }
			    long res=1l;
			    long num=a-b;
			    for(long i=1;i*i<=num;i++){
			        if(num%i==0){
			            long temp= (power(a,n,i)+ power(b,n,i))%i;
			            if(temp==0){
			                res=Math.max(res,i);
			            }
			            temp= (power(a,n,num/i) + power(b,n,num/i))%(num/i);
			             if(temp==0){
			                res=Math.max(res,num/i);
			            }
			        }
			    }
			    return res%mod;
			}
			public static long power(long a,long n,long d){
			    long res=1l;
			    while(n>0){
			        if(n%2==1){
			            res =((res%d)*(a%d))%d;
			            n--;
			        }else{
			            a=((a%d)*(a%d))%d;
			            n=n/2;
			        }
				   }
				   return res%mod;
			}
/*	static long power(long x,long y) { 
        long res=1;  
        x%=m;  
        while(y>0) { 
            if(y%2!=0) 
                res=(res*x)%m; 
            y=y>>1;
            x=(x*x)%m; 
        } 
        return res; 
    } 			
*/
	    
static long gcd(long a, long b)
{
if(b==0)
return a;
else
return gcd(b, a%b);
}
static long  nextPower_2 (  long x, long y )
{

long  count  =  0 ;
while ( y < x )
{count ++ ;
y  =  y <<  1 ;
}
return  count ;
}
/*
static long power(long a, long b, long p) 
{ 	long x = 1, y = a;
while (b > 0) {
if (b % 2 == 1) {
x = (x * y);
if (x >= p) x %= p;
}
y = (y * y); if (y >= p) y %= p;
b /= 2;}
return x;
}*/
public static class Modular {

private int MOD;

Modular(int MOD) {
   this.MOD = MOD;
}

public long add(long a, long b) {
   return (a + b) % MOD;
}

public long sub(long a, long b) {
   return (a - b + MOD) % MOD;
}

public long mul(long a, long b) {
   return (a * b) % MOD;
}

public long div(long a, long b) {
   return mul(a, inverseEuler(b));
}	public long power(long a, long b) {
long x = 1, y = a;
while (b > 0) {
    if (b % 2 == 1) {
        x = (x * y);if (x >= MOD) x %= MOD;
    }
    y = (y * y);
    if (y >= MOD) y %= MOD;
    b /= 2;}
return x;
}

public long inverseEuler(long n) {
return power(n, MOD - 2);
}
}
	static class FastReader {
byte[] buf = new byte[2048];
int index, total;
InputStream in;FastReader(InputStream is) {
in = is;
}	int scan() throws IOException {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0) { return -1;
}
}
return buf[index++];
}

String next() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan()) ;
StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) {
 sb.append((char) c);}
return sb.toString();
}String nextLine() throws IOException {
int c;for (c = scan(); c <= 32; c = scan()) ;
StringBuilder sb = new StringBuilder();
for (; c != 10 && c != 13; c = scan()) {
    sb.append((char) c);
} return sb.toString();
}char nextChar() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan()) ;
return (char) c;
}	int nextInt() throws IOException {
int c, val = 0;
for (c = scan(); c <= 32; c = scan()) ;
boolean neg = c == '-';
if (c == '-' || c == '+') {
    c = scan(); }
for (; c >= '0' && c <= '9'; c = scan()) {
    val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}long nextLong() throws IOException {
int c;long val = 0;
for (c = scan(); c <= 32; c = scan()) ;
boolean neg = c == '-';
if (c == '-' || c == '+') {
    c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
    val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
}

static class FastScanner{
	BufferedReader br;
	StringTokenizer st;
	public FastScanner(){br=new BufferedReader(new InputStreamReader(System.in));}
	String nextToken(){
		while(st==null||!st.hasMoreElements())
			try{st=new StringTokenizer(br.readLine());}catch(Exception e){}
		return st.nextToken();
	}
	int nextInt(){return Integer.parseInt(nextToken());}
	long nextLong(){return Long.parseLong(nextToken());}
	double nextDouble(){return Double.parseDouble(nextToken());}
}
}

Empire Business CodeChef Solution in PYPY 3

import sys
# import math as mt
# from collections import Counter, deque
# from itertools import permutations
# from functools import reduce
# from heapq import heapify, heappop, heappush, heapreplace

def getInput(): return sys.stdin.readline().strip()
def getInt(): return int(getInput())
def getInts(): return map(int, getInput().split())
def getArray(): return list(getInts())

# sys.setrecursionlimit(10**8)
# INF = float('inf')
# MOD1, MOD2 = 10**9+7, 998244353

tc = getInt()

for _ in range(tc):
    
    n = getInt()
    arr = getArray()
    
    dp_fwd = [0]*n
    dp_fwd[-1] = arr[-1]
    
    dp_back = [0]*n
    dp_back[0] = arr[0]
    
    for i in range(n-2, -1, -1):
        dp_fwd[i] = min(arr[i], dp_fwd[i+1] + 1)
        
    for i in range(1, n):
        dp_back[i] = min(arr[i], dp_back[i-1] + 1)
    
    ans = []
    for i in range(n):
        ans.append(min(dp_fwd[i], dp_back[i]))
        
    print(*ans)

Empire Business CodeChef Solution in PYTH


def intputs(s=''):
	'''To get data in the form of space separated integers and return a tuple containing them'''
	return map(int,raw_input().split() )

T = input()

for Q in xrange(T):
	N = input()
	A = intputs()
	r = range(N-1)
	r.reverse()
	for i in r:
		A[i] = min(A[i],A[i+1]+1)
	print A[0],
	for i in range(1,N):
		A[i] = min(A[i],A[i-1]+1)
		print A[i],
	print

Empire Business CodeChef Solution in GO

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	tc := readNum(reader)
	var buf bytes.Buffer
	for tc > 0 {
		tc--
		n := readNum(reader)
		A := readNNums(reader, n)
		res := solve(n, A)
		for i := 0; i < n; i++ {
			buf.WriteString(fmt.Sprintf("%d ", res[i]))
		}
		buf.WriteByte('\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(n int, A []int) []int {
	res := make([]int, n)
	var cur = math.MaxInt32 >> 1
	for i := 0; i < n; i++ {
		res[i] = min(cur+i, A[i])
		cur = min(cur, A[i]-i)
	}
	cur = math.MaxInt32
	for i := n - 1; i >= 0; i-- {
		res[i] = min(res[i], cur-i)
		cur = min(cur, i+A[i])
	}

	return res
}

func min(a, b int) int {
	if a <= b {
		return a
	}
	return b
}
Empire Business CodeChef Solution Review:

In our experience, we suggest you solve this Empire Business 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 Empire Business CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Empire Business 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 *