QM21B Editorial

PROBLEM LINK:

Practice
Contest

Author: Chandan Boruah
Tester: Harsh Raj
Editorialist: Chandan Boruah

DIFFICULTY:

EASY

PREREQUISITES:

Maths

PROBLEM:

Given an array, and a number is added to the odd positions and subtracted from the even positions(1 based index). Print the maximum number, that is to be added, possible such that the resulting array is in ascending order.

QUICK EXPLANATION:

Find the difference between odd and even positions. Iterate between the minimum difference and find the maximum difference possible.

EXPLANATION:

Find the difference between odd and even positions. Iterate between the minimum difference and find the maximum difference possible. Since, minimum difference will give the possibility and maximum inside that minimum will give the maximum of that minimum difference possible.

SOLUTIONS:

chandubaba's Solution
using System;
using System.Collections.Generic;
class some
{
	public static void Main()
	{
		string[]ss=Console.ReadLine().Split();
		int n=int.Parse(ss[0]);
		
		ss=Console.ReadLine().Split();
		int[]arr=new int[n];
		for(int i=0;i<n;i++)
		{
			arr[i]=int.Parse(ss[i]);
		}
		int max=int.MaxValue;
		for(int i=0;i<n-1;i+=2)
		{
			max=Math.Min(max,arr[i+1]-arr[i]);
			if(arr[i+1]-arr[i]<=0)max=-1;
		}
		if(max==-1)Console.WriteLine(-1);
		else
		{
			int a=1;
			int b=max+a;
			int ii=0;
			for(ii=b;ii>=1;ii--)
			{
				if((b-ii)>(a+ii))break;
			}
			Console.WriteLine(ii);
		}
	}
}
Harshraj22's Solution
from math import inf
import sys

n = int(input())
arr = list(map(int,input().split()))
diff = inf

odd = [arr[x] for x in range(n) if x%2==0]
even = [arr[x] for x in range(n) if x%2==1]

def is_unique(arr):
	n = len(arr)
	m = len(set(arr))
	return n==m

if odd != sorted(odd) or even != sorted(even):
	print(-1)
else:
	# try all possible values
	for i in range(101,-1,-1):
	    newArr = [x+i if index%2==0 else x-i for index,x in enumerate(arr)]
	    if newArr == sorted(newArr) and is_unique(newArr):
	        print(i)
	        # print(f'arr is {arr} and newArr {newArr}')
	        sys.exit(0)

print(-1)

https://www.codechef.com/viewsolution/29463167
Can you please tell that for which test case is my code giving incorrect ans ?

1 Like

hi! the last one… please wait, will tell you the test case…

https://www.codechef.com/viewsolution/29524270
Just needed one change, marked it in the code.

1 Like

oh thank you, sorry I was highly worried about the business. I have been working on the next member contest to be on Thursday, and making terms and conditions for the recruiting services. its both fun and stressful(cause being married making money in business is hard, as for me now), hope it gets in the flow with time :slight_smile:… it will it will.

Thanks alot bro

1 Like

Good job!