Check if sum of digits in the left half is divisible by sum of digits in the right half in the largest permutation of N
Last Updated :
01 Apr, 2022
Given a positive integer N, the task is to maximize the integer N by rearranging the digits and check if the sum of the left half digits is divisible by the sum of the right half digits or not. If found to be true, then print “Yes”. Otherwise, print “No”.
If the number of digits(say D) in the given number N is odd, then consider any of the two possibilities:
- Consider the first (D/2) digit in the left half.
- Consider the first (D/2 + 1) digit in the left half.
Examples:
Input: N = 43729
Output: Yes
Explanation:
Maximum value of N possible by rearranging the digits is 97432.
Case 1:
Sum of digits in the left half = 9 + 7 = 16.
Sum of digits in the right half = 4 + 3 + 2 = 9.
Since, 16 is not divisible by 9, the condition is not satisfied.
Case 2:
Sum of digits in the left half = 9 + 7 + 4 = 20.
Sum of digits in the right half = 3 + 2 = 5.
Since, 20 is divisible by 5, the condition is satisfied.
Input: N = 3746
Output: No
Approach: The given problem can be solved by sorting the given digits of the number N in descending order maximizing the value of N and then checking for the divisibility of the sum of the left and the right half digits. Follow the steps below to solve the problem:
- Initialize a variable, say D that stores the count of digits of the given number N.
- Store the given number as the string in a variable, say temp.
- Sort the string temp in decreasing order.
- Now check if the sum of the first (D/2) digits is divisible by the sum of the last (D/2) digits, then print “Yes”. Otherwise, print “No”.
Below is the implementation of the above approach:
C++
#include "bits/stdc++.h"
using namespace std;
void checkDivisible( int N)
{
string temp = to_string(N);
int D = temp.length();
sort(temp.begin(), temp.end(),
greater< char >());
int leftSum = 0, rightSum = 0;
for ( int i = 0; temp[i]; i++) {
rightSum += (temp[i] - '0' );
}
for ( int i = 0; i < D / 2; i++) {
leftSum += (temp[i] - '0' );
rightSum -= (temp[i] - '0' );
}
if (D & 1) {
if (leftSum % rightSum == 0) {
cout << "Yes" ;
}
else {
leftSum += (temp[D / 2] - '0' );
rightSum -= (temp[D / 2] - '0' );
if (leftSum % rightSum == 0) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
}
}
else {
if (leftSum % rightSum == 0) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
}
}
int main()
{
int N = 43729;
checkDivisible(N);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static void descOrder( char [] s)
{
Arrays.sort(s);
reverse(s);
}
static void reverse( char [] a)
{
int i, n = a.length;
char t;
for (i = 0 ; i < n / 2 ; i++)
{
t = a[i];
a[i] = a[n - i - 1 ];
a[n - i - 1 ] = t;
}
}
static void checkDivisible( int N)
{
String temp = Integer.toString(N);
char []arr = temp.toCharArray();
int D = arr.length;
descOrder(arr);
int leftSum = 0 , rightSum = 0 ;
for ( int i = 0 ; i < D; i++) {
rightSum += (arr[i] - '0' );
}
for ( int i = 0 ; i < D / 2 ; i++) {
leftSum += (arr[i] - '0' );
rightSum -= (arr[i] - '0' );
}
if (D% 2 == 1 ) {
if (leftSum % rightSum == 0 ) {
System.out.print( "Yes" );
}
else {
leftSum += (arr[D / 2 ] - '0' );
rightSum -= (arr[D / 2 ] - '0' );
if (leftSum % rightSum == 0 ) {
System.out.print( "Yes" );
}
else {
System.out.print( "No" );
}
}
}
else {
if (leftSum % rightSum == 0 ) {
System.out.print( "Yes" );
}
else {
System.out.print( "No" );
}
}
}
public static void main(String args[])
{
int N = 43729 ;
checkDivisible(N);
}
}
|
Python3
def checkDivisible(N):
temp = str (N)
D = len (temp)
temp = ''.join( sorted (temp, reverse = True ))
leftSum,rightSum = 0 , 0
for i in range (D):
rightSum + = ord (temp[i]) - ord ( '0' )
for i in range (D / / 2 ):
leftSum + = ord (temp[i]) - ord ( '0' )
rightSum - = ord (temp[i]) - ord ( '0' )
if (D % 2 = = 1 ):
if (leftSum % rightSum = = 0 ):
print ( "Yes" )
else :
leftSum + = ord (temp[D / / 2 ]) - ord ( '0' )
rightSum - = ord (temp[D / / 2 ]) - ord ( '0' )
if (leftSum % rightSum = = 0 ):
print ( "Yes" )
else :
print ( "No" )
else :
if (leftSum % rightSum = = 0 ):
print ( "Yes" )
else :
print ( "No" )
N = 43729
checkDivisible(N)
|
C#
using System;
using System.Collections;
public class GFG
{
static void descOrder( char []s)
{
Array.Sort(s);
reverse(s);
}
static void reverse( char []a)
{
int i, n = a.Length;
char t;
for (i = 0; i < n / 2; i++)
{
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
}
static void checkDivisible( int N)
{
String temp = N.ToString();
char []arr = temp.ToCharArray();
int D = arr.Length;
descOrder(arr);
int leftSum = 0, rightSum = 0;
for ( int i = 0; i < D; i++) {
rightSum += (arr[i] - '0' );
}
for ( int i = 0; i < D / 2; i++) {
leftSum += (arr[i] - '0' );
rightSum -= (arr[i] - '0' );
}
if (D%2 == 1) {
if (leftSum % rightSum == 0) {
Console.Write( "Yes" );
}
else {
leftSum += (arr[D / 2] - '0' );
rightSum -= (arr[D / 2] - '0' );
if (leftSum % rightSum == 0) {
Console.Write( "Yes" );
}
else {
Console.Write( "No" );
}
}
}
else {
if (leftSum % rightSum == 0) {
Console.Write( "Yes" );
}
else {
Console.Write( "No" );
}
}
}
public static void Main()
{
int N = 43729;
checkDivisible(N);
}
}
|
Javascript
<script>
function checkDivisible(N)
{
let temp = N.toString();
let D = temp.length;
temp.sort().reverse();
let leftSum = 0, rightSum = 0;
for (let i = 0; i < D; i++) {
rightSum += (temp[i] - '0' );
}
for (let i = 0; i < D / 2; i++) {
leftSum += (temp[i] - '0' );
rightSum -= (temp[i] - '0' );
}
if (D % 2 == 1) {
if (leftSum % rightSum == 0) {
document.write( "Yes" );
}
else {
leftSum += (temp[D / 2] - '0' );
rightSum -= (temp[D / 2] - '0' );
if (leftSum % rightSum == 0) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
}
}
else {
if (leftSum % rightSum == 0) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
}
}
let N = 43729;
checkDivisible(N);
</script>
|
Time Complexity: O(log10N)
Auxiliary Space: O(log10N)
Share your thoughts in the comments
Please Login to comment...