Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

. Please write a code in java8 only Digit sum You are given a string of length 2N consisting of only digits from 0 to

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

.

image text in transcribed

Please write a code in java8 only

Digit sum You are given a string of length 2N consisting of only digits from 0 to 9. You can make a move to choose an arbitrary position and replace the digit in that position with any digit from 0 to 9. Task Determine the minimum number moves required to make the sum of the first N digits equal to the sum of the N digits that follow. Note: 1-based indexing is used. Example Example Assumptions N=3 S = 111279 Approach Change S[1 from 1 to 9 Change S[5] from 7 to O Now, S = 911209 Therefore, the sum of S[i] to S[3) = 9 + 1 + 1 = 11 and the sum of S[4] to S[6] = 2 + 0 + 9 = 11 TETETIC, UTE LO GLOR. MAN 0 A clube 70 og +0+ 9 = 11 Hence, the minimum number of moves required is 2. Note: There can be other possible ways to achieve the answer, but we can not get the required result sum in less than 2 changes. Function description Complete the function solve provided in the editor. This function takes the following 2 parameters and returns the answer: N: Represents an integer denoting N S: Represents a string denoting S Input format Note: This is the input format you must use to provide custom input (available above the Compile and Test button). 1 The first line contains T denoting the number of test cases. I also specifies the number of times you have to run the solve function on a 2 different set of inputs. For each test case: 3 o The first line contains an integer N. W o The next line contains a string S of length 2N. Output format For each test case, print the answer in a new line. Constraints Output format 1 For each test case, print the answer in a new line. Constraints 1 Sample output 1 1 2 1325 Explanation The first line contains the number of test cases, T=1 The first test case Given 2 N=2 S=1325 Approach Change S[1 from 1 to 4. Now, S = 4325 Thus the sum of S[l] to S[2] = 4 + 3 = 7 and the sum of S[4] to S[6] = 2 + 5 = 7 Hence, the minimum number of moves required is 1 Note: There can be other possible ways to achieve the answer but we cannot get the required result in less than 1 change. 7 8 5 - public class Testclass { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Printwriter wr = new Printwriter(System.out); 9 int T = Integer.parseInt(br.readLine().trim()); 0 for(int t i = 0; t_i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students also viewed these Databases questions

Question

Evaluate employees readiness for training. page 275

Answered: 1 week ago