Skip to main content

Finding Largest Element in an Array

Optimized Approach for Finding Largest Element in an Array .

Approach:

1) Initialize a variable named "largest" with first element of an array.
2) Compare the "largest" with every element in an array .
3) If element in the array (arr[i]) is greater than "largest" then initialize "largest" with that element i.e arr[i].
4) Print "largest" which is the largest element in the array.

Program:

#include<iostream>
using namespace std;
int main()
{
int n,largest;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
largest=arr[0];
for(int i=1;i<n;i++)
{
if(arr[i]>largest)
{
largest=arr[i];
}
}
cout<<"Largest Element in an array is:"<<largest;
return 0;
}

Output:







Time Complexity : O(N)


Comments

Popular posts from this blog

Embedded Systems

  Introduction to Embedded Systems WHAT IS AN EMBEDDED SYSTEM? An embedded system is an electronic/electro-mechanical system designed to perform a specific function and is a combination of both hardware and firmware(software). Difference between embedded and general purpose computing system Embedded System A system which is a combination of special purpose hardware and embedded OS for executing a specific set of applications. May or may not contain an operating system for functioning. The firmware of the embedded system is pre-programmed and it is non alterable by the end-user (There may be exceptions for systems supporting OS kernel image flashing through special hardware settings). Application-specific requirements (like performance, power requirements, memory usage, etc.) are the key deciding factors. Highly tailored to take advantage of the power saving modes supported by the hardware and the operating system. For certain category of embedded systems like mission critical syste...

EMBEDDED SYSTEMS

  Major Application Areas of Embedded Systems We are living in a world where embedded systems play a vital role in our day-to-day life, starting from home to the computer industry, where most of the people find their job for a livelihood. Embedded technology has acquired a new dimension from its first generation model, the Apollo guidance computer, to the latest radio navigation system combined with in-car entertainment technology and the wearable computing devices (Apple watch, Microsoft Band, Fitbit fitness trackers etc.). The application areas and the products in the embedded domain are countless. A few of the important domains and products are listed below: (1) Consumer electronics: Camcorders, cameras, etc. (2) Household appliances: Television, DVD players, washing machine, fridge, microwave oven, etc.  (3) Home automation and security systems: Air conditioners, sprinklers, intruder detection alarms, close! circuit television cameras, fire alarms, etc. (4) Automotive indu...

Linked List Insertion

 Linked List Insertion at Begin, Insertion at End, Insertion at Position Program: #include<stdlib.h> #include <stdio.h>       void create(); void display(); void insert_at_begin(); void insert_at_end(); void insert_at_position();   struct node {         int data;         struct node *next; }; struct node *head=NULL; int main()      {         int choice;         while(1){                                                  printf("\n 1.Create \n");                 printf("\n 2.Display \n");                 printf("\n 3.Insert at the beginning \n");                 printf("\n 4.Insert at the end \n");      ...