Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

A Small Array CodeChef Solution

Problem -A Small Array 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.

A Small Array CodeChef Solution in C++14

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef unsigned long long lll;
typedef pair<ll, ll> Pii;
#define nl "\n"
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define YES cout << "YES\n";
#define NO cout << "NO\n";
#define Yes puts("Yes");
#define No puts("No");
#define ansr cout << ans << nl;
#define pii pair<ll, ll>
#define pdi pair<double, ll>
#define pid pair<ll, double>
#define vpii vector<pair<ll, ll>>
#define vi vector<ll>
#define si set<ll>
#define mii map<ll, ll>
#define umii unordered_map<ll, ll>
#define fi first
#define se second
#define pb push_back
#define rp(i, a, n) for (ll i = a; i < n; i++)
#define rpn(i, n, a) for (ll i = n - 1; i >= a; i--)
#define inp(a, n) rp(i, 0, n) cin >> a[i]
#define MAX 100005
#define withMOD % 1000000007
const ll INF = 2e9;
const ll N = 2e5 + 1;

void solve()
{
  ll n,x;
  cin>>n>>x;
  vi a(n);inp(a,n);
  ll sum = accumulate(all(a),0ll);
  ll x1 = 0,sum1 = 0;
  ll x2 = 0,sum2 = 0;
  rp(i,0,n){
    sum1 = max(a[i],a[i]+sum1);
    x1 = max(x1,sum1);
    sum2 = min(a[i],a[i]+sum2);
    x2 = min(x2,sum2);
  }
  float ans = 0.00;
  if(x>0){
    // sum -= x1;
    ans += sum - x1;
    ans += float(x1)/x;
    // sum += x1;
  }
  else if(x<0){
    // sum -= x2;
    ans += sum - x1;
    ans += float(x1)/x;
    // x2 /= x;
    // sum += x2;
  }
  cout<<setprecision(5)<<ans<<nl;
}

int main()
{
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#else
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#endif
  int t;
  // cin >> t;
  t = 1;
  while (t--)
    solve();
  return 0;
}

A Small Array CodeChef Solution in PYTH 3

# cook your dish here
n, x = map(int, input().split())
a = list(map(int, input().split()))
maxsum = 0
allsum = 0
currsum = 0
for i in a:
    currsum += i
    allsum += i
    maxsum = max(maxsum, currsum)
    
    if currsum < 0:
        currsum = 0
        
print(allsum-(maxsum-maxsum/x))

A Small Array CodeChef Solution in C

#include<stdio.h>
int main()
{
    int x,n,sum = 0,min = 0,dif = 0;
    scanf("%d %d", &n,&x);
    int c;
    for(int i=0; i<n; i++)
    {
        scanf("%d", &c);
        sum = sum + c;
        if(sum - min > dif){
            dif = sum - min;
        }
        if(sum < min){
            min = sum;
        }
    }
    float ans = sum - (x-1)*(1.0)*(dif)/x;
    printf("%.1f",ans);
    return 0;
    
}

A Small Array 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 double getMax(int a[]){
        
        int n = a.length;
        
        double max = Double.MIN_VALUE;
        
        double sum = 0;
        
        for(int i=0;i<n;i++){
            sum+=a[i];
            if(sum>=0){
                
                max = Math.max(sum,max);
                
            }else{
                sum = 0;
            }
        }
        
        return max;
        
    }
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		int x = sc.nextInt();
		double sum = 0;
		int a[] = new int[n];
		for(int i=0;i<n;i++){
		    a[i] = sc.nextInt();
		    sum+=a[i];
		}
		double ans =(double) getMax(a);
		if(ans>0){
		    
		    sum = (sum-ans) + (ans/x);
		    
		}
		System.out.println(sum);
	}
}

A Small Array CodeChef Solution in PYPY 3

# You don't need to add/edit anything to the below solution. 
# Click on the SUBMIT button to solve your first problem on CodeChef.
from math import isclose
n, x = map(int, input().split())
arr = list(map(int, input().split()))

p = [arr[0]]
m, idx = arr[0], 0
for i in range(1, n):
    p.append(max(p[i-1]+arr[i], 0))
    if p[i] > m:
        m = p[i]
        idx = i

if m == 0:
    print(sum(arr))
else:
    s = 0
    while s != m:
        s += arr[idx]
        arr[idx] /= x
        idx -= 1
    
    
    print(sum(arr))

A Small Array CodeChef Solution in PYTH

st = raw_input().split()
N = int(st[0]) 
X = int(st[1]) 
st = raw_input().split()
if X == 1:
	tot = 0
	for x in st:
		tot += int(x)
	# endfor x
	print tot
else:
	tot = 0
	W = 0
	MX = 0
	for x in st:
		n = int(x)
		tot += n
		if n > 0:
			W += n
			if W > MX:
				MX = W
			# endif
		else:
			W = max(0,W+n)
		# endif
	# endfor x
	tot -= MX
	tot += 1.0*MX/X
	print tot
# endif

A Small Array CodeChef Solution in GO

package main

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

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] != ' ' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(scanner *bufio.Scanner) (a int) {
	scanner.Scan()
	readInt(scanner.Bytes(), 0, &a)
	return
}

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

func readNNums(scanner *bufio.Scanner, n int) []int {
	res := make([]int, n)
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
	return res
}

func fillNNums(scanner *bufio.Scanner, n int, res []int) {
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
}

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

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

	return i
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n, x := readTwoNums(scanner)
	A := readNNums(scanner, n)
	res := solve(n, x, A)
	fmt.Printf("%.3f\n", res)
}

func solve(n, x int, A []int) float64 {
	var sum int

	for i := 0; i < n; i++ {
		sum += A[i]
	}

	if x == 1 {
		return float64(sum)
	}

	var best int
	var tmp int
	for i := 0; i < n; i++ {
		tmp += A[i]
		best = max(best, tmp)
		if tmp < 0 {
			tmp = 0
		}
	}

	if best == 0 {
		return float64(sum)
	}

	fx := float64(x)
	num := float64(best) * (fx - 1) / fx

	return float64(sum) - num
}

func max(a, b int) int {
	if a >= b {
		return a
	}
	return b
}
A Small Array CodeChef Solution Review:

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

Find on CodeChef

Conclusion:

I hope this A Small Array 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 *