Hello Programmers In this post, you will know how to solve the Ada and crayons Codechef Solution. The Problem Code: ADACRA
One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for solutions.
Problem
Ada has an array of N crayons, some crayons are pointing upwards and some downwards. Ada thinks that an array of crayons is beautiful if all the crayons are pointing in the same direction.
In one step you can flip any segment of consecutive crayons. After flipping a segment, all crayons pointing downwards will point upwards and visceversa
What is the minimum number of steps to make the array of crayons beautiful?
Input
The first line of the input contains T the number of test cases. Each test case is described in one line containing a string S of N characters, the i-th character is ‘U’ if the i-th crayon is pointing upwards and ‘D’ if it is pointing downwards.
Output
For each test case, output a single line containing the minimum number of flips needed to make all crayons point to the same direction.
Constraints
- 1 ≤ T ≤ 3000
- 1 ≤ N ≤ 50
Explanation
Example case 1. In one step we can flip all the crayons pointing downwards
Ada and crayons CodeChef Solution in JAVA
import java.util.Scanner; import java.util.stream.IntStream; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for (int tc = 0; tc < T; tc++) { String S = sc.next(); System.out.println(solve(S)); } sc.close(); } static int solve(String S) { return ((int) IntStream.range(0, S.length() - 1).filter(i -> S.charAt(i) != S.charAt(i + 1)).count() + 1) / 2; } }
Disclaimer: The above Problem (Ada and crayons) is generated by CodeChef but the solution is provided by BrokenProgrammers. This tutorial is only for Educational and Learning purpose.
Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.