Learn-dsa..in 30 days!



























CC-19 : Find maximum triangle perimeter.

Description:

Given an input array of numbers, find the sides of triangle with maximum perimeter that can be formed assuming each array element data values represent length of one possible side of the triangle.

Test cases and expected outputs:

Input Parameters Expected outputs
Array:
33, 3, 4, 97, 62, 122, 124, 20, 1,
Descending Sorted Array:
124, 122, 97, 62, 33, 20, 4, 3, 1,

Sides of Triangle with maximum perimeter:
124, 122, 97,

Pseudocode:

maxTrianglePerimeter(nums):

Integer array named nums is received as input parameter.
Set nums=bubbleSortDecreasing(nums).
Initialize int array of length 3 named triangleSides.
Iterate through nums using for loop with idx as iteration variable and values of idx ranging from 0 to nums.length-1:
(nums[idx] < (num[idx+1]+nums[idx+2])) is the necessary mathematical proof needed to ensure nums[idx], num[idx+1], nums[idx+2] can form a viable triangle. So If (nums[idx] < (num[idx+1]+nums[idx+2])) then:
Set triangleSides[0] to nums[idx].
Set triangleSides[1] to nums[idx+1].
Set triangleSides[2] to nums[idx+2].
Exit the method using break statement.
Return triangleSides, it contains the 3 sides that can form triangle with maximum parameter using numbers from nums.

bubbleSortDecreasing(nums[]):

Integer array named nums is received as input parameter.
Initialize boolean variable noSwap to false. If in an iteration of elements of array, no elements get swapped, it means the array is sorted. We will use variable noSwap to track if any elements got swapped in the current iteration.
Iterate through using for loop with iidx as iteration variable and values of iidx ranging from 0 to nums.length:
Set noSwap to false.
Iterate through using for loop with jidx as iteration variable and values of jidx ranging from 0 to (nums.length-iidx-1):
If nums[jidx] < nums[jidx+1], then swap order is wrong so swap the data at these indices.
If whole inner iteration is finished and noSwap is still 0, that means the array is already sorted and no further iterations are needed so we can return from the method.
The element of nums array have been sorted now using Bubble Sort, so return nums array from the method.

Code:

public int[] bubbleSortDecreasing(int[] nums){
	boolean noSwap=false;
	for (int iidx=0;iidx < nums.length;iidx++) {
		noSwap=false;
		for (int jidx=0;jidx < nums.length-iidx-1;jidx++) {
			if (nums[jidx] < nums[jidx+1]) {
				int swap=nums[jidx];
				nums[jidx]=nums[jidx+1];
				nums[jidx+1]=swap;
				noSwap=false;
			}
			if(noSwap==true) {
				break;
			}
		}
	}
	return nums;
}

public int[] maxTrianglePerimeter(int[] nums) {
	nums=bubbleSortDecreasing(nums);
	int[] triangleSides=new int[3];
	for (int idx=0; idx < nums.length-2; idx++) {
		if (nums[idx] < (nums[idx+1]+nums[idx+2])) {
			triangleSides[0]=nums[idx];
			triangleSides[1]=nums[idx+1];
			triangleSides[2]=nums[idx+2];
			break;
		}
	}
	return triangleSides;
}

Click here to download and run code and test cases !