Join Regular Classroom : Visit ClassroomTech

Accenture Coding Question | Number Sum | CodeWindow

Given an array of size n, write a function to find the sum of the largest and smallest element in the array.
Input specification:
Input1: integer n, describing the size of the given array.
Input2: integer array, specifying the elements of the array.

Output Specification:
Return an integer as the sum of the smallest and the largest elements in the given array.

Example 1:
Input 1: 4
Input 2: {9,5,0,11}

Output: 11
Explanation:
The largest and smallest elements in the given array are 11 and 0 respectively. Sum of the two elements is 11+0=11

Example 2:
Input 1: 7
Input 2: {-9,15,80,101,4,95,-101}

Output: 0
Explanation:
The largest and smallest elements in the given array are 101 and -101 respectively. Sum of the two elements is 101+(-101)=0

Solution in Python 3:

#https://codewindow.in
#join our telegram channel

n=input()
s=list(map(int, input().split(',')))

maxi=max(s) # finding the maximum number

mini=min(s) # finding the minimum number


print((maxi)+(mini))

# telegram @codewindow

Solution in C :

//https://codewindow.in
//join our telegram channel

#include <stdio.h>

int main(void) {
	// your code goes here
	int n;
	scanf("%d",&n);
	int a[n];
	for(int i=0;i<n;i++)
	    scanf("%d",&a[i]);
	int max=a[0], min=a[0];
	for(int i=1;i<n;i++) {
	    if(a[i] > max)
	        max = a[i];
	    if(a[i] < min)
	        min = a[i];
	}
	int sum = max+min;
	printf("%d",sum);
	return 0;
}
//telegram @codewindow

Solution in C++ :

//https://codewindow.in
//join our telegram channel

#include <iostream>
using namespace std;

int main(void) {
	// your code goes here
	int n;
	cin >> n;
	int a[n];
	for(int i=0;i<n;i++)
	    cin >> a[i];
	int max=a[0], min=a[0];
	for(int i=1;i<n;i++) {
	    if(a[i] > max)
	        max = a[i];
	    if(a[i] < min)
	        min = a[i];
	}
	int sum = max+min;
	cout << sum;
	return 0;
}
//telegram @codewindow

Solution in JAVA :

//https://codewindow.in
//join our telegram channel


import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws Exception
	{
    	// your code goes here
    	Scanner sc = new Scanner(System.in);
    	int n = sc.nextInt();
    	int a[] = new int[n];
    	for(int i=0;i<n;i++)
    	    a[i] = sc.nextInt();
    	int max=a[0], min=a[0];
    	for(int i=1;i<n;i++) {
    	    if(a[i] > max)
    	        max = a[i];
    	    if(a[i] < min)
    	        min = a[i];
    	}
    	int sum = max+min;
    	System.out.println(sum);
	}
}
//telegram @codewindow

Output:

4
9,5,0,11
11
Categories
Pages
Recent Posts