Created
November 10, 2022 13:29
-
-
Save roopeshsn/db706b0ab36ad3613a625d44895cc82f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.roopesh; | |
public class Main { | |
public static void main(String[] args) { | |
int ans = closureSum(6583); | |
System.out.println(ans); | |
} | |
public static int closureSum(int val) { | |
char[] chars = ("" + val).toCharArray(); | |
int start = 0; | |
int end = chars.length - 1; | |
int ans = 0; | |
while(start < end) { | |
int temp = chars[start] + chars[end]; | |
ans += temp; | |
start++; | |
end--; | |
} | |
if(start == end) { | |
ans += chars[start]; | |
} | |
return ans; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment