Sie sind auf Seite 1von 4

To input a testcase and array

{
import java.io.*;
import java.lang.*;
import java.util.*;
class Array
{
public static void main(String args[])throws IOException
{
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());

while(t-- > 0)
{
long n = Long.parseLong(read.readLine());
String st[] = read.readLine().trim().split("\\s+");

int arr[] = new int[(int)n];

for(long i = 0; i < n; i++)


arr[(int)i] = Integer.parseInt(st[(int)i]);

System.out.println(new MinimumNumber().minNumber(arr,0,n-1));

}
}

HAving the
class MinimumNumber{

static long minNumber(int arr[], long low, long high)


{

// Your code here

BUFFER READER
2
5
1 2 3 -2 5
4
-1 -2 -3 -4

Output:
9
-1

import java.io.*;
class Array {

public static void main (String[] args) throws IOException {


BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim()); //Inputting the
testcases
while(t-->0){
int n = Integer.parseInt(br.readLine().trim());
int arr[] = new int[n];
String inputLine[] = br.readLine().trim().split(" ");
for(int i=0; i<n; i++){
arr[i] = Integer.parseInt(inputLine[i]);
}

Kadane obj = new Kadane(); //object creation of


2nd class

System.out.println(obj.maxSubarraySum(arr, n)); //object calling of


2nd class
}
}
}

}
/*This is a function problem.You only need to complete the function given below*/
class Kadane{

// Function to find subarray with maximum sum


// arr: input array
// n: size of array
int maxSubarraySum(int arr[], int n){

// Your code here

PYTHON FOR THE SAME


{
#Initial Template for Python 3
import math

def main():
T=int(input())
while(T>0):

n=int(input())

arr=[int(x) for x in input().strip().split()]

print(maxSubArraySum(arr,n))

T-=1
if __name__ == "__main__":
main()
}
''' This is a function problem.You only need to complete the function given below
'''
#User function Template for python3
##Complete this function
def maxSubArraySum(a,size):
##Your code here

Max Circular Subarray Sum


Max Circular Subarray Sum Submissions: 894 Accuracy: 19.11% Difficulty: Hard
Marks: 8
Associated Course(s): Geeks Classes in Noida DSA- Online More

Problems
Given an array arr[] of N integers arranged in a circular fashion. Your task is to
find the maximum contigious subarray sum.

Input:
First line of input contains a single integer T which denotes the number of test
cases. First line of each test case contains a single integer N which denotes the
total number of elements. Second line of each test case contains N space separated
integers denoting the elements of the array.

Output:
For each test case print the maximum sum obtained by adding the consecutive
elements.

User Task:
The task is to complete the function circularSubarraySum() which finds the circular
subarray with maximum sum.

Constraints:
1 <= T <= 101
1 <= N <= 106
-106 <= Arr[i] <= 106

Example:
Input:
3
7
8 -8 9 -9 10 -11 12
8
10 -3 -4 7 6 5 -4 -1
8
-1 40 -14 7 6 5 -4 -1

Output:
22
23
52

Explanation:
Testcase 1: Starting from last element of the array, i.e, 12, and moving in
circular fashion, we have max subarray as 12, 8, -8, 9, -9, 10, which gives maximum
sum as 22.

** For More Input/Output Examples Use 'Expected Output' option **


{
import java.io.*;
import java.util.*;
class Array {

public static void main (String[] args) throws IOException {


BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim()); //Inputting the
testcases
while(t-->0){
int n = Integer.parseInt(br.readLine().trim());// input size of
array
int arr[] = new int[n];
String inputLine2[] = br.readLine().trim().split(" ");
for(int i=0; i<n; i++){
arr[i] = Integer.parseInt(inputLine2[i]); // input elements of
array
}

Kadane obj = new Kadane();

System.out.println(obj.circularSubarraySum(arr, n));// print the


circularSubarraySum
}
}
}

}
/*This is a function problem.You only need to complete the function given below*/
class Kadane{

// Function to find circular subarray with maximum sum


// a: input array
// n: size of array
static int circularSubarraySum(int a[], int n) {

// Your code here


return Integer.max(kadane(a,n), reverseKadane(a,n));
}

Das könnte Ihnen auch gefallen