Skip to content

Commit f0b89b6

Browse files
authored
Merge pull request ashutosh97#58 from everlearner/Divisibility-Question
Added competitive question with sol
2 parents 791958d + 1d5c0f7 commit f0b89b6

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

Divisibility_Check/Question.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
You are given an array of integers. Check whether there exists a number in this array which is divisible by all other numbers in this array. Output 1, if such a number exists, and 0 otherwise.
2+
3+
Input
4+
The only line of the input contains a list of space-separated integers ai (1 ≤ ai ≤ 100) — elements of the array. The size of the array is between 2 and 10, inclusive. Note that the size of the array is not given explicitly!
5+
6+
Output
7+
Output 1 if there exists element of this array which is divisible by all other elements of the array, and 0 otherwise.

Divisibility_Check/Solution.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
int main()
4+
{
5+
char amp[100];
6+
cin.getline(amp,99,'\n');
7+
int len=strlen(amp);
8+
9+
10+
int arr[10]={0};
11+
12+
int count=0,i=0;
13+
int start=0;
14+
for(int l=start;l<=len;l++)
15+
{
16+
17+
if(amp[l]==' ' || amp[l]=='\0')
18+
{
19+
//cout<<"hi "<<l<<" "<<start<<" ";
20+
count++;
21+
for(int m=l-1,n=0;m>=start;m--,n++)
22+
{
23+
arr[i]+= (amp[m]-'0')*pow(10,n);
24+
25+
}
26+
27+
if(l==len)
28+
break;
29+
i++;
30+
start=l+1;
31+
}
32+
33+
}
34+
35+
sort(arr,arr+count);
36+
int ans=1;
37+
for(int a=count-2;a>=0;a--)
38+
{
39+
if(arr[count-1]%arr[a]!=0)
40+
{
41+
ans=0;
42+
break;
43+
}
44+
}
45+
46+
cout<<ans;
47+
return 0;
48+
}

0 commit comments

Comments
 (0)