Hot Topics
Interview Questions
1. TCS Technical Questions
2. CTS Technical Questions
3. Infosys Technical Questions
4. Capgemini Technical Questions
5. IBM Technical Questions
6. Wipro Technical Questions
7. Accenture Technical Questions
8. Deloitte Technical Questions
9. MindTree Technical Questions
10. NTT Data Technical Questions
11. Persistent Technical Questions
12. Hexaware Technical Questions
13. ZOHO Technical Questions
14. HCL Technical Questions
15. Genpact Technical Questions
16. udostres Technical Questions
17. samsung research Technical Questions
18. Media.net Technical Questions
19. Tejas Network Technical Questions
20. Kantar Technical Questions
21. Itron Technical Questions
22. Fractal Analyst Technical Questions
23. Lexmark International Technical Questions
24. Infeneon Technologies Technical Questions
25. Razorpay Technical Questions
26. payu Technical Questions
27. Navi Technical Questions
28. Tata Cliq Technical Questions
29. Grab Technical Questions
30. Schlumberger Technical Questions
31. Latenview Analytics Technical Questions
32. GreayB Technical Questions
33. Tally solution Technical Questions
34. Amagi Technical Questions
35. HFCL Technical Questions
36. Slice Technical Questions
37. Larsen & Turbo Technical Questions
38. Zenser Technical Questions
39. Siemens Technical Questions
40. Exon Movie Technical Questions
41. Gupshup Technical Questions
42. Smart cube Technical Questions
43. Celebal Tech Technical Questions
44. Teg Analytics Technical Questions
45. Commvault Systems Technical Questions
46. Incuture Technical Questions
47. Tiger Technical Questions
48. Bridgei2i Technical Questions
49. code nation Technical Questions
50. DE Show Technical Questions
51. Amazon Technical Questions
52. Flipkart Technical Questions
53. Morgan Stanly Technical Questions
54. duutsche bank Technical Questions
55. textas Technical Questions
56. pwc Technical Questions
57. salesforce Technical Questions
58. optum Technical Questions
59. Nvidia Technical Questions
60. Itc Infotech Technical Questions
61. linkedin Technical Questions
62. Adobe Technical Questions
Nagarro Question Solved | The Card Game | Codewindow.in
- Question 1
One day, Fred and his N friends were playing a card game in which each player throws a cardwith a number written on it.
The cards are such that a number X is written on front of the card, and the negative of that number is written on the back side of the card. This game has the following rules:
Each of the N players is asked to throw a card.After at the N vards are thrown. Fred has to flip one or more cards in consecutive order , only once.
Your task is to help Fred flip the cards in such a way that the sum of the numbers, on front face of the cards is the maximum.
Input Specification:
input1: An integer N denoting the number of cards(1<=N<=500)
input2: An integer array containing N integer, where the ith integer denotes
The value on the front of the card(-1000<=input2[i]<=1000)
Output Specifications:
Return the maximum sum of the numbers, on the front of the card
Example 1:
Input1 : 5
Input2 : -2 3 -1 -4 -2
Output: 8
Explanation:
Since Fred can flip consecutive cards only once, he chooses to flip the last three cards, which results in the maximum sum (-2+3+1+4+2) i.e. 8
Therefore 8 is returned as the output.
Example 2:
Input1: 5
Input2: -1 2 3 4 -5
Output: 13
Explanation:
Since Fred can flip consecutive cards only once, he chooses to flip the last three cards,
which results in the maximum sum (-1+2+3+4+5) i.e. 13
Therefore 13 is returned as the output.
Input:
5
-2 3 -1 -4 -2
Output:
13
- Answer
Python
#https://codewindow.in
#join our telegram channel @codewindow
import sys
def minsubarraysum(n, a):
min_sum = sys.maxsize
curr_sum = 0;
for i in range(0, n):
curr_sum = curr_sum + a[i]
if curr_sum < min_sum:
min_sum = curr_sum
if curr_sum > 0:
curr_sum = 0
return min_sum;
n = int(input())
arr = list(map(int, input().split(" ")))
s = sum(arr)
min_sum = minsubarraysum(n, arr)
ans = s + min_sum * (-2)
print(ans)
C++
//https://codewindow.in
//join our telegram channel @codewindow
#include<bits/stdc++.h>
using namespace std;
int minsubarraysum(int a[], int n) {
int min_sum = INT_MAX;
int curr_sum = 0;
for (int i = 0 ; i < n; i++) {
curr_sum = curr_sum + a[i];
if (curr_sum < min_sum)
min_sum = curr_sum;
if (curr_sum > 0)
curr_sum = 0;
}
return min_sum;
}
int main() {
int n;
cin >> n;
int a[n];
int sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
int min_sum = minsubarraysum(a, n);
int ans = sum + (min_sum * (-2));
cout << ans;
}
JAVA
//https://codewindow.in
//join our telegram channel @codewindow
import java.util.*;
import java.lang.*;
import java.io.*;
class CodeWindow
{
static int minsubarraysum(int a[], int n) {
int min_sum = Integer.MAX_VALUE;
int curr_sum = 0;
for (int i = 0 ; i < n; i++) {
curr_sum = curr_sum + a[i];
if (curr_sum < min_sum)
min_sum = curr_sum;
if (curr_sum > 0)
curr_sum = 0;
}
return min_sum;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[] = new int[n];
int sum = 0;
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
sum += a[i];
}
int min_sum = minsubarraysum(a, n);
int ans = sum + (min_sum * (-2));
System.out.println(ans);
}
}
Popular Category
Hot Topics
Interview Questions
1. TCS Technical Questions
2. CTS Technical Questions
3. Infosys Technical Questions
4. Capgemini Technical Questions
5. IBM Technical Questions
6. Wipro Technical Questions
7. Accenture Technical Questions
8. Deloitte Technical Questions
9. MindTree Technical Questions
10. NTT Data Technical Questions
11. Persistent Technical Questions
12. Hexaware Technical Questions
13. ZOHO Technical Questions
14. HCL Technical Questions
15. Genpact Technical Questions
16. udostres Technical Questions
17. samsung research Technical Questions
18. Media.net Technical Questions
19. Tejas Network Technical Questions
20. Kantar Technical Questions
21. Itron Technical Questions
22. Fractal Analyst Technical Questions
23. Lexmark International Technical Questions
24. Infeneon Technologies Technical Questions
25. Razorpay Technical Questions
26. payu Technical Questions
27. Navi Technical Questions
28. Tata Cliq Technical Questions
29. Grab Technical Questions
30. Schlumberger Technical Questions
31. Latenview Analytics Technical Questions
32. GreayB Technical Questions
33. Tally solution Technical Questions
34. Amagi Technical Questions
35. HFCL Technical Questions
36. Slice Technical Questions
37. Larsen & Turbo Technical Questions
38. Zenser Technical Questions
39. Siemens Technical Questions
40. Exon Movie Technical Questions
41. Gupshup Technical Questions
42. Smart cube Technical Questions
43. Celebal Tech Technical Questions
44. Teg Analytics Technical Questions
45. Commvault Systems Technical Questions
46. Incuture Technical Questions
47. Tiger Technical Questions
48. Bridgei2i Technical Questions
49. code nation Technical Questions
50. DE Show Technical Questions
51. Amazon Technical Questions
52. Flipkart Technical Questions
53. Morgan Stanly Technical Questions
54. duutsche bank Technical Questions
55. textas Technical Questions
56. pwc Technical Questions
57. salesforce Technical Questions
58. optum Technical Questions
59. Nvidia Technical Questions
60. Itc Infotech Technical Questions
61. linkedin Technical Questions
62. Adobe Technical Questions