Skip to content

Instantly share code, notes, and snippets.

@laxmankumar2000
Created April 30, 2021 17:19
Show Gist options
  • Save laxmankumar2000/324230e693faac149d415f45ce6fc2fc to your computer and use it in GitHub Desktop.
Save laxmankumar2000/324230e693faac149d415f45ce6fc2fc to your computer and use it in GitHub Desktop.
New_PG
package StringProgram;
/*
Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
Difficulty Level : Hard
Last Updated : 23 Apr, 2020
Given a range [L, R] and two positive integers N and M. The task is to count the numbers in the range containing only non-zero digits whose sum of digits is equal to N and the number is divisible by M.
Examples:
Input: L = 1, R = 100, N = 8, M = 2
Output: 4
Only 8, 26, 44 and 62 are valid numbers
Input: L = 1, R = 200, N = 4, M = 11
Output: 2
Only 22 and 121 are valid numbers
*/
import java.util.ArrayList;
public class sumarray {
public static int solution(int l,int r, int m , int n)
{
ArrayList<Integer> all_number = new ArrayList<>();
for (int i = l; i < r; i++) {
all_number.add(i);
}
ArrayList<Integer> Sec_List = new ArrayList<>();
for (int i = 0; i <all_number.size() ; i++) {
int temp = all_number.get(i);
int temp2 = all_number.get(i);
int sum = 0;
while(temp > 0)
{
int a = temp%10;
sum = sum +a;
temp/=10;
}
if (m==sum && temp2%n==0){
Sec_List.add(temp2);
}
}
ArrayList<Integer> Final_List=new ArrayList<>();
for (int i = 0; i < Sec_List.size(); i++) {
int temp =Sec_List.get(i);
int temp2=Sec_List.get(i);
int sum = 0;
while (temp>0)
{
int a=temp%10;
if (a!=0)
{
sum = sum*10+a;
}
else
{
break;
}
temp/=10;
}
int temp3 = sum;
int final_sum = 0;
while(temp3>0)
{
int a = temp3%10;
final_sum=final_sum*10+a;
temp3/=10;
}
if (final_sum==0) {
break;
}
Final_List.add(final_sum);
}
// System.out.println(all_number);
// System.out.println(Sec_List);
// System.out.println();
//// System.out.println(count);
// System.out.println(Final_List);
// System.out.println(Final_List.size());
// int count = Final_List.size();
return Final_List.size();
}
public static void main(String[] args) {
int l=1;
int r=100;
int m=8;
int n=2;
System.out.println(solution(l,r,m,n));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment