Yet Another Palindrome Problem CodeChef Solution

Problem -Yet Another Palindrome Problem 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.

Yet Another Palindrome Problem CodeChef Solution in C++17

#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main() {
	//ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
    int t; cin >> t;
    while (t--) {
        int n; cin >> n;
        ll a[n];
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        bool flag = false;
        ll ans[n];
        for (int i = 0; i < n/2; i++) {
            ans[i] = (a[n-i-1] - a[i]);           
            if (ans[i] < 0) {
                flag = true;
                break;
            }
        }
       // bool pa = false;
        for (int i = 0; i < n/2 - 1; i++) {
            //cout << ans[i] << ans[i+1] << endl;
            if (ans[i] < ans[i + 1]) {
                flag = true;
                break;
            }
        }
        if (flag) {
            cout << -1 << endl;
        } else cout << a[n-1] - a[0] << endl;
    }
}

Yet Another Palindrome Problem CodeChef Solution in C++14

#include <iostream>
using namespace std;
#define lli long long int

int main() {
	// your code goes here
	int t;
	cin >> t;
	while(t--){
	    int n;
	    cin >> n;
	    lli arr[n];
	    for(int i=0;i<n;i++){
	        cin >> arr[i];
	    }
	    if(n==1) cout << 0 << endl;
	    else{
	        int i,j;
	        i = n/2-1;
	        if(n%2==0){
	            j=n/2;
	        }
	        else{
	            j = n/2+1;
	        }
	        lli ans=0,curr_val;
	        bool flag=true;
	        while(i>=0){
	            curr_val=arr[i]+ans;
	            if(curr_val>arr[j]){
	                flag = false;
	                break;
	            }
	            
	            else {
	               ans += (arr[j]-curr_val); 
	            }
	            i--;j++;
	        }
	        cout << (flag?ans:-1) << endl;
	        
	    }
	    
	}
	return 0;
}

Yet Another Palindrome Problem CodeChef Solution in PYTH 3

for i in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    left=[]
    k=True
    for i in range(len(a)//2):
        if(a[i]<=a[-i-1]):
            left.append(a[-i-1]-a[i])
        else:
            k=False
            print("-1")
            break
    if(k):
        m=[]
        m.extend(left)
        m.reverse()
        left.sort()
        if(m==left):
            print(m[-1])
        else:
            print("-1")
        
        
            
    

Yet Another Palindrome Problem CodeChef Solution in C

#include <stdio.h>

int main(void) {
	int T,N;
	scanf("%d",&T);
	while(T--)
	{
	    scanf("%d",&N);
	    int A[N],D[N/2],i,j=0,k=0;
	    for(i=0;i<N;i++)
	    scanf("%d",&A[i]);
	        for(i=0;i<=N/2;i++)
	        {
	            D[i]=A[N-i-1]-A[i];
	        }
	        for(i=0;i<N/2;i++)
	        {
	            if(D[i+1]>D[i]) j++;
	            if (D[i]<0) k++;
	        }
	        if(j==0&&k==0) printf("%d\n",D[0]);
	        else printf("-1\n");
	    }
}

Yet Another Palindrome Problem CodeChef Solution in JAVA


// package Practice;
import java.io.*;
import java.util.*;

class Codechef {
    private static long[] fact = new long[(int) 1e6 + 1];

    public static void main(String[] args) throws Exception {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine());
        while (t-- > 0) {
            int N = Integer.parseInt(br.readLine().trim());
            String[] values = br.readLine().split(" ");
            // String[] valuesA = br.readLine().split(" ");
            // String str = br.readLine();
            // String str2 = br.readLine();
            // String[] arrValues = br.readLine().split(" ");
            // int x = Integer.parseInt(values[0]);
            // int y = Integer.parseInt(values[1]);
            // int z = Integer.parseInt(values[2]);
            // int w = Integer.parseInt(values[3]);
            // int[] arr = new int[values.length];
            // int [] freq = new int[11];
            // int maxValue = 0, value = 0;
            // for (int i = 0; i < values.length; i++) {
            //     arr[i] = Integer.parseInt(values[i]);
            //     freq[arr[i]]++;
            // }
            // long x = Long.parseLong(values[0]);
            // long y = Long.parseLong(values[1]);
            System.out.println(solve(values));
        }
    }
    // https://www.youtube.com/watch?v=MOStCA9W0PM

    private static int solve(String [] values) {
        int maxValue = (int)1e9, n = values.length, traceValue = (int)1e9;

        for(int i = 0; i < n/2; i++){
            int a = Integer.parseInt(values[i]);
            int b = Integer.parseInt(values[n-1-i]);
            if(a > b) return -1;
            else {
                int temp = b - a;
                if(temp > traceValue) return -1;
                else if(i == 0){
                    maxValue = temp;
                    traceValue = temp;
                } else{
                    traceValue = temp;
                }
            }
        }
        return maxValue;
    }

    private static void display(long[] sumB) {
        for (Long val : sumB) {
            System.out.print(val + " ");
        }
        System.out.println();
    }

    private static boolean isPrime(int p) {

        for (int i = 2; i * i <= p; i++) {
            if (p % i == 0)
                return false;
        }
        return true;
    }

    private static int lcm(int a, int b, int GCD) {
        // gcd * lcm = a * b;
        // lcm = a * b / gcd;
        return a * b / GCD;
    }

    private static int gcd(int a, int b) {

        int x = Math.max(a, b), y = Math.min(a, b);
        while (x % y != 0) {
            int rem = x % y;
            x = y;
            y = rem;
        }
        return y;
    }

    private static boolean isPerfectSquare(int i) {

        double ans = Math.sqrt(i);
        return ans == (int) ans;
    }

    private static void display(int[] arr) {
        for (int ele : arr) {
            System.out.print(ele + " ");
        }
        System.out.println();
    }

    private static void display(int[][] arr) {
        for (int[] d : arr) {
            for (int ele : d)
                System.out.print(ele + " ");
            System.out.println();
        }
    }
}

