KQM24A editorial

PROBLEM LINK:

Practice
Contest

Author, Tester and Editorialist: Chandan Boruah

DIFFICULTY:

Easy

PREREQUISITES:

Brute Force

PROBLEM:

Given a number and a string, the value of the string is the sum of positions of the characters in the string, in the English alphabet system. Print, the minimum number of characters to change in the string, such that the value of the string becomes equal to the given number.

QUICK EXPLANATION:

Change the values of characters in the string from largest valued character to lowest valued character and vice versa depending on whether the value of the string is smaller or larger than the number. Check Detailed Explanation, below.

DETAILED EXPLANATION:

Print 0 if the number and the value of the string are equal. If the value of the string is greater than the number then, decrease the largest valued character in the string followed by 2nd largest and continue it till it becomes equal to the number. If the value of the string is smaller than the number, then change the smallest valued character in the string and increase its value to at most 26, and continue with the next larger valued character and continue, till the value of the string is equal to the number.

SOLUTIONS:

Setter's Solution
using System;
using System.Collections.Generic;
class some
{
	public static void Main()
	{
		int n=int.Parse(Console.ReadLine());
		string s=Console.ReadLine();
		char[]cc=s.ToCharArray();
		Array.Sort(cc);
		int val=0;
		foreach(char c in s)
		{
			val+=(int)(c-'a')+1;
		}
		if(val==n)Console.WriteLine(0);
		else if(val>n)
		{
			
			int sum=val;
			int count=0;
			for(int i=s.Length-1;i>=0;i--)
			{
				int k=(int)(s[i]-'a');
				count++;
				sum-=k;
				
				if(sum<=n)
				{
					
					break;
				}
			}
			Console.WriteLine(count);
		}
		else
		{
			int sum=val;
			int count=0;
			for(int i=0;i<s.Length;i++)
			{
				int k=26-(int)(s[i]-'a');
				sum+=k;count++;
				if(sum>=n)
				{
					break;
				}
			}
			Console.WriteLine(count);
		}
	}
}