The document defines a Java class that checks if an integer array is a Madhav array. A Madhav array is one where the length is of the form n(n+1)/2 and the sum of elements from index 0 to n is equal to the sum of elements from index n to 2n and so on. The class contains a main method that calls the isMadhavArray method on different test arrays, printing the result. The isMadhavArray method calculates the length and sum incrementally from the start to end indexes, returning 1 if it is a Madhav array or 0 otherwise.
The document defines a Java class that checks if an integer array is a Madhav array. A Madhav array is one where the length is of the form n(n+1)/2 and the sum of elements from index 0 to n is equal to the sum of elements from index n to 2n and so on. The class contains a main method that calls the isMadhavArray method on different test arrays, printing the result. The isMadhavArray method calculates the length and sum incrementally from the start to end indexes, returning 1 if it is a Madhav array or 0 otherwise.
The document defines a Java class that checks if an integer array is a Madhav array. A Madhav array is one where the length is of the form n(n+1)/2 and the sum of elements from index 0 to n is equal to the sum of elements from index n to 2n and so on. The class contains a main method that calls the isMadhavArray method on different test arrays, printing the result. The isMadhavArray method calculates the length and sum incrementally from the start to end indexes, returning 1 if it is a Madhav array or 0 otherwise.
int isMadhavArray = 0; int arrayLength = a.length; int startIndex = 0; // start index of current array elements under consideration int endIndex = 0; // end index of current array elements under consideration int n = 1; // track current length of array elements between start and end index int currentSum = 0; // current sum of array elements int sum = 0; // sum of elements of array between startIndex and endIndex int currentLengthOfArray = 1; // length of array from index 0 to endIndex int calculatedLengthOfArray = 1; // length calculating n * (n+1)/2 while(currentLengthOfArray <= arrayLength){ if(currentLengthOfArray == calculatedLengthOfArray){ for(int index = startIndex; index <= endIndex; index++){ sum += a[index]; } }else{ // Fail ::: The length of a Madhav array must be n*(n+1)/2 for some n isMadhavArray = 0; break; } if((currentSum == sum) || (startIndex == endIndex)){ // startIndex == endIndex to satisfy initial condition currentSum = sum; sum = 0; isMadhavArray = 1; }else{ // Fail ::: The sum is not equal isMadhavArray = 0; break; } if(currentLengthOfArray == arrayLength){ // already at the end of the array break; }else{ startIndex = endIndex + 1; endIndex = startIndex + n; if((startIndex < arrayLength && endIndex < arrayLength)){ // operating inside the array bounderies n++; }else{ // adjusting boundries; so that lastIndex points last element of array endIndex = arrayLength - 1; n = endIndex - startIndex; } calculatedLengthOfArray = n * (n + 1)/2; currentLengthOfArray = endIndex + 1; } } return isMadhavArray; } }