Question: 1. ( RESULT:Partially Accepted) import java.util.*; public class fix_k { public static void main(String args[]) { // creating Scanner object for taking input Scanner input
1. ( RESULT:Partially Accepted)
import java.util.*;
public class fix_k {
public static void main(String args[]) {
// creating Scanner object for taking input
Scanner input = new Scanner(System.in);
// taking number of test cases fromt the user
int T = input.nextInt();
// iterating through test cases
for(int i=0;i // taking size of the array in each test case int N = input.nextInt(); // assigning array of given size int array[] = new int[N]; // array to store sums for each fixed K value int sums[] = new int[N]; // taking input from user array values and storing in array for (int j = 0;j array[j] = input.nextInt(); } // fixing each element in array as K for(int k = 0;k < N;k++){ // variable to store sum int s=0; // iterating through array and finding sum for the fixed K value for (int j=0;j // if element is equal to K then add K to sum if(array[j] >= array[k]){ s = s+array[k]; } else{ // else add element into -1 that means subtract that number s = s-array[j]; } } // finally storing sum for each K in sums array sums[k]=s; } // finding max sum from the sums array int max = -100; for (int m =0;m if (sums[m]>max){ max = sums[m]; } } // printing sum System.out.println(max); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
