0

Given a number num find out how many continuous fragments of the given number is divisible by 11 for example given 1215598 these continuous fragments could be formed:

  • 1
  • 12
  • 121
  • 1215
  • 12155
  • 121559
  • 1215598
  • 2
  • 21
  • 215
  • 2155
  • 21559
  • 215598
  • 1
  • 15
  • 155
  • 1559
  • 15598
  • 5
  • 55
  • 559
  • 5598
  • 5
  • 59
  • 598
  • 9
  • 98
  • 8

The correct answer then, would be 4, because these continuous fragments are divisible by 11:

  • 121
  • 12155
  • 15598
  • 55

I want to write a program in C++ to solve this but I am unable to figure out an efficient and suitable way.

So far I have taken a number input and stored it in an array and now I am not able to understand how I can break the number into continuous fragments and check for the divisibility of the same.

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int DivisibilityByEleven(int num);
int num;
int main()
{
   int result=DivisibilityByEleven(num);
   return 0;
}

int DivisibilityByEleven(int num)
{
    int counter=0;
    cin>>num;   
    vector<int> arr;
    while(num!=0)
    {
        int temp=num%10;
        num=num/10;
        arr.push_back(temp);
    }     
    reverse(arr.begin(),arr.end());
    for(int i=0;i<arr.size();i++)
    {
        cout<<arr[i];
    }

    if(num%11==0)
    {
        counter++;
    }
}
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
Adarsh Pandey
  • 57
  • 1
  • 1
  • 4

7 Answers7

1

You can use this code:

#include<bits/stdc++.h>

using namespace std;

int main()
{
  int n,i,j,l=0,w,m=0;
  cin>>n;
  int temp = n;
  int temp2 = n;
 
  while(n!=0)
  {
    n=n/10;
    l++;
  }
  int k = l;

  l = (l*(l+1))/2;//Total number of possible continuous fragments of number
  int arr[l];
  l=k;
  for(i=0;i<l;i++)
  {
     w=(int)pow(10,i+1);
     if(i%2!=0)
       w=w+1;
     for(j=0;j<k;j++)
     {
       arr[m++] = temp%w;
       temp = temp/10;
     }
    temp = temp2;
    k--;
  }
  sort(arr,arr+m);
  for(i=0;i<m;i++)
  {
     if(arr[i]%11==0)
     cout<<arr[i]<<" ";
  }
}
1

You can do this in java ............................

public class Eleven {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int n =1215598367,count = 0,c=0,x=0;
        int t=n;
        int j=10;
        while(t>0) {
            t/=10;
            count++;
            }
        System.out.println(count);
        int b = n;
        while(b>0) {
        while(x<count) {
                int a = b%j;
                System.out.println(a);
                if(a%11==0) {
                    c++;
                }   
                x++;
                j*=10;
        }
        b=b/10;
        x=0;
        count = count-1;
        j=10;
        }
        System.out.println(c);
    }
}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • Welcome to StackOverflow. While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. Have a look here → [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – Federico Baù Jan 26 '21 at 08:25
0

Imagine that the question was to identify divisibility by 10. That would be trivial (look for fragments ending with zero, the only complication is dealing with more than one 0 in the number.). This is what I suggest you start with.

Testing divisibility by 11 is similarly trivial if you restate the number with a radix of 11. (Again, look for fragments ending with zero.)

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

To simplify the solution I'm going to assume the next base 10 digit larger than the number is exactly representable by a double (if this isn't true, you'll have to find your own way to write pow, floor, and log10). Given this, your input is stored in num, and you're storing the count in count you really just want to find all "substrings" of your number. Which is best done by just selecting all valid combinations of begining and ending characters:

for(auto MSD = num <= 0 ? 1U : static_cast<unsigned>(pow(10.0, floor(log10(num)) + 1.0)); MSD > 1U; MSD /= 10) {
    for(auto LSD = MSD / 10U; LSD > 0U; LSD /= 10U) {
        if(num % MSD / LSD % 11 == 0U) {
            ++count;
        }
    }
}

Live Example

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
0
using System;
using System.Collections.Generic;
using System.Text;
namespace test
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            int num = 1215598;
            callme(num);
            Console.ReadLine();
        }
        public static void callme(int number)
        {
            int mynumber = number;
            List<int> arr = new List<int>();
            List<int> newarr = new List<int>();
`separating numbers `
            while (mynumber != 0)
            {
                int test = mynumber % 10;
                mynumber /= 10;
                arr.Add(test);
            }
`reverse add to another array`
            for (int i = arr.Count - 1, k = i; i >= 0; i--, k--)
            {
                int cont = 1;
                while (cont <= i + 1)
                {
                    StringBuilder str = new StringBuilder();
                    for (int j = 0; j < cont; j++)
                    {
                        str.Append(arr[k - j]);
                    }
                    newarr.Add(Convert.ToInt32(str.ToString()));
                    cont++;
                }
            }
`divide and print`
            foreach (int val in newarr)
            {
                if (val % 11 == 0)
                    Console.WriteLine(val);
            }

        }
    }
}
0

Solution in Python:

def DivisibilityByEleven(num):
    str_num = str(num)
    count = 0
    for i in range(len(str_num)):
        for j in range(len(str_num)):
            my_num = str_num[i:j+1]
            if my_num != '':
                if int(my_num) % 11 == 0:
                    count += 1
    return count

if __name__ == '__main__':
    num = int(input())
    result = DivisibilityByEleven(num)
    print(result)
0
//IN C programming
  #include <stdio.h>
    int main()
    {
        int num=1215598,c=10,l=0,n=10,a=0,d,count=0,dummy,o=0,num1;
        dummy=num;
        while(num)
        {
            num=num/10;
            a++;
        }
        num=dummy;
       while(num>0){
        while(o<a)
        {
            d=num%c;
            printf("%d\n",d);
            if(d%11==0)
            {
                //printf("%d",d);
                count++;
            }
            c*=10;
            o++;
        }
        num=num/n;
        c=10;
        a-=1;
        o=0;
       }
       printf("\n%d",count);
        return 0;
    }