Find max in struct array Last Updated : 30 Apr, 2024 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice Given a struct array of type Height, find max struct Height{ int feet; int inches;}Recommended PracticeMaximum in Struct ArrayTry It!Question source : Microsoft Interview Experience Set 127 | (On-Campus for IDC) The idea is simple, traverse the array, and keep track of max value value of array element(in inches) = 12*feet + inches Implementation: CPP // CPP program to return max // in struct array #include <climits> #include <iostream> using namespace std; // struct Height // 1 feet = 12 inches struct Height { int feet; int inches; }; // return max of the array int findMax(Height arr[], int n) { int mx = INT_MIN; for (int i = 0; i < n; i++) { int temp = 12 * (arr[i].feet) + arr[i].inches; mx = max(mx, temp); } return mx; } // driver program int main() { // initialize the array Height arr[] = { { 1, 3 }, { 10, 5 }, { 6, 8 }, { 3, 7 }, { 5, 9 } }; int res = findMax(arr, 5); cout << "max :: " << res << endl; return 0; } Java // Java program to return max // in class array import java.lang.Integer; // class Height // 1 feet = 12 inches class Height { int feet; int inches; } public class Main { // return max of the array public static int findMax(Height arr[]) { int mx = Integer.MIN_VALUE; for (int i = 0; i < arr.length; i++) { int temp = 12 * (arr[i].feet) + arr[i].inches; mx = Math.max(mx, temp); } return mx; } // driver program public static void main(String[] args) { // initialize the array Height arr[] = { new Height(){ { feet = 1; inches = 3; } } , new Height() { { feet = 10; inches = 5; } }, new Height() { { feet = 6; inches = 8; } }, new Height() { { feet = 3; inches = 7; } }, new Height() { { feet = 5; inches = 9; } } } ; int res = findMax(arr); System.out.println("max :: " + res); } } Python3 # Python program to return max # in list of dictionaries # Importing the sys module for INT_MIN import sys # Dictionary to represent Height # 1 feet = 12 inches class Height: def __init__(self, feet, inches): self.feet = feet self.inches = inches # Function to return max of the list def findMax(arr): mx = -sys.maxsize - 1 for i in arr: temp = 12 * i['feet'] + i['inches'] mx = max(mx, temp) return mx # Driver program if __name__ == "__main__": # Initialize the list arr = [ {'feet': 1, 'inches': 3}, {'feet': 10, 'inches': 5}, {'feet': 6, 'inches': 8}, {'feet': 3, 'inches': 7}, {'feet': 5, 'inches': 9} ] res = findMax(arr) print("max ::", res) JavaScript class Height { constructor(feet, inches) { this.feet = feet; this.inches = inches; } } function findMax(arr) { let mx = Number.MIN_VALUE; for (let i = 0; i < arr.length; i++) { let temp = 12 * arr[i].feet + arr[i].inches; mx = Math.max(mx, temp); } return mx; } // Driver program function main() { // Initialize the array const arr = [ new Height(1, 3), new Height(10, 5), new Height(6, 8), new Height(3, 7), new Height(5, 9) ]; const res = findMax(arr); console.log("max :: " + res); } main(); Outputmax :: 125 Comment More infoAdvertise with us Next Article Find max in struct array M msdeep14 Improve Article Tags : DSA Arrays Microsoft Practice Tags : MicrosoftArrays Similar Reads Find the Target number in an Array Finding a number within an array is an operation, in the field of computer science and data analysis. In this article, we will discuss the steps involved and analyze their time and space complexities. Examples: Input: Array: {10, 20, 30, 40, 50} , Target: 30Output: "Target found at index 2" Input: A 13 min read Flexible Array Members in a structure in C Flexible Array Member(FAM) is a feature introduced in the C99 standard of the C programming language.For the structures in C programming language from C99 standard onwards, we can declare an array without a dimension and whose size is flexible in nature.Such an array inside the structure should pref 4 min read Searching in Array Searching is one of the most common operations performed in an array. Array searching can be defined as the operation of finding a particular element or a group of elements in the array. There are several searching algorithms. The most commonly used among them are: Linear Search Binary Search Ternar 4 min read LMNs-Data Structures Data structures are ways to organize and store data so it can be used efficiently. They are essential in computer science for managing and processing information in programs. Common types of data structures include arrays, linked lists, stacks, queues, trees, and graphs. Each structure is designed f 14 min read Find a String in given Array of Strings using Binary Search Given a sorted array of Strings arr and a string x, The task is to find the index of x in the array using the Binary Search algorithm. If x is not present, return -1.Examples:Input: arr[] = {"contribute", "geeks", "ide", "practice"}, x = "ide"Output: 2Explanation: The String x is present at index 2. 6 min read Like