Check if Array sum can be made equal to X using K triplet increment operations
Given an array arr of length N and two integers K and X. For every element arri in the array, if arri > 0 then the elements which are adjacent to it and the element itself will get incremented by 2 for all 1≤i≤N. Check if the sum of all array elements after performing the K operation is equal to X or not.
Examples:
Input: arr[] = { 0, 0, 1, 0, 0, 3 }, K = 2, X = 36
Output: True
Explanation: Initially arr[ ] = { 0, 0, 1, 0, 0, 3 }
After one operation array will be arr [ ] = { 0, 2, 3, 4, 4, 5 }
after Second times array will be arr [ ] = { 2, 4, 7, 8, 8, 7 }
Thus, sum of array is 36 which is equal to X
Input: arr[ ] = {10, 6, 12, 8, 10, 8}, K = 2, X = 25
Output: False
Approach: This problem can be solved using brute force hit and trial technique:
- Traverse the given array
- For every index, increment the value at that index and its adjacent index by 2, except for the first and last element because they have only one element adjacent to them.
- Repeat the operations for every possibility in given array till array sum becomes at most X
- If after K operations, in any possibility, the array sum becomes X, return true.
- Else return false, if no such possibility exists.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool find( int arr[], int N, int K, int Target)
{
while (K--) {
for ( int i = 0; i < N; i++) {
if (arr[i] > 0 && i == 0) {
arr[i + 1] = arr[i + 1] + 2;
}
else if (arr[i] > 0 && i == N - 1) {
arr[N - 2] = arr[N - 2] + 2;
}
else if (arr[i] > 0) {
arr[i - 1] = arr[i - 1] + 2;
arr[i + 1] = arr[i + 1] + 2;
}
}
}
int ans = accumulate(arr, arr + N, 0);
if (ans == Target) {
return 1;
}
else {
return 0;
}
}
int main()
{
int arr[] = { 0, 0, 1, 0, 0, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 2;
int Target = 36;
cout << (find(arr, N, K, Target)
? "true"
: "false" );
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static boolean find( int arr[], int N, int K,
int Target)
{
while ((K--) != 0 ) {
for ( int i = 0 ; i < N; i++)
{
if (arr[i] > 0 && i == 0 ) {
arr[i + 1 ] = arr[i + 1 ] + 2 ;
}
else if (arr[i] > 0 && i == N - 1 ) {
arr[N - 2 ] = arr[N - 2 ] + 2 ;
}
else if (arr[i] > 0 ) {
arr[i - 1 ] = arr[i - 1 ] + 2 ;
arr[i + 1 ] = arr[i + 1 ] + 2 ;
}
}
}
int ans = 0 ;
for ( int i = 0 ; i < arr.length; i++) {
ans += arr[i];
}
if (ans == Target) {
return true ;
}
else {
return false ;
}
}
public static void main(String[] args)
{
int arr[] = new int [] { 0 , 0 , 1 , 0 , 0 , 3 };
int N = arr.length;
int K = 2 ;
int Target = 36 ;
System.out.print(
(find(arr, N, K, Target) ? "true" : "false" ));
}
}
|
Python3
def find(arr, N, K, Target) :
for j in range (K, 1 , - 1 ):
for i in range ( 0 , N):
if (arr[i] > 0 and i = = 0 ) :
arr[i + 1 ] = arr[i + 1 ] + 2
elif (arr[i] > 0 and i = = N - 1 ) :
arr[N - 2 ] = arr[N - 2 ] + 2
elif (arr[i] > 0 ) :
arr[i - 1 ] = arr[i - 1 ] + 2
arr[i + 1 ] = arr[i + 1 ] + 2
ans = 0
for i in range ( 0 , len (arr)):
ans + = arr[i]
if (ans = = Target) :
return 1
else :
return 0
arr = [ 0 , 0 , 1 , 0 , 0 , 3 ]
N = len (arr)
K = 2
Target = 36
print ( "false" if find(arr, N, K, Target) else "true" )
|
C#
using System;
public class GFG{
static bool find( int [] arr, int N, int K,
int Target)
{
while ((K--) != 0) {
for ( int i = 0; i < N; i++)
{
if (arr[i] > 0 && i == 0) {
arr[i + 1] = arr[i + 1] + 2;
}
else if (arr[i] > 0 && i == N - 1) {
arr[N - 2] = arr[N - 2] + 2;
}
else if (arr[i] > 0) {
arr[i - 1] = arr[i - 1] + 2;
arr[i + 1] = arr[i + 1] + 2;
}
}
}
int ans = 0;
for ( int i = 0; i < arr.Length; i++) {
ans += arr[i];
}
if (ans == Target) {
return true ;
}
else {
return false ;
}
}
static public void Main (){
int [] arr = { 0, 0, 1, 0, 0, 3 };
int N = arr.Length;
int K = 2;
int Target = 36;
Console.Write(
(find(arr, N, K, Target) ? "true" : "false" ));
}
}
|
Javascript
<script>
const find = (arr, N, K, Target) => {
while (K--) {
for (let i = 0; i < N; i++) {
if (arr[i] > 0 && i == 0) {
arr[i + 1] = arr[i + 1] + 2;
}
else if (arr[i] > 0 && i == N - 1) {
arr[N - 2] = arr[N - 2] + 2;
}
else if (arr[i] > 0) {
arr[i - 1] = arr[i - 1] + 2;
arr[i + 1] = arr[i + 1] + 2;
}
}
}
let ans = 0;
for (let indx in arr) {
ans += arr[indx];
}
if (ans == Target) {
return 1;
}
else {
return 0;
}
}
let arr = [0, 0, 1, 0, 0, 3];
let N = arr.length;
let K = 2;
let Target = 36;
(find(arr, N, K, Target)
? document.write( "true" )
: document.write( "false" ));
</script>
|
Time Complexity: O(N * K)
Auxiliary Space: O(1)
Last Updated :
10 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...