Given a number n, the task is to find the remainder when n is divided by 11. The input of number may be very large.
Examples:
Input : str = 13589234356546756
Output : 6
Input : str = 3435346456547566345436457867978
Output : 4
Since the given number can be very large, we can not use n % 11. There are some steps that needs to be used to find remainder:
1. Store number in string.
2. Count length of number string.
3. Convert string character one by one
into digit and check if it's less than
11. Then continue for next character
otherwise take remainder and use
remainder for next number.
4. We get remainder.
Ex. str = "1345"
len = 4
rem = 3
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int remainder(string str)
{
int len = str.length();
int num, rem = 0;
for ( int i = 0; i < len; i++) {
num = rem * 10 + (str[i] - '0' );
rem = num % 11;
}
return rem;
}
int main()
{
string str = "3435346456547566345436457867978" ;
cout << remainder(str);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int remainder(String str)
{
int len = str.length();
int num, rem = 0 ;
for ( int i = 0 ; i < len; i++) {
num = rem * 10 + (str.charAt(i) - '0' );
rem = num % 11 ;
}
return rem;
}
public static void main(String args[])
{
String str = "3435346456547566345436457867978" ;
System.out.println(remainder(str));
}
}
|
Python3
def remainder(st) :
ln = len (st)
rem = 0
for i in range ( 0 , ln) :
num = rem * 10 + ( int )(st[i])
rem = num % 11
return rem
st = "3435346456547566345436457867978"
print (remainder(st))
|
C#
using System;
class GFG
{
static int remainder( string str)
{
int len = str.Length;
int num, rem = 0;
for ( int i = 0; i < len; i++)
{
num = rem * 10 + (str[i] - '0' );
rem = num % 11;
}
return rem;
}
public static void Main()
{
string str = "3435346456547566345436457867978" ;
Console.WriteLine(remainder(str));
}
}
|
PHP
<?php
function remainder( $str )
{
$len = strlen ( $str );
$num ; $rem = 0;
for ( $i = 0; $i < $len ; $i ++)
{
$num = $rem * 10 +
( $str [ $i ] - '0' );
$rem = $num % 11;
}
return $rem ;
}
$str = "3435346456547566345436457867978" ;
echo (remainder( $str ));
?>
|
Javascript
<script>
function remainder(str)
{
let len = str.length;
let num;
let rem = 0;
for (let i = 0; i < len; i++)
{
num = rem * 10 +
(str[i] - '0' );
rem = num % 11;
}
return rem;
}
let str = "3435346456547566345436457867978" ;
document.write(remainder(str));
</script>
|
Time Complexity: O(L ) where L is length of the string
Auxiliary Space: O(L )
Another Approach:
To find the remainder when a large number is divided by 11, we can use the following rule:
- Starting from the rightmost digit, we alternate between adding and subtracting the digits. That is, we add the rightmost digit, subtract the next digit to the left, add the next digit, and so on.
- If the result is divisible by 11, the original number is also divisible by 11, and the remainder is 0.
- Otherwise, the remainder is the absolute value of the result modulo 11.
C++
#include<bits/stdc++.h>
using namespace std;
int remainder(string str)
{
int n = str.length();
int sum = 0;
for ( int i = n-1; i >= 0; i--) {
int digit = str[i] - '0' ;
if ((n-i) % 2 == 0) {
sum = (sum - digit + 11) % 11;
} else {
sum = (sum + digit) % 11;
}
}
return sum;
}
int main()
{
string str = "3435346456547566345436457867978" ;
cout << remainder(str);
return 0;
}
|
Java
import java.util.*;
public class Main
{
public static int remainder(String str)
{
int n = str.length();
int sum = 0 ;
for ( int i = n - 1 ; i >= 0 ; i--)
{
int digit = str.charAt(i) - '0' ;
if ((n - i) % 2 == 0 )
{
sum = (sum - digit + 11 ) % 11 ;
}
else
{
sum = (sum + digit) % 11 ;
}
}
return sum;
}
public static void main(String[] args)
{
String str = "3435346456547566345436457867978" ;
System.out.println(remainder(str));
}
}
|
Python3
def remainder( str ):
n = len ( str )
sum = 0
for i in range (n - 1 , - 1 , - 1 ):
digit = int ( str [i])
if (n - i) % 2 = = 0 :
sum = ( sum - digit + 11 ) % 11
else :
sum = ( sum + digit) % 11
return sum
str = "3435346456547566345436457867978"
print (remainder( str ))
|
C#
using System;
class Program
{
static int Remainder( string str)
{
int n = str.Length;
int sum = 0;
for ( int i = n - 1; i >= 0; i--)
{
int digit = str[i] - '0' ;
if ((n - i) % 2 == 0)
{
sum = (sum - digit + 11) % 11;
}
else
{
sum = (sum + digit) % 11;
}
}
return sum;
}
static void Main( string [] args)
{
string str = "3435346456547566345436457867978" ;
Console.WriteLine(Remainder(str));
}
}
|
Javascript
function remainder(str) {
let n = str.length;
let sum = 0;
for (let i = n - 1; i >= 0; i--) {
let digit = parseInt(str[i], 10);
if ((n - i) % 2 === 0) {
sum = (sum - digit + 11) % 11;
}
else {
sum = (sum + digit) % 11;
}
}
return sum;
}
let str = "3435346456547566345436457867978" ;
console.log(remainder(str));
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
11 Apr, 2023
Like Article
Save Article