Find the final radiations of each Radiated Stations | C ++ implementation of the approach ; Function to print the final radiations ; Function to create the array of the resultant radiations ; Resultant radiations ; Declaring index counter for left and right radiation ; Effective radiation for left and right case ; Radiation for i - th station ; Radiation increment for left stations ; Radiation increment for right stations ; Print the resultant radiation for each of the stations ; Driver code ; 1 - based indexing
#include <bits/stdc++.h> NEW_LINE using namespace std ; void print ( int rStation [ ] , int n ) { for ( int i = 1 ; i <= n ; i ++ ) cout << rStation [ i ] << " ▁ " ; cout << endl ; } void radiated_Station ( int station [ ] , int n ) { int rStation [ n + 1 ] ; memset ( rStation , 0 , sizeof ( rStation ) ) ; for ( int i = 1 ; i <= n ; i ++ ) { int li = i - 1 , ri = i + 1 ; int lRad = station [ i ] - 1 , rRad = station [ i ] - 1 ; rStation [ i ] += station [ i ] ; while ( li >= 1 && lRad >= 1 ) { rStation [ li -- ] += lRad -- ; } while ( ri <= n && rRad >= 1 ) { rStation [ ri ++ ] += rRad -- ; } } print ( rStation , n ) ; } int main ( ) { int station [ ] = { 0 , 7 , 9 , 12 , 2 , 5 } ; int n = ( sizeof ( station ) / sizeof ( station [ 0 ] ) ) - 1 ; radiated_Station ( station , n ) ; return 0 ; }