|
| 1 | +/** Chef is given an array A consisting of N positive integers. Chef shuffles the array A and creates a new array B of length N, where Bi=(Ai+i)mod2, for each i(1≤i≤N). |
| 2 | +
|
| 3 | +Find the maximum possible sum of integers of the array B, if Chef shuffles the array A optimally. |
| 4 | +
|
| 5 | +Input Format |
| 6 | +The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. |
| 7 | +Each test case contains two lines of input. |
| 8 | +The first line of each test case contains an integer N. |
| 9 | +The second line of each test case contains N space-separated integers A1,A2,…,AN. |
| 10 | +Output Format |
| 11 | +For each test case, print a single line containing one integer - the maximum sum of integers of the array B. |
| 12 | +**/ |
| 13 | +import java.util.*; |
| 14 | +public class Shuffling_Parties |
| 15 | +{ |
| 16 | + public static void main (String[] args) //main function |
| 17 | + { |
| 18 | + Scanner in = new Scanner (System.in); |
| 19 | + int t = in.nextInt(); // taking variable t for no. of test cases |
| 20 | + while(t>0) |
| 21 | + { |
| 22 | + int N = in.nextInt(); //Number of Positive Integers |
| 23 | + int even =0; //Counter for even numbers |
| 24 | + int odd =0; //Counter for odd numbers |
| 25 | + int sum=0; //Variable to store sum |
| 26 | + for(int i =0; i<N; i++) // Loop to get input from user and check whether the input is odd or even. |
| 27 | + { |
| 28 | + int inp =in.nextInt(); |
| 29 | + if(inp % 2==0) // Input is even |
| 30 | + { |
| 31 | + even++; |
| 32 | + } |
| 33 | + else //Input is odd |
| 34 | + { |
| 35 | + odd++; |
| 36 | + } |
| 37 | + |
| 38 | + } |
| 39 | + int eind =N/2, oind = N/2+N%2; //Calculating no. of all the even and odd indices possible for n numbers. |
| 40 | + //we have to find the maximum possible sum of integer for array B. So maximum value which we can have is 1 at a particular index to get that we should have (Ai+i) = odd. |
| 41 | + int x = Math.min(oind,even); //Calculating No.of 1s possible by even numbers |
| 42 | + int y = Math.min(eind,odd); //Calculating No.of 1s possible by odd numbers |
| 43 | + sum = x+y; //Calculating sum |
| 44 | + System.out.println(sum); //Printing the sum |
| 45 | + t--; //decrementing the value after running a testcase. |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | +} |
0 commit comments