Yet Another Palindrome Problem CodeChef Solution in PYPY 3

for _ in range(int(input())):
    n = int(input())
    ls = list(map(int, input().split()))
    i, j = 0, n - 1
    ans = []
    while i < j:
        ans.append(ls[j] - ls[i])
        i += 1
        j -= 1
    flag = True
    for i in range(1, len(ans)):
        if ans[i] > ans[i - 1]:
            flag = False
            break
    if not flag:
        print(-1)
    else:
        if min(ans) < 0:
            print(-1)
        else:
            print(max(ans))
        

Yet Another Palindrome Problem CodeChef Solution in PYTH

t = int(raw_input())
for i in range(t):
	N = int(raw_input())
	st = raw_input().split()
	A = []
	for x in st:
		A.append(int(x))
	# endfor x
	r = 0
	p = N/2 -1
	q = p+1+N%2
	while (r > -1) and (p >= 0):
		if A[p]+r > A[q]:
			r = -1
		else:
			r = A[q] - A[p]
		# endif
		p -= 1
		q += 1
	# endwhile
	print r
# endfor i

Yet Another Palindrome Problem CodeChef Solution in C#

using System;

public class Test
{
	public static void Main()
	{
		var t = int.Parse(Console.ReadLine());
		var n = 0;
		string[] input = null;
		long[] diff = null;
	
	    while (t-- > 0) {
	        n = int.Parse(Console.ReadLine());
	        if (n == 1) {
	            Console.WriteLine(0);
	            continue;
	        }
	        input = Console.ReadLine().Split(' ');
	        diff = new long[n / 2];
	        for(var i = 0; i < n / 2; i++) {
	            diff[i] = long.Parse(input[n - 1 - i]) - long.Parse(input[i]);
	            
	            if (diff[i] < 0 || i > 0 && diff[i] > diff[i - 1]) {
	                Console.WriteLine(-1);
	                break;
	            }
	            else if (i == n / 2 - 1) {
	                Console.WriteLine(diff[0]);
	            }
	        }
	    }
	}
}

Yet Another Palindrome Problem CodeChef Solution in NODEJS

process.stdin.resume();
process.stdin.setEncoding('utf8');

// your code goes here
let inputString = "";
let currentline  = 0;

process.stdin.on("data", (inputStdin)=>{
    inputString += inputStdin;
});

process.stdin.on("end", (inputStdin)=>{
    inputString = inputString.trim()
                               .split("\n")
                               .map((string)=> string.trim());
    main();                               
});

function readline(){
    return inputString[currentline++];
}

function main(){
    let t = parseInt(readline(), 10);
    
    while(t-- > 0){
        
        let n = parseInt(readline(), 10);
        let arr = readline().split(" ")
                            .map((x)=> parseInt(x, 10));
        // let arr = readline();                    
        
        let ans = find(arr,n);                    
        console.log(ans);
        
    }
    
}   

function find(arr, n){

     let dd = [], max = 0;
     
     for(let i =0 ; i < n/2; i++){
         dd[i] = arr[n-1-i] - arr[i];
         if(dd[i] < 0) return -1;
         max = Math.max(max, dd[i]);
     }
     
     for(let i =0 ; i < dd.length-1; i++){
         if(dd[i] < dd[i+1]) return -1;
     }
     
     return max;
     
}

Yet Another Palindrome Problem CodeChef Solution in GO

package main

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

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

	var buf bytes.Buffer

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n := readNum(reader)
		A := readNNums(reader, n)
		res := solve(A)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}

	fmt.Print(buf.String())
}

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 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 readString(reader *bufio.Reader) string {
	s, _ := reader.ReadString('\n')
	for i := 0; i < len(s); i++ {
		if s[i] == '\n' || s[i] == '\r' {
			return s[:i]
		}
	}
	return s
}

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 solve(A []int) int {
	n := len(A)

	var p, q int

	if n&1 == 1 {
		p = n/2 - 1
		q = p + 2
	} else {
		p = n/2 - 1
		q = p + 1
	}

	var add int

	for p >= 0 {
		cur := A[p] + add
		if cur > A[q] {
			return -1
		}
		add += A[q] - cur
		p--
		q++
	}

	return add
}
Yet Another Palindrome Problem CodeChef Solution Review:

In our experience, we suggest you solve this Yet Another Palindrome Problem 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 Yet Another Palindrome Problem CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Yet Another Palindrome Problem 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 *