SKYSCR - Editorial (LOCJUN16)

Problem Link

Contest
Practice

Author: Vaibhav Tulsyan
Editorialist: Arjit Srivastava

Difficulty

Cakewalk

Pre-requisites

Basic Maths

Problem

Given two numbers A and B, find the minimum number of moves to make them equal - where every move takes one unit.

Quick Explanation

The answer would always be the absolute difference between their values.

Detailed Explanation

There are two ways by which we can ensure that the two given numbers end up being equal.
Reduce the number which is greater to the small number.
Increase the number which is smaller to the greater number.

Since, the cost of increasing a number and decreasing a number is the same, we can decipher that irrespective of the choice we make, we’ll have to use | A - B | units to make the two numbers equal.

Pseudo-code

read a,b
print abs(a-b)

Sub-task wise solutions

20 points:
Using int data type in C/C++ would give 20 points.

100 points:
Notice that the constraint is 1018, so long long int data type will be needed to hold values as large as the constraints.

Languages like Python can use the their integer data type for this range still.

Editorialist’s solution

package contest;

import java.io.;
import java.util.
;

public class come {

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

	BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	int t=Integer.parseInt(br.readLine());
	int s=t;
	int i=0;
    long N=0,M=0;
	long k=0;
	long r[]=new long[100000];
	while(t>0)
	{
		k=0;
		String lines=br.readLine();
		String arr[]=lines.split(" ");
		N=Long.valueOf(arr[0]);
		M=Long.valueOf(arr[1]);
		if(N>M)
		{
			k=N-M;
			r[i]=k;
		}
		else if(M>N)
		{
			k=M-N;
			r[i]=k;
		}
		else
		{
			k=0;
		}
		t--;
		i++;
	}
	for(int j=0;j<s;j++)
	{
		System.out.println(r[j]);
	}
		}
		
	}
	// TODO Auto-generated method stub

why it is showing wrong answer?

import java.util.;
import java.io.
;

class height {

public static void main(String[] args)throws IOException
{
	
	BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	int t=Integer.parseInt(br.readLine());
	int s=t;
	int i=0;
    long N=0,M=0;
	long k=0;
	long r[]=new long[100000];
	Scanner sc=new Scanner(System.in);
	while(t>0)
	{
		
		k=0;
		
		String lines=br.readLine();
		String arr[]=lines.split(" ");
		N=Long.valueOf(arr[0]);
		M=Long.valueOf(arr[1]);
	
		int mid=(int)(N+M)/2;
			while(N!=mid && M!=mid)
			{
				if(N>M)
				{
				N--;
				k++;
				M++;
				k++;
				r[i]=k;
			}
				else if(M>N)
				{
					M--;
					k++;
					N++;
					k++;
					r[i]=k;
			}
			}
			while(N==mid && M!=mid)
			{
                  
				if(mid>M)
				{
				 M++;
				 k++;
				 r[i]=k;
			 }
			 else
			 {
				 M--;
				 k++;
				 r[i]=k;
			 }
			 }
			while(M==mid && N!=mid)
			{
				if(mid>N)
				{
				 N++;
				 k++;
				 r[i]=k;
				}
			 else
			 {
				 N--;
				 k++;
				 r[i]=k;
			 }
			 }
			
		t--;
		i++;
	
		}
	for(int j=0;j<s;j++)
	{
		System.out.println(r[j]);
	}
	}
	}
	// TODO Auto-generated method stub

why it is showing wrong answer??

ok its working now

@wittyceaser @admin2 Please provide editorials or some help for the problem ‘Maximum Disjoint Subarrays (MXSUBARR)’.

1 Like