United Lex Coding Questions and Answers
United Lex Coding Questions with Solutions
Here, in United Lex Coding Questions and Answers page we have given Sample United Lex Coding Questions with Solutions that will help you for preparing for United Lex Recruitment Drive.
Go through this page to get all Sample United Lex Coding Questions and at last get FAQ’s related to United Lex Recruitment Drive.
United Lex Coding Questions and Answers - Set 1
Question 1: Password ASCII
Problem statement : Aman, who is working at a software company forgot the password of his Linkedin id.But he knows the ASCII values of his password in reverse order. Help aman to find the password.
To decode the password, first reverse the string of digits, then successively pick valid values from the string and convert them to their ASCII equivalents. Some of the values will have two digits, and others three. Use the ranges of valid values when decoding the string of digits.
Some of the ASCII values are given with their characters:
- The ASCII value of A to Z is 65 to 90.
- The ASCII value of a to z is 97 to 122.
- The ASCII value of space characters is 32.
Note: The password only has alphabets and blank spaces.
Given a string , decode the password by following the steps mentioned above.
Constraints:
- 1<= |s| <=10^5
- s[i] is an ascii character in the range [A-Za-z] or a space character
Sample Input:
- 796115110113721110141108
Sample Output:
- PrepInsta
Explanation
- The reversed string will be 801141011127311011511697, which if analysed as ascii will be “PrepInsta”
Solution:
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
#include<bits/stdc++.h> using namespace std; string s1,s; int n; void fun(int i){ if(i>=s.length()) return; string s2=s.substr(i,2); int j=stoi(s2); if(j==32) s1+=" "; else if((j>=65&&j<=91)||(j>=97&&j<=99)) s1+=char(j); else { s2=s.substr(i,3);s1+=char(stoi(s2)); fun(i+3);return; } fun(i+2); } int main(){ s1="";cin>>s; reverse(s.begin(),s.end()); fun(0); cout<<s1; }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); String str = sc.next(); char arr[] = str.toCharArray(); String current = ""; String result = ""; for(int i=arr.length-1;i>0;i=i-2){ current = ""; current = "" + arr[i] + arr[i-1]; int n = Integer.parseInt(current); if(n == 32) result +=" "; else if((n>=65 && n<=90 )|| (n>=97 && n<=99)) result += (char)n; else{ if(i-2<0) break; current += arr[i-2]; n=Integer.parseInt(current); result +=(char)n; i--; } } System.out.println(result); } }
s = input() s = s[::-1] i = 0 res = "" while(i<len(s)-1): x = s[i]+s[i+1] if x == "32": res += " " elif int(x) in range(65,91) or int(x) in range(97,100): res += chr(int(x)) elif i+2<len(s): x = x+s[i+2] res += chr(int(x)) i += 1 i += 2 print(res)
Question 2 : Game Of Clicks (R->Hard)
Problem Statement :
Sahil watches TV all day and gets bored. He started playing this dumb game of identifying minimum number of inputs needed to reach a channel. As his cousin, you have to help him, but you live far from his house. So you decide to write a code that will ask Sahil for some inputs and give outputs respectively.
Here are the problems you need to keep in mind :
- There are 13 buttons on his remote: 10 buttons for the numbers (0-9) to form integers denoting respective channel index, “Up channel” button and “ Down channel” button for going i +1th channel and i-1th channel from i respectively, and a “Last viewed” button to see what’s the last channel before it.
- The number buttons allow you to jump directly to a specific channel (Ex: to go to channel 172 by typing 1,7,2).
- If the channel which you are in is ith and that is the max channel index possible, by Up channel, you will reach the first channel possible. Same goes for the down channel button. You can go to the highest channel possible if you go down from the lowest channel possible.
- Sahil can get from one channel to the next in one of the two ways.
- Sahil’s parents have set some parental control on some channels on Aniruth’s television. The “Up Channel “ and “Down buttons” buttons skip these channels as these channels are not viewable.
- Given a list of channels to view, the lowest channel, the highest channel, and a list of blocked channels, your program should return the minimum number of clicks necessary to get through all the shows that Anirudh would like to match.
Input Format :
- First line is the lowest Channel
- Second-line is the highest Channel
- Followed by a number of blocked channels B,
and the next B lines contain the actual blocked channels. - Followed by the number of Channels to view V, and the next V lines contain the actual channels to view.
Constraints :
- The lowest channel on the television will be greater than 0. and less than or equal to 10,000.
- The highest channel on the television will be greater than or equal to the lowest channel. and less than or equal to 10.000.
- The list of channels that are blocked on Anirudh’s television. All the channels in this list will be valid channels (greater than or equal to lowest channel, less than or equal 1 to highest channel). Duplicates may be Ignored. The blocked list can be a maximum of 40 channels.
- The sequence that Sahil must view contains between 1 and 50 elements. inclusive. All channels in this sequence are not in the blocked list and are between lowest channel and highest channel. Inclusive.
Sample Input 0:
1
20
2
18
19
5
15
14
17
1T
17
Sample output 0:
7
#include<bits/stdc++.h> using namespace std; unordered_map < int, int > m; int l, u; int util(int a, int b) { if (a == b) return 0; if (m[a]) return util(a + 1, b); return 1 + util(a + 1, b); } int func(int b, int prev) { if (b < prev) return min(util(prev, u) + util(l, b) + 1, util(b, prev)); else return min(util(prev, b), util(l, b) + util(prev, u) + 1); } int main() { int flag = 0, ans = 0, prev, prev2; cin >> l >> u; int bn, b; cin >> bn; while (bn--) { cin >> b; m[b]++; } cin >> bn; while (bn--) { cin >> b; if (b > 9 && flag == 0) { ans += 2; flag++; prev = b; } else if (flag == 0) { ans += 1; flag++; prev = b; } else if (prev2 == b) { prev2 = prev; prev = b; ans++; } else { ans += min(b > 9 ? 2 : 1, func(prev, b)); prev2 = prev; prev = b; } } cout << ans; }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int l = sc.nextInt(); int h = sc.nextInt(); int b = sc.nextInt(); List < Integer > bl = new ArrayList < > (); for (int i = 0; i < b; i++) { bl.add(sc.nextInt()); } int v = sc.nextInt(); List < Integer > vl = new ArrayList < > (); for (int i = 0; i < v; i++) { vl.add(sc.nextInt()); } Set < Integer > sl = new HashSet < > (); int res = 0; for (Integer i: vl) { if (bl.contains(i)) continue; sl.add(i); } for (Integer i: sl) { String s = i + ""; res += s.length(); } System.out.println(res); } }
def prev(now, l, h, blocked): if now != l: if (now - 1) not in blocked: return now - 1 else: return prev(now - 1, l, h, blocked) else: if h not in blocked: return h else: return prev(h, l, h, blocked) def next(now, l, h, blocked): if now != h: if (now + 1) not in blocked: return now + 1 else: return next(now + 1, l, h, blocked) else: if l not in blocked: return l else: return next(l, l, h, blocked) def digits(n): count = 0 while n > 0: n = n // 10 count += 1 return count for i in range(2): if i == 0: l = int(input()) else: h = int(input()) b = int(input()) blocked = [] for i in range(b): blocked.append(int(input())) back = -1 now = -1 c = int(input()) k = 0 for i in range(c): n = int(input()) n1 = digits(n) if now == -1: now = n k += n1 continue if back == n: k += 1 back, now = now, back continue pf = 0 pb = 0 now1 = now prev1 = now for j in range(n1): if j == (n1 - 1): pf = n1 pb = n1 break else: now1 = next(now1, l, h, blocked) pf += 1 prev1 = prev(prev1, l, h, blocked) pb += 1 if now1 == n: break if prev1 == n: break k += pf back = now now = n print(k)
Question 3: Formatting large Products
Problem Statement: Rohan is weak in mathematics.He is giving mathematics Olympiad , but he got stuck in one of the question .Help rohan to solve the question.In Question there are two positive integer A and B. You have to find the product of all integer between A and B which is represented in the form C=D*10^E , where C is the product of numbers , D and E are non-negative integers and the last digit of D is non-zero.
Function Description
- Complete the function formatProducts in the editor below, formatProduct must return a string that represents C in the above described form.
- Function has the following parameters
- A: an integer
- B: an integer
Constraints
- A will between 1 and 1,000,000 . Inclusive.
- B will be between A and 1,000,000. Inclusive.
Sample Input 0
- 1
- 5
Sample Output 0
- 12 * 10^1
Explanation
- 1*2*3*4*5=120 = 12 * 10^1
Sample Input 1
- 3
- 10
Sample Output 1
- 18144 * 10^2
Explanation
- 3*4*….*10=1814400 =18144 * 10^2
#include<bits/stdc++.h> using namespace std; string formatProducts(int a, int b) { int result = 1; for (int i = a; i <= b; i++) result = result * i; int temp = result; int power = 0; while ((result % 10) == 0) { power = power + 1; result = result / 10; } return to_string(result) + " * 10^" + to_string(power); } int main() { int a, b; cin >> a >> b; cout << formatProducts(a, b) << endl; return 0; }
import java.util.*; public class Main { public static String formatProducts (int a, int b) { int result = 1; for (int i = a; i <= b; i++) result = result * i; int temp = result; int power = 0; while ((result % 10) == 0) { power = power + 1; result = result / 10; } return result + " * 10^" + power; } public static void main (String[]args) { Scanner sc = new Scanner (System.in); int a = sc.nextInt (); int b = sc.nextInt (); System.out.println (formatProducts (a, b)); } }
def formatProducts(L,H): a=1 for i in range(L,H+1): a=a*i c=0 while(True): if a%10!=0: break else: c+=1 a=a//10 return (str(a)+" * 10^"+str(c)) L=int(input()) H=int(input()) print(formatProducts(L,H))
Question 4: Queries for Count
The task is to determine the number of elements within a specified range in an unsorted array. Given an array of size n, the goal is to count the elements that fall between two given values, i and j, inclusively.
Examples:
Input:
Array: [1, 3, 3, 9, 10, 4]
Range 1: i = 1, j = 4
Range 2: i = 9, j = 12
Output:
For Range 1: 4
For Range 2: 2
Explanation:
In the first query, the numbers within the range 1 to 4 are 1, 3, 3, and 4.
In the second query, the numbers within the range 9 to 12 are 9 and 10.
#include<bits/stdc++.h> using namespace std; int findLowerIndex(int arr[], int n, int x) { int low = 0, high = n - 1; while (low <= high) { int mid = (low + high) / 2; if (arr[mid] >= x) high = mid - 1; else low = mid + 1; } return low; } int findUpperIndex(int arr[], int n, int y) { int low = 0, high = n - 1; while (low <= high) { int mid = (low + high) / 2; if (arr[mid] <= y) low = mid + 1; else high = mid - 1; } return high; } int countElementsInRange(int arr[], int n, int x, int y) { int count = 0; count = findUpperIndex(arr, n, y) - findLowerIndex(arr, n, x) + 1; return count; } int main() { int arr[] = {1, 4, 4, 9, 10, 3}; int n = sizeof(arr) / sizeof(arr[0]); sort(arr, arr + n); int lowerRange = 1, upperRange = 4; cout << countElementsInRange(arr, n, lowerRange, upperRange) << endl; lowerRange = 9; upperRange = 12; cout << countElementsInRange(arr, n, lowerRange, upperRange) << endl; return 0; }
def find_lower_index(arr, x): low, high = 0, len(arr) - 1 while low <= high: mid = (low + high) // 2 if arr[mid] >= x: high = mid - 1 else: low = mid + 1 return low def find_upper_index(arr, y): low, high = 0, len(arr) - 1 while low <= high: mid = (low + high) // 2 if arr[mid] <= y: low = mid + 1 else: high = mid - 1 return high def count_elements_in_range(arr, x, y): arr.sort() lower_index = find_lower_index(arr, x) upper_index = find_upper_index(arr, y) count = upper_index - lower_index + 1 return count arr = [1, 4, 4, 9, 10, 3] lower_range = 1 upper_range = 4 print(count_elements_in_range(arr, lower_range, upper_range)) lower_range = 9 upper_range = 12 print(count_elements_in_range(arr, lower_range, upper_range))
import java.util.Arrays; class Main { static int findLowerIndex(int[] arr, int n, int x) { int low = 0, high = n - 1; while (low <= high) { int mid = (low + high) / 2; if (arr[mid] >= x) high = mid - 1; else low = mid + 1; } return low; } static int findUpperIndex(int[] arr, int n, int y) { int low = 0, high = n - 1; while (low <= high) { int mid = (low + high) / 2; if (arr[mid] <= y) low = mid + 1; else high = mid - 1; } return high; } static int countElementsInRange(int[] arr, int n, int x, int y) { int count = 0; count = findUpperIndex(arr, n, y) - findLowerIndex(arr, n, x) + 1; return count; } public static void main(String[] args) { int[] arr = {1, 4, 4, 9, 10, 3}; int n = arr.length; Arrays.sort(arr); int lowerRange = 1, upperRange = 4; System.out.println(countElementsInRange(arr, n, lowerRange, upperRange)); lowerRange = 9; upperRange = 12; System.out.println(countElementsInRange(arr, n, lowerRange, upperRange)); } }
Question 5: Password Hacker
Problem statement :
Elliot made a KeyLogger for his friend Romero, so that he can see the passwords of his friend. Keylogger is a software that can tell you the buttons pressed in the keyboard without the consent of the user, and hence unethical. Elliot made it to hack Romero’s passwords. The one problem is, Romero writes the passwords in lowercase characters only, and the keylogger only takes the values of the keys. Like, for a it takes 1, for b 2, and for z 26. For a given number Elliot produces all combinations of passwords in a dictionary and starts a dictionary based password attack. For a given number, print all the possible passwords in a lexicographic order.
Input Format:
One line, denoting the value given by the keylogger
Output Format:
All possible combinations of keyloggers in new lines are lexicographically ordered.
Constraints:
2<=Number of digit in input<=1000
Sample Input:
1234
Sample Output:
abcd
axd
Mcd
Explanation:
For 12, you can take 1,2 that is ab, or you can take l.
#include<bits/stdc++.h> using namespace std; string ss; vector < string > v; void fun(int i, string s) { //cout<< i<< s<< endl; if (i == ss.length()) { v.push_back(s); return; } if (i > ss.length()) return; if (ss[i] == '0') return; char c = 'a' + (ss[i] - '1'); fun(i + 1, s + c); if (i != s.length() - 1) if (ss[i] < '3' && ss[i + 1] < '7') { int a = (ss[i] - '1' + 1), b = (ss[i + 1] - '0'); c = ('a' + (10 * a + b - 1)); //cout<< a<< b<< endl; fun(i + 2, s + c); } } int main() { cin >> ss; fun(0, ""); sort(v.begin(), v.end()); for (auto i: v) cout << i << endl; }
import java.util.*; public class Main { static String ss; static ArrayList v = new ArrayList<>(); public static void fun(int i, String s) { if (i == ss.length()) { v.add(s); return; } if (i > ss.length()) return; if (ss.charAt(i) == '0') return; char c = (char)('a' + (ss.charAt(i) - '1')); fun(i + 1, s + c); if (i != ss.length() - 1) { if (ss.charAt(i) < '3' && ss.charAt(i + 1) < '7') { int a = (ss.charAt(i) - '1' + 1); int b = (ss.charAt(i + 1) - '0'); c = (char)('a' + (10 * a + b - 1)); fun(i + 2, s + c); } } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); ss = sc.next(); fun(0, ""); Collections.sort(v); for (String i : v) { System.out.println(i); } } }
def fun(i, s): global v if i == len(ss): v.append(s) return if i > len(ss): return if ss[i] == '0': return c = chr(ord('a') + int(ss[i]) - 1) fun(i + 1, s + c) if i != len(ss) - 1: if ss[i] < '3' and ss[i + 1] < '7': a = int(ss[i]) - 1 + 1 b = int(ss[i + 1]) c = chr(ord('a') + (10 * a + b - 1)) fun(i + 2, s + c) ss = input() v = [] fun(0, "") v.sort() for i in v: print(i)
United Lex Coding Questions and Answers - Set 2
Question 6 :A Good Prime Number
Problem Statement :
A prime number is a number which is divisible by one and itself. Also a number is called a good prime number if the sum of its digits is a prime number. For example a number 23 is a good prime number because the sum of 2 and 3 ( 2+3=5) is 5 which is a prime number. You are given an integer K. Your task is to find the kth good prime number that is greater than a provided number N.
For example , 232 is a good prime number since the sum of all digits is 7 which is a prime number whereas 235 is not a good prime number.
Input format :
- The first line contains an integer N.
- The next line contains an integer K.
Output format :
A single integer which is a Kth good prime number that is greater than a provided number N.
Constraints :
- 1<=N<=10^5
- 1<=K<<=10^5
Sample Input 1:
4 4
Sample Output 1:
12
Explanation :
Good prime numbers starting from 4 are 5,7,11(1+1=2 which is prime number),12(1+2=3 which is prime number),14(1+4=5 which is a prime number) and so on. Because the sum of digits of an individual number is a prime number And 4 th good prime number is 12 in this series.Hence the output is 12.
Sample Input 2:
17 5
Sample Output 2:
29
Explanation :
Good prime numbers starting from 17 are 20,21,23,25,29…and the 5th prime number is 29.Hence the output is 29.
#include #include<bits/stdc++.h> using namespace std; bool isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if ((n % i == 0) || (n % (i + 2) == 0)) return false; return true; } int solve(int n, int k) { int c = 0; vector < int > list; for (int i = n + 1; c < k; i++) { int temp = i; int sum = 0; while (temp != 0) { sum = sum + temp % 10; temp = temp / 10; } if (isPrime(sum)) { list.push_back(i); c++; } } return list[k - 1]; } int main() { int n; cin >> n; int k; cin >> k; cout << solve(n, k); return 0; }
import java.util.*; class Main { public static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if ((n % i == 0) || (n % (i + 2) == 0)) return false; return true; } public static int solve(int n, int k) { int c = 0; ArrayList < Integer > list = new ArrayList < > (); for (int i = n + 1; c < k; i++) { int temp = i; int sum = 0; while (temp != 0) { sum = sum + temp % 10; temp = temp / 10; } if (isPrime(sum)) { list.add(i); c++; } } return list.get(k - 1); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); System.out.println(solve(n, k)); } }
import math def isPrime(n): if n <= 1: return False if n <= 3: return True if n % 2 == 0 or n % 3 == 0: return False for i in range(5, int(math.sqrt(n)) + 1, 6): if n % i == 0 or n % (i + 2) == 0: return False return True def solve(n, k): c = 0 list = [] for i in range(n + 1, 1000000000): temp = i sum = 0 while temp != 0: sum = sum + temp % 10 temp = temp // 10 if isPrime(sum): list.append(i) c += 1 if c == k: break return list[k - 1] n, k = map(int, input().split()) print(solve(n, k))
Question 7 : Loki’s Mind Stone
Problem Statement :
Loki, the God of mischief can brainwash any living person by touching him/her with his Mind stone, and has decided to break the avengers (a warrior group) to face into each other, so that they can turn against each other and make Loki’s evil plans easier. Now all the avengers have some amount of strength that is denoted in integers. Loki wants to brainwash the least amount of people possible, because he is lazy. But he wants his team of avengers to win the battle. What is the number of avengers Loki will get brainwashed.
Input Format:
First line contains an integer n, denoting the number of total avengers
the next line contains n space separated integers denoting the power of each avenger.
Output Format:
One line denoting the total number of avengers brainwashed by Loki’s Mind stone.
Constraints:
2<=n<=10^6
Test case:
Sample Input:
6
9 3 1 2 4 2
Sample Output:
2
Output Specifications:
Loki can brainwash the avengers with power 9 and 3, or with 9 and 2, or with 9,4, and the rest will be losing cause cumulative power of rest avengers is less than the brainwashed total power by Loki.
#include <bits/stdc++.h> using namespace std; map < int, int > m; int main() { int n, sum = 0, sum2 = 0, ans = 0; cin >> n; vector < int > v(n); for (int i = 0; i < n; i++) { cin >> v[i]; sum += v[i]; } sort(v.begin(), v.end(), greater < int > ()); sum /= 2; while (sum2 <= sum && ans < n) { sum2 += v[ans]; ans++; } cout << ans; }
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int arr[] = new int[n]; int sum = 0; for (int i = 0; i < arr.length; i++) { arr[i] = sc.nextInt(); sum = sum + arr[i]; } Arrays.sort(arr); int sum1 = 0, count = 0; for (int i = arr.length - 1; i >= 0; i--) { sum1 = sum1 + arr[i]; sum = sum - arr[i]; count++; if (sum1 > sum) break; } System.out.println(count); } }
n = int(input()) arr = list(map(int, input().split())) s = sum(arr) arr.sort(reverse=True) dup, sum1, ans = 0, 0, 0 for i in arr: dup += i sum1 = s - dup ans += 1 if sum1 < dup: break print(ans)
Question 8 : Spiral Matrix
Problem Statement :
You will be given a 2d matrix. Write the code to traverse the matrix in a spiral format. Check the input and output for better understanding.
Sample Input :
Input :
5 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
Output :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <bits/stdc++.h> using namespace std; void Spiral(vector < vector < int >> a) { if (a.size() == 0) return; int m = a.size(), n = a[0].size(); int i, k = 0, l = 0; while (k < m && l < n) { for (i = l; i < n; ++i) cout << a[k][i] << " "; k++; for (i = k; i < m; ++i) cout << a[i][n - 1] << " "; n--; if (k < m) { for (i = n - 1; i >= l; --i) cout << a[m - 1][i] << " "; m--; } if (l < n) { for (i = m - 1; i >= k; --i) cout << a[i][l] << " "; l++; } } } int main() { int r, c; cin >> r >> c; vector < vector < int >> mat(r, vector < int > (c)); for (int i = 0; i < r; i++) for (int j = 0; j < c; j++) cin >> mat[i][j]; Spiral(mat); }
import java.util.*; public class Main { public static List < Integer > solve(int [][]matrix,int row,int col) { List < Integer > res=new ArrayList < Integer > (); boolean[][] temp=new boolean[row][col]; int []arr1={0,1,0,-1}; int []arr2={1,0,-1,0}; int di=0,r=0,c=0; for(int i=0;i < row*col; i++) { res.add(matrix[r][c]); temp[r][c]=true; int count1=r+arr1[di]; int count2=c+arr2[di]; if(count1 >= 0 && row > count1 && count2 >= 0 && col > count2 && !temp[count1][count2]){ r=count1; c=count2; } else { di=(di+1)%4; r+=arr1[di]; c+=arr2[di]; } } return res; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int m=sc.nextInt(); int n=sc.nextInt(); int matrix[][]=new int[m][n]; for(int i=0;i < m;i++) { for(int j=0;j < n;j++) matrix[i][j]=sc.nextInt(); } System.out.println(solve(matrix,m,n)); } }
def spiralOrder(arr): ans = [] while arr: ans += arr.pop(0) arr = (list(zip(*arr)))[::-1] return ans arr = [] n, m = map(int, input().split()) for i in range(n): arr.append(list(map(int, input().split()))) print(*spiralOrder(arr))
Question 9 : Coin Game
Raman was playing a game, he starts with x coins. Now in every step, he wins and loses and he has to get the money or pay the money as needed. He came in contact with a psychic who can see the future and the Psychic predicted the outcomes after each step. Now Raman wants to start the game with the minimum wage where he doesn’t run out of money. Help Raman to find what money he should start with. The only rule to keep playing is not going in a credit situation.
Input Format:
- First line with n, number of steps in the game
- Next n lines, n integers denoting outcomes of every game. Positive means winning and negative means losing that money.
Output Format:
- One single integer denoting the minimum amount to start with
Constraints:
- Number of steps<=10^9
- -1000<=Money needed in each step<=1000
Sample Input:
4
2
-9
15
2
Sample Output:
7
Explanation:
If he starts with 7 rupees, then after steps : 7 ->9 -> 0-> 15 -> 17.
#include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; int a[n]; for(int i=0;i<n;i++) cin>>a[i]; int sum=0,ans=0; for(int i=0;i<n;i++) { sum+=a[i]; if(sum<1) { sum=-sum; ans+=sum; sum=0; } } cout<<ans<<endl; }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) arr[i]=sc.nextInt(); int sum=0,ans=0; for(int i=0;i<n;i++) { sum+=arr[i]; if(sum<1) { sum=-sum; ans+=sum; sum=0; } } System.out.println(ans); } }
n=int(input()) arr=[] for i in range(n): arr.append(int(input())) s,a=0,0 for i in arr: s=s+i if(s<1): a=a+(-1*s) s=0 print(a)
Question 10 : Profit balance
Problem Statement :
Anand and Brijesh got a bunch of profit in their business. Now it’s time to divide the profit between themselves. Anand being the one with the calculator was the one who could decide who will get how much. But as he was one hell of an honest guy, he can’t cheat on Brijesh to get some extra money. So he decided to divide them in such a way where they can get almost the same amount possible. Although, it is sometimes impossible to divide the profit into two, because the profits must be written in their production managed copy in a way where you cannot share the profit of a single thing, A single Profit will be held by a single person.
you are the one who is called to mitigate the problem. Given an array of profit, find out in the process, what can be the minimum difference between them in terms of income.
Constraints:
- 1<=N<=10^3
- 0<=Profits<=100
Input Format:
- First line contains an integer N, number of profits.
- Next line, N space separated Integers denoting the i-th Profit.
Output:
- A single integer denoting minimum possible difference between the total profits.
Sample Input:
4
1 2 3 4
Output:
0
Explanation:
He will take 1 and 4, so his profit will be 5, so the difference will be 0.
#include<bits/stdc++.h> using namespace std; vector < int > arr; int n, s; map < int, map < int, int >> m; int MinDif(int i, int ss) { if (m[i][ss]) return m[i][ss]; if (i == n) return m[i][ss] = abs(s - 2 * ss); if (ss >= s / 2) return m[i][ss] = abs(s - 2 * ss); return m[i][ss] = min(MinDif(i + 1, ss + arr[i]), MinDif(i + 1, ss)); } int main() { cin >> n; int a; s = 0; for (int i = 0; i < n; i++) { cin >> a; arr.push_back(a); s += a; } cout << MinDif(0, 0); }
import java.util.HashMap; import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int s = 0; int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); s += arr[i]; } HashMap < Integer, HashMap < Integer, Integer >> m = new HashMap < > (); System.out.println(minDiff(0, 0, s, n, m, arr)); } private static int minDiff(int i, int j, int s, int n, HashMap < Integer, HashMap < Integer, Integer >> m, int[] arr) { if (m.containsKey(i)) { if (m.get(i).containsKey(j)) return m.get(i).get(j); } if (i == n) { HashMap < Integer, Integer > z = new HashMap < > (); z.put(j, Math.abs(s - 2 * j)); m.put(i, z); return m.get(i).get(j); } if (j >= s / 2) { HashMap < Integer, Integer > z = new HashMap < > (); z.put(j, Math.abs(s - 2 * j)); m.put(i, z); return m.get(i).get(j); } int temp = Math.min(minDiff(i + 1, j + arr[i], s, n, m, arr), minDiff(i + 1, j, s, n, m, arr)); HashMap < Integer, Integer > z = new HashMap < > (); z.put(j, temp); m.put(i, z); return m.get(i).get(j); } }
from collections import defaultdict m = defaultdict(lambda: defaultdict(int)) def MinDif(i, ss, a, n, m): global s if m[i][ss] != 0: return m[i][ss] if i == n: m[i][ss] = abs(s - (2 * ss)) return m[i][ss] if ss >= s // 2: m[i][ss] = abs(s - (2 * ss)) return m[i][ss] m[i][ss] = min(MinDif(i + 1, ss + a[i], a, n, m), MinDif(i + 1, ss, a, n, m)) return m[i][ss] n = int(input()) a = list(map(int, input().split())) s = sum(a) print(MinDif(0, 0, a, n, m))
FAQs related to United Lex Coding Questions and Answers
Question 1: What does United Lex do?
UnitedLex is a company specializing in data and professional services. They focus on delivering results that add value to top-performing law firms and corporate legal departments. Their expertise lies in various domains, including litigation and investigations, intellectual property, contracts, compliance, and legal operations.
Question 2: Does United Lex includes Coding Questions in Hiring Process?
Yes, United Lex includes Coding Questions in their Technical Interview and Online Assessments along with Aptitude Questions in their Recruitment Drive for hiring freshers.
Question 3: What is the Eligibility Criteria for United Lex Recruitment Process for Freshers ?
- B.Tech in Any Stream
- Eligible Batch : 2024
- Minimum 7 CGPA or Equivalent Percentage is required for applying
- No active backlogs
Question 4: What are the Job Roles offered by United Lex ?
Here are the following Job Roles Offered by United Lex:
- SDE Intern
- Associate Software Engineer
Question 5: What is the selection process of United Lex ?
The United Lex Recruitment Process includes:
- Online Assessment
- Technical Interviews
- HR Interviews
Question 6: What is the Salary offered by United Lex?
- For Associate Level Post: ₹ 7 LPA
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others