Answer to Question #268113 in Java | JSP | JSF for Meghana

Question #268113

Placement session begins code by using array and integer in java plz help


Input an integer n which denotes the no of students present



1
Expert's answer
2021-11-18T06:54:56-0500
public class GFG
{
	static boolean isPossible(int arr[], int n, int m, int curr_min)
	{
		int curr_sum = 0;
		for (int i = 0; i < n; i++)
		{
			if (arr[i] > curr_min)
				return false;
	
			if (curr_sum + arr[i] > curr_min)
			{
				studentsRequired++;
				curr_sum = arr[i];
    			return false;
			}
			else
				curr_sum += arr[i];
		}
		return true;
	}
	static int findPages(int arr[], int n, int m)
	{
		long sum = 0;
		if (n < m)
			return -1;
		for (int i = 0; i < n; i++)
			sum += arr[i];
		int start = 0, end = (int) sum;
		int result = Integer.MAX_VALUE;
		while (start <= end)
		{
			int mid = (start + end) / 2;
			if (isPossible(arr, n, m, mid))
			{
				result = mid;
				end = mid - 1;
			}
			else
				start = mid + 1;
		}
		return result;
	}
	public static void main(String[] args)
	{
		int arr[] = {12, 34, 67, 90};
		int m = 2; //No. of students
		System.out.println("Minimum number of pages = " +
						findPages(arr, arr.length, m));
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS