0% found this document useful (0 votes)
19 views6 pages

ADA Open Ended

Uploaded by

Angle Priya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views6 pages

ADA Open Ended

Uploaded by

Angle Priya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

OPEN ENDED EXPERIMENT

Over Smart Antonio


Problem Statement

Your friend Antonio is not a cool guy. While talking to his girlfriend he started discussing coding problems.

Unfortunately, his girlfriend is also a pro-coder like you. Seeing Antonio trying to be over-smart she gives
him a positive integer K and asks to find a pair of positive integers a and b such that a*b=K and LCM (a, b)
is minimum.

In case there are multiple pairs, she wants one with the largest a.

Now, Antonio is unable to solve this problem and asks you to help. Help him to save his relationship.
LCM- lowest common multiple of two integers is the smallest positive integer that is multiple of both given
integers.

Input Format
• The first line contains a single integer denoting K.

Output Format
• In a single line print a and b (the answer to the query).

Constraints
• 1<=K<=10^3

TEST CASES

TEST CASE I
Sample Input I
36
Sample Output
66
TEST CSE II
Sample Input II
288
Sample Output
24 12

Explanation of TEST CASE II


a=24, b=12, a*b = 24*12 = 288
Also, it can be proved that among all a’s 24 is the maximum value of a.
METHOD 1:
BRUTE FORCE(Iteration)
PSEUDO CODE:

GROUP (*arr,k,*arr21,*arr22,no_group,*arr3)
For i=1 to no_group
For j=1 to no_group
if(arr[i]*arr[j] == k)
arr21[j] <- arr[i]
arr22[j] <- arr[j]
arr3[j] <- lcm(arr21[j],arr22[j])
end of loop 1
end of loop0 2
declare int res <- arr3[1],loc <- 0;
for i = 1 to no_group
res <- min(res, arr3[i])
for no_group to 1
if(arr3[i]==res)
loc <- i

CODE:
#include<iostream>

using namespace std;

int factors_count(int K)
{
int count = 0;
for(int i=1;i<=K;i++)
{
if(K%i == 0)
{
count++;
}
}
return count;
}

void factors(int K, int *arr, int j)


{
for(int i=1;i<=K;i++)
{
if(K%i == 0)
{
arr[j] = i;
j++;
}
}
}

long gcd(long int a, long int b)


{
if (b == 0)
return a;
return gcd(b, a % b);
}
long lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}

void group(int *arr,int k,int *arr21,int *arr22,int no_group,int *arr3)


{
for(int i=0;i<no_group;i++)
{
for(int j=0;j<no_group;j++)
{
if(arr[i]*arr[j] == k)
{
arr21[j]=arr[i];
arr22[j]=arr[j];
arr3[j] = lcm(arr21[j],arr22[j]);
}
}
}
int res = arr3[0],loc = 0;
for (int i = 1; i < no_group; i++)
{
res = min(res, arr3[i]);
}
for(int i=no_group-1;i>=0;i--)
{
if(arr3[i]==res)
loc =i;
}
cout<<endl<<arr21[loc]<<" "<<arr22[loc]<<endl<<endl;

}
int main()
{
int K;
cout<<"Enter the positive number:";
cin>>K;
int n=factors_count(K);
int *arr = new int(n);
int *arr3 = new int(n);
int *arr21 = new int(n);
int *arr22 = new int(n);
factors(K,arr,0);
group(arr,K,arr21,arr22,n,arr3);
return 0;
}

OUTPUT:

TIME COMPLEXITY: O(n2)


METHOD 1:
HASHING
PSEUDO CODE:

CODE:
#include<iostream>
#include <unordered_set>
using namespace std;

int factors_count(int K)
{
int count = 0;
for(int i=1;i<=K;i++)
{
if(K%i == 0)
{
count++;
}
}
return count;
}

void factors(int K, int *arr, int j)


{
for(int i=1;i<=K;i++)
{
if(K%i == 0)
{
arr[j] = i;
j++;
}
}
}

long gcd(long int a, long int b)


{
if (b == 0)
return a;
return gcd(b, a % b);
}
long lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}

void group(int *arr,int k,int no_group,int *arr3)


{
unordered_set <int> s;
unordered_set <int> s1;
for (int i=0; i<no_group; i++)
{
int z=(k/arr[i]);
if (s.find(z) == s.end() || s.find(z) != s.end())
{
s1.insert(z);
arr3[i] = lcm(arr[i],z);
}
s.insert(arr[i]);

}
int res = arr3[0],loc = 0,count =0;
for (int i = 1; i < no_group; i++)
{
res = min(res, arr3[i]);
}
for(int i=no_group-1;i>=0;i--)
{
if(arr3[i]==res)
loc =i;
}
unordered_set<int> :: iterator itr;

for (itr = s1.begin(); itr != s1.end(); itr++)


{
if(count==loc)
{
cout<<*itr<<" ";
break;
}
else
count++;
}
count=0;
for (itr = s.begin(); itr != s.end(); itr++)
{
if(count==loc)
{
cout<<*itr<<" ";
break;
}
else
count++;

int main()
{
int K;
cout<<"Enter the positive number:";
cin>>K;
int n=factors_count(K);
int *arr = new int(n);
int *arr3 = new int(n);
factors(K,arr,0);
group(arr,K,n,arr3);

return 0;
}

OUTPUT:

TIME COMPLEXITY: O(n)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy