Rearranging digits to get a multiple of 5 CodeChef Solution

Problem – Rearranging digits to get a multiple of 5 CodeChef Solution

Given a positive integer N, MoEngage wants you to determine if it is possible to rearrange the digits of N (in decimal representation) and obtain a multiple of 5.

For example, when N=108, we can rearrange its digits to construct 180=36⋅5 which is a multiple of 5.

Input Format

  • The first line contains an integer T, the number of test cases. The description of the T test cases follow.
  • Each test case consists of two lines
  • The first line contains a single integer D, the number of digits in N.
  • The second line consists of a string of length D, the number N (in decimal representation). It is guaranteed that the string does not contain leading zeroes and consists only of the characters 0,1,…9.

Output Format

  • For each test case, print Yes if it is possible to rearrange the digits of N so that it becomes a multiple 5. Otherwise, print No.

You may print each character of the string in uppercase or lowercase (for example, the strings YeS, yEs, yes and YES will all be treated as identical).

Constraints

  • 1≤T≤1000
  • 1≤D≤1000
  • 1≤N<10^1000
  • Sum of D over all test cases ≤1000.

Sample 1:

Input:
3
3
115
3
103
3
119
Output:
Yes
Yes
No

Explanation:

Test Case 1: The given number is already divisible by 5, therefore the answer is Yes.

Test Case 2: We can obtain 310=62⋅5 by rearranging the digits of 103, so the answer is Yes.

Test Case 3: The only numbers that can be obtained by rearranging the digits of 119 are {119,191,911}. None of these numbers are multiples of 5, so the answer is No.

Rearranging digits to get a multiple of 5 CodeChef Solution in Java

import java.util.*;
class rearrange
{
    public static void main(String[]args)
    {
       int t,n,d,c=0;
       Scanner sc=new Scanner(System.in);
       t=sc.nextInt();
       for(int i=1;i<=t;i++)
       {
           n=sc.nextInt();
          String p=sc.next();
           for(int j=0;j<n;j++)
           {
               char ch=p.charAt(j);
               if(ch=='0'||ch=='5')
               {
               c++;
               break;
               }
           }
           if(c>0)
           System.out.println("Yes");
            else
            System.out.println("No");
            c=0;
         }
       }
    }

Rearranging digits to get a multiple of 5 CodeChef Solution in C++17

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    int d;
	    cin>>d;
	    string n;
	    cin>>n;
	    int flag=0;
	   for(int i=0;i<d;i++)
	   {
	       if(n[i]=='0' || n[i]=='5')
	       {
	           cout<<"Yes"<<endl;
	           flag=1;
	           break;
	       }
	   }
	   if(flag==0)
	   {
	       cout<<"No"<<endl;
	   }
	   
	}
	return 0;
}

Rearranging digits to get a multiple of 5 CodeChef Solution in Pyth 3

a=int(input())
for i in range (a):
    b=int(input())
    lst=list(str(int(input())))
    #for i in lst:
        #if (i=="0") or (i=="5"):
    if '0' in lst or '5'in lst:
        print("yes")
    else:
        print("NO")
            
Rearranging digits to get a multiple of 5 CodeChef Solution Review:

In our experience, we suggest you solve this Rearranging digits to get a multiple of 5 CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Rearranging digits to get a multiple of 5 CodeChef Solution

Find on CodeChef

Conclusion:

I hope this Rearranging digits to get a multiple of 5 CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *