Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Little Elephant and Divisors CodeChef Solution

Problem -Little Elephant and Divisors CodeChef Solution

This website is dedicated for CodeChef solution where we will publish right solution of all your favourite CodeChef problems along with detailed explanatory of different competitive programming concepts and languages.
<<Previous CodeChef Problem Next Codechef Problem>>

CodeChef Solution CodeChef Solution in C++17


#include<bits/stdc++.h>
using namespace std;
#define all(arr) arr.begin(),arr.end()
#define rep(i,s,e) for(int i=s;i<e;i++)
#define lli long long int
#define ll long long
const ll INF=1e18;
const int mod=1e9+7;

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t;cin>>t;
	while(t--){
		int n;cin>>n;vector<int>arr(n);for(int i=0;i<n;i++){cin>>arr[i];}
		int gg=arr[0];
		for(int i=1;i<n;i++){
			gg=__gcd(gg,arr[i]);
		}
		if(gg==1){
			cout<<-1<<"\n";continue;
		}int ans=gg;
		for(int i=2;i*i<=gg;i++){
			if(gg%i==0){
				ans=i;break;
			}
		}cout<<ans<<"\n";
	}
	return 0;
}

Little Elephant and Divisors CodeChef Solution in C++14

#include<bits/stdc++.h>
using namespace std;
#define all(arr) arr.begin(),arr.end()
#define rep(i,s,e) for(int i=s;i<e;i++)
#define lli long long int
#define ll long long
const ll INF=1e18;
const int mod=1e9+7;

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t;cin>>t;
	while(t--){
		int n;cin>>n;vector<int>arr(n);for(int i=0;i<n;i++){cin>>arr[i];}
		int gg=arr[0];
		for(int i=1;i<n;i++){
			gg=__gcd(gg,arr[i]);
		}
		if(gg==1){
			cout<<-1<<"\n";continue;
		}int ans=gg;
		for(int i=2;i*i<=gg;i++){
			if(gg%i==0){
				ans=i;break;
			}
		}cout<<ans<<"\n";
	}
	return 0;
}

Little Elephant and Divisors CodeChef Solution in PYTH 3

# cook your dish here
from math import gcd
from math import sqrt
from functools import reduce
t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int,input().split()))
    l = reduce(gcd,a)
    if l>1:
        mind = l
        for i in range(2,int(sqrt(l))+1):
            if l%i==0:
                mind = i
                break
        print(mind)
    else: print(-1)

Little Elephant and Divisors CodeChef Solution in C

#include <stdio.h>
int gcd(int a, int b)
{
    if(a < b)
    {
        int temp = a;
        a=b;
        b=temp;
    }
    if(b == 0)
    {
        return a;
    }
    else
    {
        gcd(b, a%b);
    }
}
int main()
{
    int t;
    
    scanf("%d", &t);
    
    while(t--)
    {
        int n, p;
        
        scanf("%d", &n);
        
        int a[n];
        
        for(int i=0; i<n; i++)
        {
            scanf("%d", &a[i]);
        }
        
        p=a[0];
        
        for(int i=1; i<n; i++)
        {
            p = gcd(a[i], p);
        }
        
        if(p == 1)
        {
            printf("-1\n");
            continue;
        }
        int r=0;
        for(int i=2; i*i<=p; i++)
        {
            if(p%i == 0)
            {
                r++;
                printf("%d\n", i);
                break;
            }
        }
        
        if(r == 0)
        {
            printf("%d\n", p);
        }
    }
}

Little Elephant and Divisors CodeChef Solution in JAVA

/* package codechef; // don't place package name! */

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
{
    static int gcd(int a, int b)
    {
        if (a == 0)
            return b;
        return gcd(b % a, a);
    }
    static int lcm(int a, int b)
    {
        return (a / gcd(a, b)) * b;
    }
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		
		while(T-->0){
		    int n = sc.nextInt();
		    int[] arr = new int[n];
		    int max  = 0;
		    for(int i=0;i<n ;i++){
		        arr[i]= sc.nextInt();
		        max=Integer.max(max,arr[i]);
		    }
		    int res = arr[0];
		    for(int i=1;i<n;i++){
		        res = gcd(arr[i],res);
		    }
		    int result = -1;
		    boolean found =false;
		    for(int i=2;i*i<=res;i++){
		        
		            if(res%i==0){
		                result =i;
		                found=true;
		                break;
		            }
		            
		        
		    }
		        if(found){
		            
		    System.out.println(result);
		        }else{
		            if(res<=1){
		                System.out.println(-1);
		            }else{
		                
		            System.out.println(res);
		            }
		        }    
		        
		    
		    
		}
	}
}

