Skip to content

Commit 60ec246

Browse files
added divisible by 7 number
1 parent e581d47 commit 60ec246

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

Maths/DivisibleBy7.txt

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
Given an n-digit large number in form of string, check whether it is divisible by 7 or not. Print 1 if divisible by 7, otherwise 0.
2+
3+
4+
Example 1:
5+
6+
Input: num = "8955795758"
7+
Output: 1
8+
Explanation: 8955795758 is divisible
9+
by 7.
10+
11+
12+
class Solution{
13+
public:
14+
int isdivisible7(string num){
15+
//complete the function here
16+
int n=num.size();
17+
int first = (num[n-1]-'0') * 2;
18+
int sum=0;
19+
for(int i=0; i<num.size()-1; i++){
20+
21+
int dig = num[i]-'0';
22+
sum=sum*10+dig;
23+
24+
sum=sum%7;
25+
26+
}
27+
28+
// cout<<sum<<endl;
29+
30+
int diff=sum-first;
31+
32+
33+
if(diff%7==0){
34+
return 1;
35+
}
36+
37+
38+
39+
40+
return 0;
41+
42+
}
43+
};
44+
45+
TC: O(n)
46+
SC: O(1)

0 commit comments

Comments
 (0)