# Largest in array without using conditionals / bitwise / ternary operators

• Difficulty Level : Easy
• Last Updated : 27 Sep, 2021

Define a function int max(int a[], int n) which returns the largest integer from an integer array consisting of n elements (without using conditionals / bitwise / ternary operators / library function to find largest
Examples:

```Input : arr[] = {16, 14, 15, 17, 9}
Output : 17

Input : arr[] = {10, 13, 11, 13}
Output : 13```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

The idea is similar to Maximum of four numbers
We use the fact that value of “(x – y + abs(x – y))” will be 0 of x is less than or equal to y. We use this value as index in an array of size 2 to pick maximum. Once we have found maximum of two elements, we can use same technique for finding maximum of all.

## C++

 `// CPP program to find largest in an array``// without conditional/bitwise/ternary/ operators``// and without library functions.``#include ``using` `namespace` `std;` `int` `maxArray(``int` `a[], ``int` `n)``{``    ``int` `tmp;``    ``tmp = a;``    ``for` `(``int` `i = 1; i < n; i++) {``        ``tmp = a[i];``        ``swap(tmp, tmp[``bool``(``abs``(tmp - tmp) +``                                  ``tmp - tmp)]);``    ``}``    ``return` `tmp;``}` `// Driver code``int` `main()``{``    ``int` `a[] = { 15, 11, 17, 16, 10 };``    ``int` `n = ``sizeof``(a) / ``sizeof``(a);``    ``cout << maxArray(a, n);``    ``return` `0;``}`

## Java

 `// JAVA program to find largest in an array``// without conditional/bitwise/ternary/ operators``// and without library functions.``import` `java.util.*;``class` `GFG``{` `static` `int` `maxArray(``int` `a[], ``int` `n)``{``    ``int` `[]tmp = ``new` `int``[``2``];``    ``tmp[``0``] = a[``0``];``    ``for` `(``int` `i = ``1``; i < n-``1``; i++) {``        ``tmp[``1``] = a[i];``      ` `        ``int` `temp = tmp[``0``];``        ``tmp[``0``] = tmp[(Math.abs(tmp[``1``] - tmp[``0``]) +``                ``tmp[``1``] - tmp[``0``])%``2``+``1``];``        ``tmp[(Math.abs(tmp[``1``] - tmp[``0``]) +``                ``tmp[``1``] - tmp[``0``])%``2``+``1``] = temp;``    ``}``    ``return` `tmp[``1``];``}` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``int` `a[] = { ``15``, ``11``, ``17``, ``16``, ``10` `};``    ``int` `n =a.length;``    ``System.out.print(maxArray(a, n));``}``}` `// This code is contributed by umadevi9616`

## Python3

 `# Python 3 program to find largest in an array``# without conditional/bitwise/ternary/ operators``# and without library functions.` `def` `maxArray(a, n):``    ``tmp ``=` `[a[i] ``for` `i ``in` `range``(``len``(a))]``    ``tmp[``0``] ``=` `a[``0``]``    ``for` `i ``in` `range``(``1``,n,``1``):``        ``tmp[``1``] ``=` `a[i]``        ``temp ``=` `tmp[``int``(``bool``(``abs``(tmp[``1``] ``-` `tmp[``0``]) ``+``                                ``tmp[``1``] ``-` `tmp[``0``]))]``        ``tmp[``int``(``bool``(``abs``(tmp[``1``] ``-` `tmp[``0``]) ``+``                         ``tmp[``1``] ``-` `tmp[``0``]))] ``=` `tmp[``0``]``        ``tmp[``0``] ``=` `temp``    ` `    ``return` `tmp[``0``]` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ``a ``=` `[``15``, ``11``, ``17``, ``16``, ``10``]``    ``n ``=` `len``(a)``    ``print``(maxArray(a, n))``        ` `# This code is contributed by``# Surendra_Gangwar`

## C#

 `// C# program to find largest in an array``// without conditional/bitwise/ternary/ operators``// and without library functions.``using` `System;` `public` `class` `GFG {` `    ``static` `int` `maxArray(``int``[] a, ``int` `n)``    ``{``        ``int``[] tmp = ``new` `int``;``        ``tmp = a;``        ``for` `(``int` `i = 1; i < n - 1; i++) {``            ``tmp = a[i];` `            ``int` `temp = tmp;``            ``tmp = tmp[(Math.Abs(tmp - tmp) + tmp``                          ``- tmp) % 2 + 1];``            ``tmp[(Math.Abs(tmp - tmp) + tmp``                 ``- tmp) % 2 + 1] = temp;``        ``}``        ``return` `tmp;``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main(String[] args)``    ``{``        ``int``[] a = { 15, 11, 17, 16, 10 };``        ``int` `n = a.Length;``        ``Console.Write(maxArray(a, n));``    ``}``}` `// This code is contributed by umadevi9616`

## Javascript

 ``

Output:

`17`

My Personal Notes arrow_drop_up