Little Elephant and Divisors CodeChef Solution in PYPY 3

from math import gcd,sqrt
from functools import reduce
for _ in range(int(input())):
    n=int(input())
    l1=list(map(int,input().split()))
    a=reduce(gcd,l1)
    if a==1:
        print(-1)
    else:
        for i in range(2,int(sqrt(a))+1):
            if a%i==0:
                print(i)
                break
        else:
            print(a)

Little Elephant and Divisors CodeChef Solution in PYTH

N = 10**3 # value of N required
def primesieve(N):
	n = N+1
	sieve = [True] * (n/2)
	for i in xrange(3,int(n**0.5)+1,2):
		if sieve[i/2]:
			sieve[i*i/2::i] = [False] * ((n-i*i-1)/(2*i)+1)
		# endif
	# endfor i
	return [2] + [2*i+1 for i in xrange(1,n/2) if sieve[i]]
# end fun
primes = primesieve(N)
def gcd(n, m):
	r = n % m
	while r > 0:
		n = m
		m = r
		r = n % m
	#endwhile
	return m
#end fun
t = int(raw_input())
for i in range(t):
	N = int(raw_input())
	st = raw_input().split()
	g = int(st[0])
	k = 1
	while (k < N) and (g > 1):
		n = int(st[k])
		g = gcd(g,n)
		k += 1
	# endwhile
	r = -1
	if g > 1:
		sq = int(g**0.5)
		q = 0
		p = primes[q]
		while (p <= sq) and (g%p > 0):
			q += 1
			p = primes[q]
		# endwhile
		if g%p == 0:
			r = p
		else:
			r = g
		# endif
	# endif
	print r
# endfor i


Little Elephant and Divisors CodeChef Solution in C#

using System;

namespace CC_E_LittleElephantDivisors
{
	class MainClass
	{
		public static void Main(string[] args)
		{
			int terms = int.Parse(Console.ReadLine());
			int[] result = new int[terms];

			for (int i = 0; i < terms; i++)
			{
				int lenght = int.Parse(Console.ReadLine());
				int[] array = Array.ConvertAll(Console.ReadLine().Trim().Split(), Convert.ToInt32);
				result[i] = array[0];

				for (int j = 1; j < lenght; j++)
				{
					result[i] = GCD(result[i], array[j]);
				}

				if (result[i] == 1)
					result[i] = -1;
				else
				{
					for (int j = 2; j <= Math.Sqrt(result[i]); j++)
					{
						if (result[i] % j == 0)
						{
							result[i] = j;
							break;
						}
					}
				}
			}

			foreach (int divisor in result)
				Console.WriteLine(divisor);

			Console.ReadLine();
		}

		public static int GCD(int a, int b) 
		{
			if (b == 0)
				return a;
			return GCD(b, a % b);
		}
	}
}

Little Elephant and Divisors CodeChef Solution in NODEJS

var input = '';

function cacheInput(data) {
	input += data;
}

function callMain() {
	input = input.split(/\s+/).map(Number);
	main();
}

var readNumber = function() {
	var i = -1;
	return function() {
		return input[++i];
	};
}();

function GCD(a, b) {
	var r = a % b;
	while (r != 0) {
		a = b;
		b = r;
		r = a % b;
	}
	return b;
}

function smallestPrimeFactor(number) {
	for (var s = 2; s * s <= number; ++s) {
		if (number % s == 0) {
			return s;
		}
	}
	return number;
}

function main() {
	var T = readNumber();
	var output = '';
	var smallestPrimes = new Array(100001);
	for (var i = 0; i < 100001; ++i) {
		smallestPrimes[i] = smallestPrimeFactor(i);
	}
	for (var t = 0; t < T; ++t) {
		var N = readNumber();
		var gcd = readNumber();
		for (var i = 1; i < N; ++i) {
			gcd = GCD(gcd, readNumber());
		}
		if (gcd == 1) {
			output += '-1\n';
		}
		else {
			output += smallestPrimes[gcd] + '\n';
		}
	}
	process.stdout.write(output);
	process.exit();
}

process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', cacheInput).on('end', callMain);
Little Elephant and Divisors CodeChef Solution Review:

In our experience, we suggest you solve this Little Elephant and Divisors CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Little Elephant and Divisors CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Little Elephant and Divisors CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

Cognitive Class Answer

CodeChef Solution

Microsoft Learn

Leave a Reply

Your email address will not be published. Required fields are marked *